[go: up one dir, main page]

0% found this document useful (0 votes)
63 views10 pages

LAB02 Chapter 06 (A First Look at Classes) - Part 2 - 2

This document contains instructions for four programming assignments involving creating classes in Java. The assignments involve creating classes to represent: 1. An Employee class with fields for name, ID number, department, and position. It includes instructions for constructors and getter/setter methods. 2. A Car class with fields for year, make, and speed. It includes instructions for a constructor, getter methods, and methods to accelerate and brake the car. 3. A Circle class with fields for radius and PI. It includes instructions for constructors, getter/setter methods for radius, and methods to calculate area, diameter, and circumference. 4. A Temperature class with a field to store temperature in Fahrenheit.

Uploaded by

Muhanad Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views10 pages

LAB02 Chapter 06 (A First Look at Classes) - Part 2 - 2

This document contains instructions for four programming assignments involving creating classes in Java. The assignments involve creating classes to represent: 1. An Employee class with fields for name, ID number, department, and position. It includes instructions for constructors and getter/setter methods. 2. A Car class with fields for year, make, and speed. It includes instructions for a constructor, getter methods, and methods to accelerate and brake the car. 3. A Circle class with fields for radius and PI. It includes instructions for constructors, getter/setter methods for radius, and methods to calculate area, diameter, and circumference. 4. A Temperature class with a field to store temperature in Fahrenheit.

Uploaded by

Muhanad Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Faculty of Computers & Information Technology.

Programming Language#1 (ITCS1315)

LAB 02

Chapter 06 (A First Look at Classes)


[part2]

Eng. Samia M. Hassouna


Q1. Employee Class (Q1 page 396)

Write a class named Employee that has the following fields:

• 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.

The class should have the following constructors:

• A constructor that accepts the following values as arguments and assigns


them to the appropriate fields: employee’s name, employee’s ID number,
department, and position.
• A constructor that accepts the following values as arguments and assigns
them to the appropriate fields: employee’s name and ID number. The
department and position fields should be assigned an empty string ("").
• A no-arg constructor that assigns empty strings ("") to the name,
department, and position fields, and 0 to the idNumber field.

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)

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 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)

Write a Circle class that has the following fields:

• radius: a double
• PI: a final double initialized with the value 3.14159

The class should have the following methods:

• Constructor. Accepts the radius of the circle as an argument.


• Constructor. A no-arg constructor that sets the radius field to 0.0.
• setRadius. A mutator method for the radius field.
• getRadius. An accessor method for the radius field.
• getArea. Returns the area of the circle, which is calculated as area = PI *
radius * radius
• getDiameter. Returns the diameter of the circle, which is calculated as
diameter = radius * 2
• getCircumference. Returns the circumference of the circle, which is
calculated as circumference = 2 * PI * radius

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)

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 setFahrenheit 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.
• getKelvin – Returns the value of the ftemp field converted to Kelvin.

Use the following formula to convert the Fahrenheit temperature to Celsius:

Use the following formula to convert the Fahrenheit temperature to Kelvin:

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

You might also like