Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I am having issues with this code. I am not getting the output I would like. The output I am supposed to get is

Hello, I am having issues with this code. I am not getting the output I would like. The output I am supposed to get is

Please enter the details for each trip. Enter 1 miles travelled (-1 to quit): 15 Enter 1 fuel used: 6 Enter 2 miles travelled (-1 to quit): 25 Enter 2 fuel used: 13 Enter 3 miles travelled (-1 to quit) : 35 Enter 3 fuel used: 25 Enter 4 miles travelled (-1 to quit): -1 Total mileage is 75 miles. Total fuel used is 44 gallons. The total miles per gallon is 1.0 miles per gallon.

But I am getting

Please enter the details for each trip when asked. Enter 1 miles travelled (-1 to quit): 4 Enter 1 fuel used: 265 Enter 2 miles travelled (-1 to quit): 2 Enter 2 fuel used: 24 Enter 3 miles travelled (-1 to quit): 2 Enter 3 fuel used: 24 Enter 4 miles travelled (-1 to quit): 4 Enter 4 fuel used: 25 Enter 5 miles travelled (-1 to quit): 6 Enter 5 fuel used: 3 Enter 6 miles travelled (-1 to quit): 6 Enter 6 fuel used:

My code looks like this.

package mileage; import java.util.Scanner;

public class GasMileage {

// Declaration of instance variables private int miles; private int gallons; //Initialize the scanner Scanner input = new Scanner(System.in); // constructor initializes with no arguments // constructor name is class name public GasMileage(){ } // constructor initializes Employee with arguments public GasMileage (int miles, int gallons) { // initialize the variables this.miles = miles; this. gallons = gallons; } // end constructor

public int getMiles() { return miles; } public void setMiles (int miles) { this.miles = miles; } public int getGallons() { return gallons; } public void setGallons (int gallons) { this.gallons = gallons; } public void displayMessage(){ // Display welcome message and instructions System.out.println("Please enter the details for each trip when asked."); } public void calcMileage(){ // create Scanner to obtain input from command window Scanner input = new Scanner(System.in); int tripCounter = 1; int value = 0; int miles = 0; int fuel = 0;

// ask for mileage System.out.printf("Enter " + tripCounter + " miles travelled (-1 to quit): "); value = input.nextInt(); if(value == -1){ System.out.printf("You have exited the program."); } setMiles (value); // while loop while (getMiles() != -1){ // ask for mileage System.out.printf("Enter " + tripCounter + " fuel used: "); setGallons (input.nextInt()); miles = miles + getMiles(); fuel = fuel + getGallons(); // increment the counter tripCounter++; // ask for mileage System.out.printf("Enter " + tripCounter + " miles travelled (-1 to quit): "); value = input.nextInt(); setMiles (value); } if((miles > 0) && (fuel > 0)){ float milesPerGallon = miles / fuel; System.out.println("Total mileage is " + miles + " miles."); System.out.println("Total fuel used is " + fuel + " gallons."); System.out.println("The total miles per gallon is" + milesPerGallon + " miles per gallon."); input.close(); } } }

package mileage; import java.util.Scanner;

public class MileageTest {

public static void main(String[] args) { // create Scanner to obtain input from command window Scanner input = new Scanner (System.in); // create two Employee object GasMileage mileage = new GasMileage (); // Display a welcome message mileage.displayMessage(); //calculate total total mileage, fuel used and miles per gallon for each trip mileage.calcMileage(); // close the Scanner input.close();

}

} Thank you very much

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

How does selection differ from recruitment ?

Answered: 1 week ago

Question

Briefly describe computer-aided approaches to production.

Answered: 1 week ago

Question

How can a layout help or hinder productivity?

Answered: 1 week ago