Question: Write a program in Java to compute the following properties of automobiles: average gas mileage, lowest price, the latest model. The program must have the

Write a program in Java to compute the following properties of automobiles: average gas mileage,
lowest price, the latest model. The program must have the following:
A class named Automobile (library).
One constructor function that initializes the model year only.
A method to set the gas mileage and price for the object variables.
Three separate methods to compute the results (average gas mileage, lowest price, latest
model) and show the outputs.
The Main function will stay in another class (test or main class) from where the
constructors and all other methods will be invoked through three objects named: sedan,
minivan, and truck.
Inputs are to be taken inside the Main function from the keyboard. The program will show
the outputs on the screen.
Sample Input (from keyboard):
Enter the model year for sedan: 2012
Enter the gas mileage for sedan: 35
Enter the price for sedan: 20000
Enter the model year for minivan: 2013
Enter the gas mileage for minivan: 26
Enter the price for minivan: 18000
Enter the model year for truck: 2010
Enter the gas mileage for truck: 21
Enter the price for truck: 17000
Sample Output (on screen):
Avg gas mileage: 27.33
Lowest price: 17000
Latest model: 2013 i am not allowed to use inheritance in this code and i have written a little bit of it as well package Objects;
class Automobile
{
private int model;
private double gasMileage;
private double lowestPrice;
public Automobile(int newModel)
{
model = newModel;
}
public void setgasMileage(double newGasMileage)
{
gasMileage = newGasMileage;
}
public void setprice(double price)
{
lowestPrice = price;
}
public double getGasMileage(){
return gasMileage;
}
public double getLowestPrice(){
return lowestPrice;
}
public int getLatestModel(){
return model ;
}
}package Objects;
import java.util.Scanner;
public class TestAutomobile
{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the model year for sedan: ");
int sedanYear = input.nextInt();
System.out.print("Enter the gas mileage for sedan: ");
double sedanMileage = input.nextDouble();
System.out.print("Enter the price for sedan: ");
double sedanPrice = input.nextDouble();
System.out.print("Enter the model year for minivan: ");
int minivanYear = input.nextInt();
System.out.print("Enter the gas mileage for minivan: ");
double minivanMileage = input.nextDouble();
System.out.print("Enter the price for minivan: ");
double minivanPrice = input.nextDouble();
System.out.print("Enter the model year for truck: ");
int truckYear = input.nextInt();
System.out.print("Enter the gas mileage for truck: ");
double truckMileage = input.nextDouble();
System.out.print("Enter the price for truck: ");
double truckPrice = input.nextDouble();
Automobile sedan = new Automobile(sedanYear);
sedan.setgasMileage(sedanMileage);
sedan.setprice(sedanPrice);
Automobile minivan = new Automobile(minivanYear);
minivan.setgasMileage(minivanMileage);
minivan.setprice(minivanPrice);
Automobile truck = new Automobile(truckYear);
truck.setgasMileage(truckMileage);
truck.setprice(truckPrice);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!