Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

home / study / questions and answers / engineering / computer science / part 1:

ID: 672488 • Letter: H

Question

home / study / questions and answers / engineering / computer science / part 1: the lab6_methods.java file contains instructions...

Question

Part 1: The Lab6_Methods.java file contains instructions for completing the lab. Specifically, you will create a program that can be used by a car dealer. In addition to the main method, the program will contain the following two methods: 1. Given the number of gallons of gas in the tank (entered in by the user) and a Miles per Gallon (MPG) rating for the vehicle, return how far the car can go before it runs out of gas. 2. Displays the Make, Model, Year and MPG (with only one decimal) of the car. Only display how far the car can go if the tank is NOT empty. Within the main method, add the following: 1. Hardcode the make, model and year of YOUR car (for testing purposes only) 2. Call your calculation method (Method #1). 3. Call your display method (Method #2).

Part 2: Add a method called getInput that reads in the gallons of gas from the user as a double, validates that the value is between 0-25 (inclusive), and returns that value back to main once it is valid. Your method will NOT return a value until it has been validated to be between 0-25 (inclusive). Comment the method as shown in the other two methods given. -Call this method from main.

please used the this codes and answer my question please someone save my life ..

Explanation / Answer

Program

import java.util.Scanner;
public class Lab6_Methods {
  
   public static void main(String[ ] args) {
  
   Scanner sca=new Scanner(System.in);
double milesTraveled = 0; // miles until empty - tank initially empty
double gallonsOfGas = 0; // amount of fuel left in tank - initially empty
final double MPG = 25; // Miles per Gallon - Constant
  
// Hardcode values to make, model, and year of YOUR car
String make="Audi";
String model="Q1 FaceLift";
int year=2014;

// -------------------- Get User-entered Input ------------------
System.out.print("How many gallons do you have left in the tank?" +
" <enter 0 if empty>: ");
gallonsOfGas=getInt(sca);
//gallonsOfGas = sca.nextDouble();
  
// -------------------- Calculations ------------------
// ***Your code goes here***
// If zero wasn't entered above, call the method to calculate how far you
// can go on the current amount of gas in the tank
if(gallonsOfGas!=0)
   milesTraveled=howFar(gallonsOfGas,MPG);
  
  

// ----------------------- Display Results ------------------------------
// ***Your code goes here***
// Call the method to display the following:
// Car Info (make, model, year), the MPG,
// and miles you can go before empty (ONLY if there is gas in the tank!)
Display(make,model,year,milesTraveled,MPG);
  
} // end main method

/**
* Method that displays info about the car (Make, Model, Year), MPG
* and, if the tank is not empty, how far the car can travel on the current
* amount of fuel in the tank (only displayed if there is gas in the tank!)
*
* @param year - year of the car
* @param make - make of the car
* @param model- model of the car
* @param mpg - Miles per Gallon rating of the car
* @param miles- Miles car can go with current fuel in the tank
* @return None
*/
// ***Code for your method goes here***
   public static void Display(String make, String model, int year, double mile, double mpg)
   {
       if(mile>0)
       {
           System.out.println("Year - "+year);
           System.out.println("Make - "+make);
           System.out.println("Model - "+model);
           System.out.println("MPG - "+mpg);
           System.out.format("Miles - %.1f", mile);
       }
   }
/**
* Method returns how far (in miles) a car object can go until
* the gas tank is empty
*
* @param mpg - Miles per Gallon rating of the car
* @param fuel - amount of fuel currently in the tank
* @return Miles car can travel on current fuel amount
*/
// ***Code for your method goes here***
   public static double howFar(double gallons,double MPG)
{
   return gallons*MPG;
}
   public static double getInt(Scanner sca)
   {
       double g;
       while(true)
       {  
           g=sca.nextDouble();
           if(g>=0 && g<=25)
               break;
           System.out.println("plz enter value between 0-25");
       }
       return g;
              
          
          
   }

} // end class

Reault:

How many gallons do you have left in the tank? <enter 0 if empty>: 39
plz enter value between 0-25
23
Year - 2014
Make - Audi
Model - Q1 FaceLift
MPG - 25.0
Miles - 575.0