Labs
- We will be creating and testing a class to model a Car.
-
Create a
Carclass. It will have fields for- make
- model
- color
- numberOfWheels
- purchasePrice
Add a
getCarData()method that returns aStringwith all of the car data.Add a
displayCar()method that prints the object's data to the screen. (This will be very similar togetCarData. In fact, it is better in terms of design ifdisplayCarcallsgetCarDataand prints the return value to the screen.)(Solution: Car.java)
-
Create a
CarTesterclass.- In its
mainmethod, create twoCarobjects (usingnew). Initialize all fields. - In
main, calldisplayCarand visually verify that the output is what you expect. - Also call
getCarDatafor eachCarand print its return value to the screen.
(Solution: CarTester.java)
- In its
-
Create a
DMVSimulatorclass. It will: - Prompt the user to enter data for a
Car. - Upon entry of all data, store the data in a new
Carinstance. Then display theCar's information to the screen. - Prompt the user to calculate tax (C) or exit (E)
- If the user types C, calculate tax, where
tax = purchasePrice * 0.01 * numberOfWheels. Then the program ends. - If the user types E, print an exit message to the screen, and end the program with the command
System.exit(0). - If the user types a different command, prompt the user again. Keep prompting until the user enters C or E.
- If the user types C, calculate tax, where
(Solution: DMVSimulator.java)
-
We will create a thermometer. The formulas for Celsius and Fahrenheit are
celsius = (fahrenheit - 32) / 1.8 fahrenheit = (celsius * 1.8) + 32 -
Create a
Thermometerclass which has-
Fields:
charscale - (C or F).doublecurrentTemp - the temperature reading. -
Methods:
getTempAsCelsius- return a temperature based on the thermometer's scale.getTempAsFahrenheit- return a temperature based on the thermometer's scale.
(Solution: Thermometer.java)
-
-
Create a
ThermometerDriverclass. It will prompt a user for a temperature and scale, then print the temperature (in both Celsius and Fahrenheit) to the screen.(Hint: Remember that the
String"C"andchar'C'are not the same. Use theStringmethodcharAt(0)to convert theString"C" or "F" into achar.)(Solution: ThermometerDriver.java)
Optional: Place the user interaction in an infinite loop. If the user types
Qfor the scale, exit the program.