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.
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.
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[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)
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 ProgramIt 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.)
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.
I will plan to run the following two commands:
linux$ javac Inventory.java linux$ java InventoryYour 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.