Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description: In this assignment, we will learn about Object Oriented Programming

ID: 3883206 • Letter: D

Question

Description:

In this assignment, we will learn about Object Oriented Programming by creating an application that uses Java classes to manage multiple cars and their gas mileage.

Requirements:

You should not use static/global variables or methods anywhere in this assignment.

Step 1:

Create a new Java project in NetBeans called “Assignment2”

Create a new Java Class in that project called "Car"

Step 2:

Implement a properly encapsulated "Car" class that has the following attributes:

The brand of the car (e.g. Honda)

Odometer reading (distance traveled since new)

Total fuel consumed since new

…and methods to perform the following tasks:

Set the brand of the car

Set the odometer reading

Set the total fuel consumed since new

Return all info about the car (you may want to call this method getInfo())

Robustly calculate (and return) Average MPG since new (properly function with zeros as input, and return -1 if input is otherwise invalid). Use exception handling as discussed in class.

Step 3:

Create a three argument constructor that lets you set the brand of the car, odometer reading, and fuel consumed since new.

Create a no argument constructor that sets the brand of the car to "none" and sets the odometer reading and fuel consumed to zero.

Step 4:

In your main method:

Create an instance of your Car class using your three argument constructor. You may assign any values you like for brand, odometer reading, and total fuel consumed.

Create another instance of your Car class using your no argument constructor. Then call your setter methods to assign any values you like for brand, odometer reading, and total fuel consumed.

Display the info for both Cars.

For both Car objects that you created, call the method to calculate average MPG since new, and display this value.

Step 5:

Export your NetBeans project to a Zip file named “Assign2_YourLastName.zip”, and upload to Canvas.

Explanation / Answer

Below is your program. Let me know in comments if you have any issue in this

Car.java

public class Car {

private String brandName;

private double odometerInMiles;

private double feulInGallonsSinceNew;

public Car() {

this.brandName = "none";

this.odometerInMiles = 0;

this.feulInGallonsSinceNew = 0;

}

public Car(String brandName, double odometerInMiles, double feulInGallonsSinceNew) {

this.brandName = brandName;

this.odometerInMiles = odometerInMiles;

this.feulInGallonsSinceNew = feulInGallonsSinceNew;

}

public void getinfo() {

System.out.println("Brand Name of Car : " + this.brandName);

System.out.println("Odometer Reading : " + this.odometerInMiles);

System.out.println("Total Feul Consumed since new : " + this.feulInGallonsSinceNew);

}

public double getAverageMPG() {

try {

if (this.odometerInMiles == 0.0)

return 0;

else if (this.feulInGallonsSinceNew == 0.0)

throw new ArithmeticException();

else if (this.odometerInMiles > 0.0)

return this.odometerInMiles / this.feulInGallonsSinceNew;

else

return -1;

} catch (ArithmeticException e) {

System.out.println("Feul cannot be zero, If Odometer is not zero.");

return -1;

} catch (Exception e) {

return -1;

}

}

public void setBrandName(String brandName) {

this.brandName = brandName;

}

public void setOdometerInMiles(double odometerInMiles) {

this.odometerInMiles = odometerInMiles;

}

public void setFeulInGallonsSinceNew(double feulInGallonsSinceNew) {

this.feulInGallonsSinceNew = feulInGallonsSinceNew;

}

public static void main(String[] args) {

Car car1 = new Car("Honda", 54321.5, 9562);

Car car2 = new Car();

car2.setBrandName("Merz");

car2.setOdometerInMiles(345.2);

car2.setFeulInGallonsSinceNew(20);

System.out.println("Car 1 info : ");

car1.getinfo();

System.out.println("Car 2 info : ");

car2.getinfo();

System.out.println(

"Average MPG since new for car 1 : " + (car1.getAverageMPG() == -1 ? "error" : car1.getAverageMPG()));

System.out.println(

"Average MPG since new for car 2 : " + (car2.getAverageMPG() == -1 ? "error" : car2.getAverageMPG()));

}

}

Sample run

Car 1 info :
Brand Name of Car : Honda
Odometer Reading : 54321.5
Total Feul Consumed since new : 9562.0
Car 2 info :
Brand Name of Car : Merz
Odometer Reading : 345.2
Total Feul Consumed since new : 20.0
Average MPG since new for car 1 : 5.68097678309977
Average MPG since new for car 2 : 17.259999999999998