CS35 Lab #1: Auto Dealership


Due 11:59pm Tuesday, 27 January 2008

This homework will require you to write some basic classes in Java. You should save your programs in your cs35/homework/01 directory. This directory will appear if you run update35. The program handin35 will only submit files in this directory. For this assignment, you should work by yourself.

Auto Dealership

Imagine that you are working as a software developer for a consulting company. You are hired to build an interactive system that can keep track of the car and truck inventory at a local auto dealership.

Auto, Car, & Truck Classes
You will implement an Auto class, a Car class and a Truck class. Figure 1 shows the relationship between the classes and the list of data members and methods of each class (it is up to you to determine each data member's type).



Figure 1


Each class will have a constructor that takes arguments to initialize all the relevant data members. For the Auto, basePrice is the cost to the dealer, percentMarkup represents the profit made by selling the automobile and should be initially set to 1.10. miles is the current number of miles on each car and should be set to a random number between 500 and 2,500 miles. isForSale should indicate whether the car is for sale or has already been sold. You will also need to write a printAuto() method, and a couple of "get" and "set" methods for the various data members.

The Car class has four data members, one for each feature. The features should be assigned at random where hasCupHolders occurs with 1/2 probability, hasLeatherSeats with 1/3 probability, hasGPS with 1/4 probability, and hasDVDplayer with 1/5 probability. Similarily, the Truck class has three data members representing different features. The features should be assigned at random where hasCupHolders occurs with 1/2 probabilty, has4WD (four wheel drive) with 2/3 probability, and twoCapacity should be a random number between 2 and 8.

ASIDE: Randomness

Random numbers can be generated using an instance of the Random class. The class must be imported from the Java utility library. To do this, place the following code at the top of the Auto class .java file.

  
  import java.util.Random;

  public clas Auto{
  
    //data member
    ...
    static Random rng = new Random(System.currentTimeMillis());
    
    //constructors
    public Auto( ...

       miles = rng.nextInt(2000)+500; 

You then need to create a new instance of Random class. (See above.) It is important that this object be declared static since we only need one random number generator for all instance of type Auto (and thus Car and Truck since they will inherit rng.) In the code example above, we create a random number between 0 and 1999 and then add 500. This makes our range between 500 and 2499. You can learn more about the this class by reading the java.util.Random javadocs.

Both Car and Truck classes should have a printAuto() methods that overloads the Auto class method of the same name. (You can use super.printAuto() to call the Auto class printAuto() method from the Car class printAuto() method and Truck class printAuto() method.) For example, a Truck object's printAuto() should print the following information:

	The 1992 Chevy Blazer:
	--------------------- 
         2,100 miles
 	 retail price is $18000.0
 	 Special Features:		
  	      * 6 ton capacity			
  	      * 4 wheel drive
  	      * cup holders
         This automobile has already been sold.

Inventory
You should also write an Inventory class that contains the main() method for your code. The inventory should store an array of Auto objects. You will need to create a constructor that creates at least 3 cars and 3 trucks:

    inventory[0] = new Car("Toyota", "Prius", 2007, 22000);
    inventory[1] = new Truck("Ford", "F-150", 2009, 23000);
    ...

You will also need to create a number of helper "get()", "set()", and "print()" methods for the interactive mode. (See Below)
Going Interactive
You will also need to code up an interactive menu for the salespeople. You should write a method called goInteractive() that is called from the main() method in the Inventory class. The pseudocode for the goInterative() method is as follows:
     goInteractive()
       while not Exit
         printMenu()
         readResponse()
         Based on Option, do
            1) PrintInventory
                a) Print Entire Inventory
                b) Print Active Inventory // only cars for sale
                c) Print Old Inventory    // only car that have been sold
            2) Add New Auto 
                 a) Request Car or Truck
                 b) Request Make
                    ...
                 f) Request Base Price
            3) Sell Auto 
                 a) Request Auto Index
                 b) Print Auto
                 c) Ask if this is correct auto?
                 d) If yes, change isForSale to false
            4) Test Drive Auto 
                 a) Request Auto Index
                 b) Request length of test drive
            5) Change Price
                 a) Request Auto Index
                 b) Report Base Price, percent markup, and price
                 c) Ask for new percent markup
                 d) Print out new price
            6) Exit Program
It will be important that you break up this method into a number of lower-level methods to handle each of the options.

In order to read in information from the user, you should use an instance of the Scanner class. You can find the documentation for this class in the java.util.Scanner javadoc page. Like the java.util.Random example above, you will need to import this class, create a Scanner object using new, and then use that object's methods as described in the Javadoc. (Also like Random, this object should also be declared static.)

You should also make sure that your code continues to work even if the user enters bad input. For example, if you ask for a menu option that should be an integer and the user types "asdf", you should reply "'asdf' is not a valid option, please type a number between 1 and 6." This may be repeated until the user types a valid entry. (Hint: use a while loop.)

Bonus: Using exception to catch bad user input.
One way to create a fluid interative menu is to use a number of while loops or if...then conditional statements. However, Java has a more powerful error checking mechanism. See Section 2.3 in the textbook. If you can correctly use exceptions in your code, you can receive 1 or 2 bonus points. (e.g., You should create an exception class, throw the exception when a user error occurs, and catch the exception so that you can properly deal with the error.
Tips and Tricks
1. Start Early! Java can be tricky at first. If you have not programmed in Java before (this applies to most of the class), allow extra time on this assignment to learn Java. Write a few lines, then compile, fix bugs, and test.

2. All the top-down design principles from cs21 apply in any computer science course. Do not try to write the entire program in one go. Break the problem into managable pieces.

3. Make sure you test your code. Have a friend play with your interactive system to make sure it doesn't break.

Submitting
Along with your java source code, you should hand in a file named README. These files will be imported automatically via handin35. Your README should include a brief summary of your code (what does it do?) along with any known problems/bugs. Also, please let me know if you have attempted the extra credit.

I will plan to run the following two commands:

linux$ javac Inventory.java 
linux$ java Inventory
Your code should compile and run without any bugs. Make sure it works before you submit!!! If you don't, you are likely to needlessly lose points.

Once you are satisfied with your programs, hand them in by typing handin35 in the unix prompt. You may run handin35 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.