Objects First With Java 5E – Chapter 02 Notes

Elements of class definitions : fields, constructor, and methods.

The class header is top part of the outer wrapping of the class that essentially names the class ie

public class Car
    {
        class body and definitions
    }

Public class Car is considered the class header. Unlike methods there is no parameter list.

By convention we always start class names with a capital letter to help distinguish them from field and methods.

“public” and “class” are Java keywords or reserved words. Keywords are always lowercase.

The inner part of the class is where we define the field, constructor, and methods.

Fields provide a place to store long-lasting data (ie, persistent data). Fields store data persistently within an object.

Though data is persistent it can still change. Fields values can change often, hence the name variable.

The constructors are responsible for initializing an object correctly when it is first created.

Once the object has been created, the constructor plays no further role in that objects life.

In Java fields are automatically initialized to a default value if they are not explicitly initialized. Interger fields default is zero. Boolean default value is false. The default value of any object reference is null.

** Understand constructor / method space **

Is the constructor space is used to store parameters during the object creation? Does the constructor / method space take up space in memory? Am I overthinking this?

public Car(int topSpeed)

formal parameter is the name given to the parameter and actual parameter is the value supplied for the parameter.

The methods implement the behavior of an object; they provide it’s functionality.

There are two types of methods: accessor and mutator method. Alternatively get and set methods respectively.

Best practice define all fields at the start of the class definition.

For now we will always define fields as private.

Parameters are variables that are defined in the header of a constructor or method.

** Java does allow void methods to have a special return statement. This takes the form

return;

and simple causes the method to exit without executing any further code.

Exercise 2.8 Check whether or not it is possible to leave out the public from the outer wrapper of the TicketMachine class.

Exercise 2.8 It is possible to leave out public from the class and it appears to compile correctly.

Exercise 2.9 It in not possible to leave out the class from the outer wrapper of the class header. Class, enum or interface expected.

Exercise 2.11 What are two features of the constructor that make it look signifigantly different from the methods of the class.

Exercise 2.11 The two main features that make the constructor different from the methods of the class is that no return type is specified in the constructor and method names do not start with capital letters.

Exercise 2.22 The following obect creation will result in the constructor  of the Dat class being called. Can you wirte the constructor’s  header?

new Date("March", 23, 1861)

Exercise 2.22

public Date(String month, int day, int year)

Exercise 2.28 Compare the method headers of getPrice and printTicket. Apart from their names what is the main difference between them?

Exercise 2.28 getPrice has a return type of int while printTicket has a void return type.

Exersise 2.29 insertMoney and printTicket do not have return types becuase they are not accesor methods.

Exercise 2.33 score = score + points;

Exercise 2.34 The increase method is a mutator method. We can demonstrate this using an accessor method that returns the points field to view the changes that the increase method made.

Exercise 2.35 price = price – amount;

 

Posted in Programming | Leave a comment

Unity 4 – Experimenting with Cars

September 05 / 2014

Followed this tutorial

http://forum.unity3d.com/threads/basic-to-advanced-car-or-vehicle-tutorial.86703/

Some things to keep in mind that was not mentioned in the tutorial

  1. The forward direction of your car is the Z axis – make sure all object Z axis are aligned with the word Z axis during setup
  2. Make sure the wheel collider gizmo line is pointing from the center down – the direction of the ground.
  3. When adding the wheels with wheel colliders to the CarController1 wheels array start with; frontRight, frontLeft, rearRight, rearLeft
  4. Fix camera smooth follow issues by parenting an empty to the car and using the empty as the Target for the camera to follow

Another thing to consider is scale and unit setup; 1 Unit in unity is supposed to represent 1 Meter. That makes our car 5 meters long and 3 meters wide; not to realistic.

Also there is the issue of the car rolling over around corners. So far the most practical solution would be to implement stabilizaton as described in the following tutorial.

http://forum.unity3d.com/threads/how-to-make-a-physically-real-stable-car-with-wheelcolliders.50643/

 

Posted in Uncategorized, Unity3D | Leave a comment

Unity4 Scripting – Choose / Select from Array

A simple script that cycles through an array

using UnityEngine;
using System.Collections;

public class Selector3 : MonoBehaviour {

public GameObject[] selectionSet;
GameObject selection;
//GameObject nextSelection;
//GameObject previousSelection;
int index = 0;

//Vector3 previousPosition = new Vector3(-1,0,0);
Vector3 selectionPosition = new Vector3(0,0,0);
//Vector3 nextPosition = new Vector3(1,0,0);

// Use this for initialization
void Start () {

selection = Instantiate (selectionSet [index], selectionPosition, Quaternion.identity) as GameObject;

}

// Update is called once per frame
void Update () {

if(Input.GetKeyDown(KeyCode.RightArrow)){

Destroy(selection);

if(index < (selectionSet.Length – 1)){ index++; } else { index = 0; } selection = Instantiate (selectionSet [index], selectionPosition, Quaternion.identity) as GameObject; } if(Input.GetKeyDown(KeyCode.LeftArrow)){ Destroy(selection); if(index > 0){
index–;
} else {
index = (selectionSet.Length – 1);
}

selection = Instantiate (selectionSet [index], selectionPosition, Quaternion.identity) as GameObject;
}

}
}

Posted in Unity3D | Leave a comment