Question
public class CarDriver { public static void main(String[] args) { // Create two objects and initialize using constructors Car car1 = new Car(); Car car2
public class CarDriver { public static void main(String[] args) { // Create two objects and initialize using constructors Car car1 = new Car(); Car car2 = new Car(); // Print the information using toString method System.out.println("Car 1: " + car1.toString()); System.out.println("Car 2: " + car2.toString()); // set car1 fields car1.speed = 188; car1.make = "BMW"; car1.yearModel = 2020; car1.numberOfWheels = 4; car1.fuelLevel = 2.0; // set car2 fields car2.speed = 200; car2.make = "Tesla"; car2.yearModel = 2020; car2.numberOfWheels = 4; car2.fuelLevel = 5.0; System.out.println(); // Print the information using toString method System.out.println("Car 1: " + car1.toString()); System.out.println("Car 2: " + car2.toString()) // Accelerate 4 times car1.accelerate(); car1.accelerate(); car1.accelerate(); car1.accelerate();
// Brake 6 times car2.brake(); car2.brake(); car2.brake(); car2.brake(); car2.brake(); car2.brake(); System.out.println(" After 4 accelerations for car1 and 6 brakes for car 4"); // Print the information using toString method System.out.println("Car 1: " + car1.toString()); System.out.println("Car 2: " + car2.toString()); // Create two more objects Car car3 = new Car(); Car car4 = new Car();
// set car3 fields car3.speed = 25; car3.make = "Google"; car3.yearModel = 2022; car3.numberOfWheels = 4; car3.fuelLevel = 5.0;
// set car4 fields car4.speed = 20; car4.make = "Apple"; car4.yearModel = 2022; car4.numberOfWheels = 2; car4.fuelLevel = 5.0;
System.out.println(); System.out.println("Car 3: " + car3.toString()); System.out.println("Car 4: " + car4.toString()); // Let's accelerate car3 a couple of times // and brake car4 the same amount of times for(int i = 1; i
// Change car3 number of wheels car3.numberOfWheels = 2;
System.out.println(); System.out.println("Car 3: " + car3.toString()); System.out.println("Car 4: " + car4.toString());
// Test equality if(car3.equals(car4)){ System.out.println("The cars are equal"); } else{ System.out.println("The cars are not equal"); }
// Change car4 make car4.make = "Google";
System.out.println(); System.out.println("Car 3: " + car3.toString()); System.out.println("Car 4: " + car4.toString());
// Test equality if(car3.equals(car4)){ System.out.println("The cars are equal"); } else{ System.out.println("The cars are not equal"); } }
} ------------------------------------------------------------------------------------------------------------------------
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 another DateTime) 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 his hour, mis 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] 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 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. Lab Assignment 03, Object-Oriented Programming, CSE 271 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] 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: , Speed: 0, Fuel Level: 0.0 Car 2: Make: null. Year: , Wheels: 0, Speed: , 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started