CS 120   Winter 2006,  Instructor:  Jeffrey Horn   7pm version  FINAL EXAM         NAME:________________________



INSTRUCTIONS:

Partial credit:  I am big on this.  Show your work.  I will give zero credit for a blank answer (I am forced to!) but on the other hand I will give you more than zero if you show ANY work at all!  You can always use more paper, and you can always CROSS OFF what you don't like, but don't throw anything away, or erase anything, that shows some thinking, whether correct or not!


for QUESTIONS 6 and 7:

Here is a class named Car:
class Car
          {
              private int age; 
              private Company manufacturer;
              private String model;

              private Color color;

              private int year_made;

              private int mileage;

              private int vehicle_ID_number;
              private History written_history;
              private Person owner;
             

              public Car()
                     {
                        age = 0;

                                                mileage = 0;
                        model = "";
                        color = Color.black;
                        vehicle_ID_number = 0;
                        History = new History();
                      }

              public int incr_odometer(int new_miles) 

                  {mileage += new_miles;   return mileage; }

                            public void change_color(Color in_color) 

                     { color = in_color;}             

              public void change_model(String model_name) 

                     { model = model_name;}                       

              public void change_owner(Person next_owner)

                     { owner = next_owner ;}

              public int get_odometer()
                   {  ________________________;  }

                        //  Assume more methods, but not more

                                       //  state variables than shown.

 

 

 

 

 

 

 

 

 

 

 

 

 

QUESTION 6:  Basic OO Concepts:  (10 points)
  1. Name any and all state variables of Car that are NOT initialized in the default constructor.  (A default constructor is a constructor that takes no arguments (i.e., no input parameters)):

     

  2. In the above code, list ALL of the different classes that are being used (and don't include standard Java  data types like int or standard Java classes like String, but DO include "Person" for example...)

     

  3. Write a comment for the method incr_odometer(int) that accurately describes everything it does.

     

  4. Write the return statement that should be the body (blank line above) in the method "get_odometer" above, which returns the current value of the odometer for that car:

     

  5. Write a line or two of Java that declares a new variable "myCar" to be of type Car
  6. Now write code that CREATES a new object of type Car,  assigns it to the variable "myCar", and then makes it a pink (color) Cadillac (model).  Assume there exists a color constant "Color.pink" in Java!

     

  7. Create a "secret" (by that I mean private) method to reset the odometer (mileage) back by X miles.  (Only you will know about this!)  Show it:

 

 

 

 

 

 

 

 

 

 

 

 

QUESTION 7:  Arrays of Objects

  1. Write a line of code to declare an array named "parkingLot" of objects of class Car.  (2 pt.s)
     
     
  2. Write a line of code to initialize (create) the ARRAY "parkingLot" to be of size 1000.  (2 pt.s)

     
  3. Now write code to put myCar (the pink Caddy) into the array in the fourth position.  Assume she has already been created and initialized as in question 6 above.  (2 pt.s)

     

  4. Write code that goes through the "parkingLot" array and computes the average mileage of all the cars.  Do this by calling "get_odomenter" for each car, from the first car (i.e., parkingLot[0]) to the last car (assume there exist an integer variable, int num_cars, that indicates how many cars have been created, initialized, and inserted into the array parkingLot;  thus parkingLot[0]..parkingLot[num_cars-1]  has real, valid cars in it.   (Average mileage is the sum of mileages divided by the number of cars!) (4 pt.s)