Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Exercises: A First Look at These values should be assigned to the

Classes and Objects object’s yearModel and make fields. The


constructor should also assign 0 to the
Prof. Carolina Peña-Ortega
speed field.
 Accessor. The appropriate accessor methods
Reference: chapters 3, 4 and 5 of Gaddis’s get the values stored in an object’s
textbook (Starting out with Java) yearModel, make, and speed fields.
 accelerate. The accelerate method
For each exercise, you must do the UML diagram for the should add 5 to the speed field each time it
class, flowcharts for each method and write the Java is called.
code.  brake. The brake method should subtract
5 from the speed field each time it is
1. (Employee Class). Write a class named called.
Employee that has the following fields:
Demonstrate the class in a program that creates a
• name. The name field is a String object Car object, and then calls the accelerate
that holds the employee’s name. method five times. After each call to the
• idNumber. The idNumber is an int accelerate method, get the current speed of
variable that holds the employee’s ID number. the car and display it. Then, call the brake
• department. The department field is a method five times. After each call to the
String object that holds the name of the brake method, get the current speed of the car
department where the employee works. and display it.
• position. The position field is a
String object that holds the employee’s job 3. (Temperature Class).Write a Temperature class
title. that will hold a temperature in Fahrenheit and
provide methods to get the temperature in
Write appropriate mutator methods that store values Fahrenheit, Celsius, and Kelvin. The class should
in these fields and accessor methods that return the have the following field:
values in these fields. Once you have the written the  ftemp. A double that holds a Fahrenheit
class, write a separate program that creates three temperature.
Employee objects to hold the following data.
The class should have the following methods:
Name ID Number Department Position
Susan Meyers 47899 Accounting Vice President  Constructor. The constructor accepts a
Mark Jones 39119 IT Programmer Fahrenheit temperature (as a double) and
Joy Rogers 81774 Manufacturing Engineer
stores it in the ftemp field.
The program should store this data in the three  setFahrenheit. The setFahrenheit
objects and then display the data for each employee method accepts a Fahrenheit temperature (as
on the screen. a double) and stores it in the ftemp
field.
2. (Car Class). Write a class named Car that has the  getFahrenheit. Returns the value of the
following fields: ftemp field, as a Fahrenheit temperature
 yearModel. The yearModel field is an (no conversion required).
int that holds the car’s year model.  getCelsius. Returns the value of the ftemp
 make. The make field is a String object field converted to Celsius.
that holds the make of the car.  getKelvin. Returns the value of the ftemp
 speed. The speed field is an int that field converted to Kelvin.
holds the car’s current speed.
Use the following formula to convert the
Fahrenheit temperature to Celsius:
In addition, the class should have the following
methods.
Celsius = (5/9) (Fahrenheit - 32)
 Constructor. The constructor should accept
the car’s year model and make as arguments.
Use the following formula to convert the
Fahrenheit temperature to Kelvin:
Kelvin = ((5/9) (Fahrenheit - 32)) + 273
 getSpeedInAir. This method should return the
Demonstrate the Temperature class by number of seconds it would take a sound wave to
writing a separate program that asks the user for travel, in air, the distance stored in the distance field.
a Fahrenheit temperature. The program should The formula to calculate the amount of time it will
create an instance of the Temperature class, take the sound wave to travel the specified distance
with the value entered by the user passed to the in air is:
constructor. The program should then call the Time = distance/1100
object’s methods to display the temperature in
Celsius and Kelvin.  getSpeedInWater. This method should return
the number of seconds it would take a sound wave to
4. (TestScores Class). Design a TestScores travel, in water, the distance stored in the distance
class that has fields to hold three test scores. The field. The formula to calculate the amount of time it
class should have accessor and mutator methods for will take the sound wave to travel the specified
the test score fields and a method that returns the distance in water is:
average of the test scores. Demonstrate the class by
writing a separate program that creates an instance Time = distance/4900
of the class. The program should ask the user to
enter three test scores, which are stored in the  getSpeedInSteel. This method should return the
TestScores object. Then the program should number of seconds it would take a sound wave to
display the average of the scores, as reported by the travel, in steel, the distance stored in the distance
TestScores object. field. The formula to calculate the amount of time it
will take the sound wave to travel the specified
5. (Other TestScores Class). Design a distance in air is:
TestScores class that has fields to hold three test
scores. The class constructor should accept three test Time = distance/16400
scores as arguments and assign these arguments to
the test score fields. The class should also have Write a program to demonstrate the class. The
accessor methods for the test score fields, a method program should display a menu allowing the user to
that returns the average of the test scores, and a select air, water, or steel. Once the user has made a
method that returns the letter grade that is assigned selection, he or she should be asked to enter the
for the test score average. Use the grading scheme in distance a sound wave will travel in the selected
the following table. medium. The program will then display the amount
of time it will take. Check that the user has selected
Test Score Average Letter Grade one of the available choices from the menu.
90–100 A
80–89 B 8. (Distance Traveled). The distance a vehicle travels
70–79 C can be calculated as follows:
60–69 D
Below 60 Distance = Speed * Time
E
For example, if a train travels 40 miles per hour (mph)
6. (Running the Race). Design a class that stores the
for three hours, the distance traveled is 120 miles.
names of three runners and the time, in minutes, it
took each of them to finish a race. The class should
Design a class that stores the speed of a vehicle (in miles
have methods that return the name of the runner in
per hour) and the number of hours it has traveled. It
1st, 2nd, or 3rd place.
should have a method named getDistance that
7. (The Speed of Sound). The following table shows the returns the distance, in miles, that the vehicle has
approximate speed of sound in air, water, and steel. traveled.

Design a class that stores in a distance field the Demonstrate the class in a program that uses a loop to
distance, in feet, traveled by a sound wave. display the distance a vehicle has traveled for each hour
The class should have the appropriate accessor and of a time period specified by the user. For example, if a
mutator methods for this field. In addition, the class vehicle is traveling at 40 mph for a three-hour time
should have the following methods:
period, it should display a report similar to the one
shown here.

Hour Distance Traveled


1 40
2 80
3 120

Input Validation: Do not accept a negative number


for speed, and do not accept any value less than one
for time traveled.

9. (Population). Write a class that will predict the size


of a population of organisms. The class should store
the starting number of organisms, their average daily
population increase (as a percentage), and the
number of days they will multiply. The class should
have a method that uses a loop to display the size of
the population for each day.

Test the class in a program that asks the user for the
starting size of the population, their average daily
increase, and the number of days they will multiply.
The program should display the daily population.

Input Validation: Do not accept a number less than


2 for the starting size of the population. Do not
accept a negative number for average daily
population increase. Do not accept a number less
than 1 for the number of days they will multiply.

You might also like