I have a program that I need to convert the existing methods to get and set methods but I am struggling with how to do this. I have one set in place. Can anyone help in changing some of the others to get or set as appropriate? Any help is appreciated.
TJ
Here is the code:
import java.util.Scanner;
public class Odometer
{
int milesPerGallon;
int milesDriven;
int startTrip;
int endTrip;
int newOdometer;
int gals;
int galsUsed;
public int calculateMPG()
{
milesPerGallon = milesDriven/gals;
System.out.println("Gallons Used " + milesPerGallon);
return (milesPerGallon);
}
public void setOdometer()
{
newOdometer = milesDriven;
System.out.println("Total Miles on Odometer " + newOdometer);
milesDriven = 0;
System.out.println("New Trip miles " + milesDriven);
}
public int calculateMiles()
{
milesDriven = startTrip + endTrip;
System.out.println("Starting Trip " + startTrip);
System.out.println("Starting Trip " + endTrip);
System.out.println("Total Trip " + milesDriven);
return (milesDriven);
}
public void readMileage()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the starting number of miles on odometer");
System.out.println("Enter the ending number of miles on odometer");
startTrip = keyboard.nextInt();
endTrip = keyboard.nextInt();
}
public void readGallons()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter # per gallons");
gals = keyboard.nextInt();
}
}
TJ
Here is the code:
import java.util.Scanner;
public class Odometer
{
int milesPerGallon;
int milesDriven;
int startTrip;
int endTrip;
int newOdometer;
int gals;
int galsUsed;
public int calculateMPG()
{
milesPerGallon = milesDriven/gals;
System.out.println("Gallons Used " + milesPerGallon);
return (milesPerGallon);
}
public void setOdometer()
{
newOdometer = milesDriven;
System.out.println("Total Miles on Odometer " + newOdometer);
milesDriven = 0;
System.out.println("New Trip miles " + milesDriven);
}
public int calculateMiles()
{
milesDriven = startTrip + endTrip;
System.out.println("Starting Trip " + startTrip);
System.out.println("Starting Trip " + endTrip);
System.out.println("Total Trip " + milesDriven);
return (milesDriven);
}
public void readMileage()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the starting number of miles on odometer");
System.out.println("Enter the ending number of miles on odometer");
startTrip = keyboard.nextInt();
endTrip = keyboard.nextInt();
}
public void readGallons()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter # per gallons");
gals = keyboard.nextInt();
}
}