A good answer might be:

Is the Car class made up of smaller software objects?

  • No---it consists of some primitive (non-object) data and one method.

What are the instance variables of class Car?

  • startMiles
  • endMiles
  • gallons

What is the method of class Car?

  • calculateMPG()

Holding the State of an Object

class mpgTester
{
  public static void main ( String[] args )
  {
    Car myCar = new Car( 12000, 12340, 12.3 );

    . . . . . . 
  }
}

Objects have (i) identity, (ii) state, and (iii) behavior. The idea of "state" is that an object has characteristics that it keeps as long as it exists. Sometimes the values of the characteristics change during the lifetime of the object, but the changes are long term.

The state of an object is held it its instance variables (not in the parameters of methods, nor in the local variables of methods.) The instance variables of a class are not declared as part of any method. Here is a main() program that constructs a Car object and sets its state to:

  • startMiles = 12000
  • endMiles = 12340
  • gallons = 12.3

The object referenced by myCar will hold on to these values for as long as it exists---until the program stops.


QUESTION 3:

Could a collection of several cars be regarded as an object?

Click Here after you have answered the question