Assignment
Problem 1:
Write a class named Car that has the following fields:
• yearModel: The yearModel field is an int that holds the car's year model.
• make: The make field is a String object that holds the make of the car.
• speed: The speed field is an int that holds the car's current speed.
In addition, the class should have the following methods :
• Constructor : The constructor should accept the car's year model and make as arguments.
These values should be assigned to the object 's yearModel and make fields. The constructor should also assign 0 to the speed field.
• Accessor: The appropriate accessor methods should be implemented to access the values stored in the object 's yearModel, make, and speed fields.
• accelerate: The accelerate method should add 5 to the speed field when it is called.
• brake: The brake method should subtract 5 from the speed field each time it is called.
Demonstrate the class in a program that contains a Car object, and then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and print it on a separate line. Then, call the brake method five times, each time printing the current speed of the car on a separate line.
Problem 2:
Design a Payroll class with the following fields:
• name : a String containing the employee's name
• idNumber: an int representing the employee's ID number
• rate: a double containing the employee's hourly pay rate
• hours: an int representing the number of hours this employee has worked
The class should also have the following methods :
• Constructor : takes the employee's name and ID number as arguments
• Accessors: allow access to all of the fields of the Payroll class
• Mutators: let the user assign values to the fields of the Payroll class
• grossPay: returns the employee's gross pay, which is calculated as the number of hours worked times the hourly pay rate.
Write another program that demonstrates the class by creating a Payroll object , then asking the user to enter the data for an employee in the order: name , ID number, rate, hours.
The program should then print out a statement in the following format (for example, if you had an employee named Chris Jacobsen with ID number 11111, who works for 5 hours at $10/hr):
Chris Jacobsen, employee number 11111, made $50.00 in gross pay.
Using text forming so that the gross pay is rounded to two decimal places.
Problem 3:
Write a Temperature class that will hold a temperature in Fahrenheit and provide methods to get the temperature in Fahrenheit, Celsius, and Kelvin. The class should have the following field:
• ftemp: a double that holds a Fahrenheit temperature.
The class should have the following methods :
• Constructor : The constructor accepts a Fahrenheit temperature (as a double ) and stores it in the ftemp field.
• setFahrenheit: The set Fahrenheit method accepts a Fahrenheit temperature (as a double ) and stores it in the ftemp field.
• getFahrenheit: Returns the value of the ftemp field as a Fahrenheit temperature (no conversion required)
• getCelsius: Returns the value of the ftemp field converted to Celsius. Use the following formula to convert to Celsius:
Celsius = (5/9) * (Fahrenheit - 32)
• getKelvin: Returns the value of the ftemp field converted to Kelvin. Use the following formula to convert to Kelvin:
Kelvin = ((5/9) * (Fahrenheit - 32)) + 273
Demonstrate the Temperature class by writing a separate program that asks the user for a Fahrenheit temperature. The program should create an instance of the Temperature class, with the value entered by the user passed to the constructor . The program should then call the object 's methods to display the temperature in the following format (for example, if the temperature in Fahrenheit was -40):
The temperature in Fahrenheit is -40.0
The temperature in Celsius is -40.0
The temperature in Kelvin is 233.0.