LAB02 Chapter 06 (A First Look at Classes) - Part 2 - 2
LAB02 Chapter 06 (A First Look at Classes) - Part 2 - 2
LAB 02
• name. The name field references a String object that holds the
employee’s name.
• idNumber. The idNumber is an int variable that holds the employee’s ID
number.
• department. The department field references a String object that holds
the name of the department where the employee works.
• position. The position field references a String object that holds the
employee’s job title.
Write appropriate mutator methods that store values in these fields and accessor
methods that return the values in these fields. Once you have written the class,
write a separate program that creates three Employee objects to hold the
following data:
The program should store this data in the three objects and then display the data
for each employee on the screen.
Solution:
UML diagram:
Employee
- name : String
- idNumber : int
- department : String
- position : String
+ Employee (name : String, idNumber: int, department
: String, position : String)
+ Employee(name : String, idNumber : int)
+ Employee()
+ getName() : String
+ getIdNumber() : String
+ getDepartment() : String
+ getPosition() : String
+ setName(name : String) : void
+ setIdNumber(idNumber: int) : void
+ setDepartment(department : String) : void
+ setPosition(position : String) : void
Code:
Q2. Car Class (Q2 page 396)
• yearModel. The yearModel field is an int that holds the car’s year model.
• make. The make field references 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 constructor and other 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.
• Accessors. Appropriate accessor methods should get the values stored in an
object’s yearModel, make, and speed fields.
• accelerate. The accelerate method should add 5 to the speed field each time
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 creates 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 display it. Then call the brake method five times.
After each call to the brake method, get the current speed of the car and display
it.
Solution:
UML diagram:
Car
- yearModel: int
- make : String
- speed : int
+ Car (yearModel : int, make : String)
+ getYearModel() : int
+ getMake() : String
+ getSpeed() : int
+ accelerate() : void
+ brake():void
Code:
Q3. Circle Class (Q7 page 398)
• radius: a double
• PI: a final double initialized with the value 3.14159
Write a program that demonstrates the Circle class by asking the user for the
circle’s radius, creating a Circle object, and then reporting the circle’s area,
diameter, and circumference.
Solution:
UML diagram:
Circle
- radius: double
- PI : double = 3.14159
+ Circle (radius : double)
+ Circle ()
+ setRadius(double radius) : void
+ getRadius() : double
+ getArea() : double
+ getDiameter() : double
+ getCircumreference(): double
Code:
Q4. Temperature Class (Q8 page 398)
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
Celsius and Kelvin.
Solution:
UML diagram:
Temperature
- ftemp : double
+ Temperature (ftemp: double)
+ setFahrenheit(ftemp : double) : void
+ getFahrenheit() : double
+ getCelsius() : double
+ getKelvin() : double
Code:
The End