Question & Answer: I've got the following code in Java and need to break out the file using classes, objects, and obj…..

I’ve got the following code in Java and need to break out the file using classes, objects, and object-oriented programming. I need to modify this class and create a main program to instantiate that class; the main program should call the methods of that class to do the work. Can someone help me fix this.

package pkg4.pkg17;

import java.util.Scanner;

public class GasMileage
{
public static void main(String[] args)
{
int miles = 1;
int gallons = 1;
Scanner input = new Scanner(System.in);
int tripCount = 1;
while(miles != -1 || gallons != -1)
{

System.out.println(“Enter the details for Trip #”+tripCount);
System.out.print(“Input miles travelled (or -1 to exit): “);
miles = Integer.parseInt(input.nextLine());
if(miles == -1)

{
System.out.println(“GoodBye!”);
System.exit(0);
}
else
{
if(miles < -1)
{
System.out.println(“Invalid input! Please try again.”);
continue;
}
}

System.out.print(“Input Gallons of gas consumed (or -1 to exit): “);
gallons = Integer.parseInt(input.nextLine());
if(miles != -1 || gallons != -1)
{

double mileage = (double) miles / gallons;
System.out.printf(“*** Mileage for Trip #%d is %.2f miles/gallon***nn”, tripCount, mileage);
tripCount++;
}
else
{
if(gallons < -1)
{
System.out.println(“Invalid input! Please try again.”);
continue;
}
}
}
}
}

Expert Answer

 

import java.util.Scanner;
public class GasMileage
{
int miles;
int gallons;
int tripCount;
GasMileage(int m,int g,int t)//contructor
{
miles =m;
gallons =g;
tripCount = t;
}
//object method
void ComputeMileage(int m,int g)
{
miles =m;
gallons =g;
double mileage = (double) miles / gallons;
System.out.printf(“*** Mileage for Trip #%d is %.2f miles/gallon***nn”, tripCount, mileage);
tripCount++;
}

public static void main(String[] args)
{
int miles = 1;
int gallons = 1;
Scanner input = new Scanner(System.in);
int tripCount = 1;

//Creating object…
GasMileage g = new GasMileage(miles,gallons,tripCount);

while(miles != -1 || gallons != -1)
{
System.out.println(“Enter the details for Trip #”+tripCount);
System.out.print(“Input miles travelled (or -1 to exit): “);
miles = Integer.parseInt(input.nextLine());
if(miles == -1)

{
System.out.println(“GoodBye!”);
System.exit(0);
}
else
{
if(miles < -1)
{
System.out.println(“Invalid input! Please try again.”);
continue;
}
}
System.out.print(“Input Gallons of gas consumed (or -1 to exit): “);
gallons = Integer.parseInt(input.nextLine());
if(miles != -1 || gallons != -1)
{
g.ComputeMileage(miles, gallons);//calling method from the object..
tripCount++;
}
else
{
if(gallons < -1)
{
System.out.println(“Invalid input! Please try again.”);
continue;
}
}
}
}
}

output:-

run:
Enter the details for Trip #1
Input miles travelled (or -1 to exit): 5
Input Gallons of gas consumed (or -1 to exit): 4
*** Mileage for Trip #1 is 1.25 miles/gallon***

Enter the details for Trip #2
Input miles travelled (or -1 to exit): 4
Input Gallons of gas consumed (or -1 to exit): 3
*** Mileage for Trip #2 is 1.33 miles/gallon***

Enter the details for Trip #3
Input miles travelled (or -1 to exit): -1
GoodBye!

Still stressed from student homework?
Get quality assistance from academic writers!