Assignment #2

1.      Create a class called ’Car’. Have fields for make (String), model (String), milesPerGallon (double), year (int). Create a printInfo method for this class.

2.      Create a class called a class called ’Shirt’ that has fields for size – small, medium or large (char), color (string), cost (double), salesTax (double). Again have a method called printInfo. Also have a method called ‘calcSalesTax‘ that takes the cost of the shirt and calculates the cost of the tax. The tax rate is 10%.  Note: You may run into situations where there are several decimal places to some of your calculations.  (e.g. salesTax = 4.8223352). If that happens, don’t worry about it for now.   Bonus (up to 4 points): Research and apply the printf  function to format your so that when you output your results, the salesTax field prints to exactly 2 decimal places.

3.      Use a third class for an array review problem: Create an array of size 100 called ‘dieRolls’. Fill the array with random numbers between 1 and 6. Then ask the user to pick a number between 1 and 6. When they pick the number, output how many times that number was present in the array.

·         For example, if the user enters ‘4’, go through your array, count the number of 4s that are present, and output the result.

·         Reminder: To randomly generate a  random number between 1 and 6 you could type:

roll = (int) (Math.random() * 6) + 1;

4.      Create a fourth class for testing your objects from problem #1 and #2 above.  Inside this class, for each of the two user-defined classes above, create at least two objects.

For one of your two Car objects, ask the user to enter the value for each field. Then create the object using that data.

e.g.

Enter the make:  Honda

Enter the model: Accord

Etc…

c1.make = …whatever the user entered for the make…

etc

For the second object, you may assign values directly by “hard-coding” them:  e.g..   c2.make = “Acura”;

Repeat this process for your two Shirt objects. However, do not ask the user to enter the salesTax. Instead, calculate it by using your calcSalesTax method.

Demonstrate the use of the ‘printInfo’ method for all of your objects.

Reminders:

·         For this assignment, I don’t mind giving you some reminders. However, after this week, I will expect you to remember to do so without being reminded!

·         Reminder: Comment all of your methods using the various guidelines discussed in class.

·         Reminder: Use meaningful identifiers for variables, functions, etc. When creating objects, though, I’m fine with c1, c2, s1, s2, etc (for now).

To submit:

-          Submit a ZIP file containing all of the files used in your assignment. Submit your zip file to the dropbox.