Description
In this lab, you will practice how to create a class and define its methods. Create a project in Eclipse
named Lab_03. You are going to design two classes in this project.
You must make Javadoc style comments for all methods and classes including parameter and return
description. Follow the commenting style discussed in the lecture.
• Class DateTime:
Create a class name DateTime that has the following properties:
• int second. The second field of the time ranges between 0 and 59.
• int minute. The minute field of the time ranges between 0 and 59.
• int hour. The hour field of the time in 24 hours format ranges between 0 and 23.
• int day. The day field of the calendar date ranges between 1 and 31.
• String month. The month field of the calendar date. For example: “January”.
• int year. The year filed of the calendar date. For example: 2009
The class should also have the following methods:
• Create a method name public boolean equals(DateTime anotherDateTime) which
should return true if both DateTime objects have the same second, minute, hour, day, month
and year, otherwise it should return false.
• Create another method name public String toString() which returns a string
representation of the date and time, i.e. “12:59:45 23 January 2020”. The time format is in
hh:mm:ss, where h is hour, m is minute and s is second.
Test your DateTime class using the DateTimeDriver class posted on Canvas. Download
DateTimeDriver.java and add it to your project. Run your program and it should have the
following output: [Do not modify any code in the DateTimeDriver class]
• Class Car:
Create a class named Car which has the following properties:
• int yearModel : The yearModel field is an integer that holds the car’s year model.
• String make : The make field is a String reference that holds the make of the car.
• int numberOfWheels: The number of wheels the car has.
• int speed: The speed field is an integer that holds the car’s current speed in mile per hour.
The speed cannot be a negative number. The maximum speed of a car is 200 mph. You have
to make sure that the speed is within the valid range when a car accelerates or brakes.
DateTime 1: 12:21:59 25 January 2020
DateTime 2: 12:43:59 25 January 2009
Two DateTime objects are not equal
DateTime 1: 12:21:59 25 January 2020
DateTime 2: 12:43:59 25 January 2020
Two DateTime objects are not equal
DateTime 1: 12:43:59 25 January 2020
DateTime 2: 12:43:59 25 January 2020
Two DateTime objects are equal
Lab Assignment 03, Object-Oriented Programming, CSE 271, Spring 2020
Department of Computer Science and Engineering, Miami University
Java Class, Equals Method, toString Method, Javadoc Comments
• double fuelLevel: A double that holds the current fuel level of the car. The value of fuel level
ranges between 0 and 10.0. You have to make sure the value is always within this range when
you accelerate.
The class should also have the following methods:
• public void accelerate(): The method increments the car’s speed by 4 miles per hour and
decreases the fuel level by 0.5. The method does not return anything. Don’t accelerate if the car
does not have enough fuel (0.5 amount) to do that. If the current speed is the maximum then
acceleration won’t increase speed but will burn fuel.
• public void brake(): The method decrements the car’s speed by 3 miles per hour. The method
does not return anything. The speed cannot be negative as a result of a break. If you hit break
while the car is running at 3 miles/hour or less then hitting the break should reduce the speed to
0 miles/hour.
• public boolean equals(Car c): The method returns true if the two cars have the same number
of wheels, make and model.
• public String toString(): The method returns the String representation of the car that includes
the yearModel, make and speed of the car. When this method is called, it returns a String like
“Make: Toyota, Year: 2014, Wheels: 4, Speed: 45, Fuel Level: 7.5”.
Test your Car class using the CarDriver class posted on Canvas. Download CarDriver.java and add
to your project. Run you program and it should have the following output: [Do not modify any code
in the DateTimeDriver class]
Car 1: Make: null. Year: 0, Wheels: 0, Speed: 0, Fuel Level: 0.0
Car 2: Make: null. Year: 0, Wheels: 0, Speed: 0, Fuel Level: 0.0
Car 1: Make: BMW. Year: 2020, Wheels: 4, Speed: 188, Fuel Level: 2.0
Car 2: Make: Tesla. Year: 2020, Wheels: 4, Speed: 200, Fuel Level: 5.0
After 4 accelerations for car1 and 6 brakes for car 4
Car 1: Make: BMW. Year: 2020, Wheels: 4, Speed: 200, Fuel Level: 0.0
Car 2: Make: Tesla. Year: 2020, Wheels: 4, Speed: 182, Fuel Level: 5.0
Car 3: Make: Google. Year: 2022, Wheels: 4, Speed: 25, Fuel Level: 5.0
Car 4: Make: Apple. Year: 2022, Wheels: 2, Speed: 20, Fuel Level: 5.0
After 12 accelerations for car3 and 12 brakes for car 4
Car 3: Make: Google. Year: 2022, Wheels: 4, Speed: 65, Fuel Level: 0.0
Car 4: Make: Apple. Year: 2022, Wheels: 2, Speed: 0, Fuel Level: 5.0
The cars are not equal
Car 3: Make: Google. Year: 2022, Wheels: 2, Speed: 65, Fuel Level: 0.0
Car 4: Make: Apple. Year: 2022, Wheels: 2, Speed: 0, Fuel Level: 5.0
The cars are not equal
Car 3: Make: Google. Year: 2022, Wheels: 2, Speed: 65, Fuel Level: 0.0
Car 4: Make: Google. Year: 2022, Wheels: 2, Speed: 0, Fuel Level: 5.0
The cars are equal
Lab Assignment 03, Object-Oriented Programming, CSE 271, Spring 2020
Department of Computer Science and Engineering, Miami University
Java Class, Equals Method, toString Method, Javadoc Comments
Grading Rubric:
Important Note:
If you are done with the task within the lab then you need to show your work to one of the
instructors present in the lab. Make sure the file name is correct and code is well commented. No
late submission. You will get zero for the late submission.
Submission:
Submit ONLY java files to the appropriate submission folder on the Canvas by the due time.
DateTime
Declare public fields with appropriate type or class 3
equals() method with appropriate header/signature 12
toString() method with appropriate header/signature 10
Correct output 5
Car
Declare public fields with appropriate type or class 3
accelerate() method with appropriate header/signature 12
break() method with appropriate header/signature 12
equals() method with appropriate header/signature 12
toString() method with appropriate header/signature 10
Correct output 5
Javadoc comments [6 methods and 2 classes]: 8 x 2 points 16
Total 100