I need help with this Java program. JAVA JAVA JAVA JAVA JAVA Write a program to
ID: 3711219 • Letter: I
Question
I need help with this Java program.
JAVA JAVA JAVA JAVA JAVA
Write a program to create objects that represent automobiles.
Program Requirements
? with a main class named: AutoManager
NetBeans will create your main class file, AutoManager.java, containing the AutoManager class.
But this program will also need second class, used to define objects.
? Use File | New to create a new class named: Auto
NetBeans will create your Auto class file, Auto.java, containing the Auto class.
Within the Auto class (which will be used to create Auto type objects), define:
? Four private data fields (members) to hold:
o int modelYear - the year the auto was built
o String makeModel - the auto’s make and model, as one String
o double mileage - the auto’s mileage in miles per gallon
o double tankCapacity - the auto’s tank capacity in gallons
? A default constructor
o Constructor will initializes the integer fields to 0, double fields to 0.0
and the String to "unknown"
? Auto()
? Standard public mutator & accessor methods for each member (must use these exact names):
o A setter for each data field/member
? setModelYear()
? setMakeModel()
? setMileage()
? setTankCapacity()
o A getter for each data field/member
? getModelYear()
? getMakeModel()
? getMileage()
? getTankCapacity()
Within the AutoManager class, define:
? A readAutoData() method to read input from the user and set the values of all the data fields
in one object.
? The method will have two parameters:
o a Scanner object (to read data from the keyboard)
o an Auto object
? The method will prompt the user for the data for the automobile (year, make/model, mileage, and
tank capacity, in that order), and read the user input.
? It will then use the setters to set the value of data field/member of the Auto object with the user’s
input values.
HINT: Before prompting for the make/model, be sure to call nextLine() with the Scanner
variable, to clear the newline from the buffer, before reading the String.
Example:
Enter auto model year:
2010
Enter auto make and model:
Honda Accord
Enter auto mileage (in miles per gallon):
27.5
Enter auto tank capacity (in gallons):
13.9
Within the AutoManager class’ main method:
? Use the default constructor, to define and instantiate an object of the Auto class
? Create a Scanner object.
? Call the readAutoData() method to set the object’s data field values, using the Auto object and
the Scanner object its arguments.
? Display a blank line.
? Use the getters to display the values of each data field of the Auto object, formatted as follows
2010 Honda Accord has a 13.9 gallon tank and gets 27.5 miles per gallon
with the double values rounded to one decimal place.
Explanation / Answer
Auto.java
public class Auto {
// Declaring instance variables
private int modelYear;
private String makeModel;
private double mileage;
private double tankCapacity;
// Zero argumented constructor
public Auto() {
this.modelYear = 0;
this.makeModel = "unknown";
this.mileage = 0.0;
this.tankCapacity = 0.0;
}
// getters and setters
public int getModelYear() {
return modelYear;
}
public void setModelYear(int modelYear) {
this.modelYear = modelYear;
}
public String getMakeModel() {
return makeModel;
}
public void setMakeModel(String makeModel) {
this.makeModel = makeModel;
}
public double getMileage() {
return mileage;
}
public void setMileage(double mileage) {
this.mileage = mileage;
}
public double getTankCapacity() {
return tankCapacity;
}
public void setTankCapacity(double tankCapacity) {
this.tankCapacity = tankCapacity;
}
}
_________________
AutoManager.java
import java.util.Scanner;
public class AutoManager {
public static void main(String[] args) {
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Creating an instance of Auto class
Auto auto=new Auto();
//calling the method
readAutoData(sc,auto);
System.out.println();
//Displaying the output
System.out.printf("%d %s has a %.1f gallon tank and gets %.1f miles per gallon ",auto.getModelYear(),auto.getMakeModel(),auto.getTankCapacity(),auto.getMileage());
}
//This method will read the input entered by the user
private static void readAutoData(Scanner sc, Auto auto) {
System.out.print("Enter auto model year :");
int year=sc.nextInt();
sc.nextLine();
System.out.print("Enter auto make and model :");
String make=sc.nextLine();
System.out.print("Enter auto mileage (in miles per gallon) :");
double mileage=sc.nextDouble();
System.out.print("Enter auto tank capacity (in gallons) :");
double gallons=sc.nextDouble();
//calling the setters
auto.setModelYear(year);
auto.setMakeModel(make);
auto.setMileage(mileage);
auto.setTankCapacity(gallons);
}
}
___________________
Output:
Enter auto model year :2010
Enter auto make and model :Honda Accord
Enter auto mileage (in miles per gallon) :27.5
Enter auto tank capacity (in gallons) :13.9
2010 Honda Accord has a 13.9 gallon tank and gets 27.5 miles per gallon
_______________Thank You