Assignment 8
Up Assignment 1 Assignment 2 Assignment 3 Assignment 4 Assignment 5 Assignment 6 Assignment 7 Assignment 8 Assignment 9 Assignment 10

 

Object-Oriented Programming: A Car Class Hierarchy

Due: Monday, Nov 12, 5:00 PM

[ modified 2 PM on Wed Nov 7 ]

This assignment tests your ability to work with inheritance, class hierarchies, overriding, and abstract classes.  It's more of a thinking exercise than doing: the program in the end doesn't do much, but its structure requires a lot of thought.  You should understand all of Chapter 9 and Section 10.6.

Define a class hierarchy according to the following specifications.

  1. Abstract class Car has the following components:

    Protected instance variables:
        model   -- a string representing the model
        color     -- a string representing the color
        doors    -- an integer representing the number of doors
        cost       -- a double reflecting the wholesale (dealer) cost
        misc      -- a string representing miscellaneous information

    Properties Model, Color, Doors, Cost, and Misc.

    A constructor Car that takes as arguments all of the information implied by the instance variables.  Also create an overloaded version that does not take the last argument (misc), but rather initializes it to the empty string.

    An abstract method Price whose purpose will be to compute and return (as a double) the price of the car.

    A method Print which neatly prints out the information contained in the instance variables (including misc), but instead of printing the cost, it prints the price, obtained by calling the Price method (even though it is abstract!).
     
  2. Class Chevy inherits from Car and additionally has the following components:

    A protected instance variable margin, a double representing the profit margin.

    A property Margin.

    Two versions of the constructor Chevy that take as arguments all that the two versions of Car take, and also an argument for the margin.  It uses base to set up the inherited instance variables, and sets Margin itself.

    A method Price that computes the price by adding the Margin to the Cost.

    A method Print that overrides the one in Car, and first prints out the make (Chevrolet), then uses base to print all of the standard info.
     
  3. Class Ford inherits from Car and is similar to Chevy except that:

    The two Ford constructors take an extra string argument representing accessories; the M property should be set to this value.

    Instead of margin, there is a protected instance variable profit representing a percentage profit.

    There is a property Profit.

    The Price method should compute the price as (1+Profit)*Cost.

    A method Print that overrides the one in Car, and first prints out the make (Ford), then uses base to print all of the standard info.
     
  4. Class Jeep inherits from Car and is similar to Ford except that:

    There is a protected Boolean instance variable fourWD that indicates whether or not the vehicle has four-wheel drive.

    These is a property FourWD.

    The constructors for Jeep take an extra argument, a Boolean value indicating whether or not the vehicle has four-wheel drive.

    A method Print that overrides the one in Car, and first prints out the make (Jeep), then uses base to print all of the standard info.   In addition, it should print a line saying whether or not the vehicle has four-wheel drive.

Extra Credit (1 point out of 10):  Add a method Make to the Car class that returns a string indicating the make of the car, but do not add any instance variables to make this work.  In addition, add a protected static instance variable and a public static property to each of the classes above (including the abstract class Car), that collectively keep track of the number of total cars, the number of Chevys, the number of Fords, and the number of Jeeps.

To test your design, write a console application whose Main method is written according to the outline below:

static void Main (string[] args)
{
    Car[] cars = new Car[5];  // create more if you wish!
 
    cars[0] = new Chevy ("Corvette", "red", 2, 32150, 1500);
    cars[1] = new Ford ("WindStar", "green", 3, 21500, 0.10);
    cars[2] = new Chevy ("Blazer", "blue", 4, 26450, 1500);
    cars[3] = new Jeep ("Cherokee", "yellow", 4, 29000, 0.15, "ski rack", true);
    cars[4] = new Ford ("Taurus", "white", 4, 24500, 0.10, "leather seats");
 
    for (...)
    {   loop that invokes the print method on each Car in the array
        (and if you did the extra credit, also calls "Make" to determine the make of each Car)
    } // for

    // for the extra credit:
    Console.WriteLine ("\nWe have " + ... + " Chevrolets, " + ... +
                                " Fords, and " + ... + " Jeeps, for a total of " + ... + " cars." );
} // method main
 
} // class CarTest