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

Update \"YourCar\" and \"YourCarTrip\" such that you use a “for” loop to enter e

ID: 3678715 • Letter: U

Question

Update "YourCar" and "YourCarTrip" such that you use a “for” loop to enter each leg. Your application should ask the user for the number of legs for the trip, then subsequently ask for the distance and the speed limit for each leg of the trip. The input values for speed and distance will help you calculate the time required for each leg of the trip and total distance and total time for the trip. Additionally, make sure you have enough gas to make the return trip by calculating the amount of fuel needed for the return trip (please assume the same route in return). The programmer must use a 'for' loop to implement this application. Outputs should include, but are not limited to: time to travel each leg, total time, total distance, fuel consumption and remaining fuel.

Example: Depart UHCL (point A), take Bay Area Boulevard for 3.5 miles at 30 MPH, take I45 South for 26.3 miles at 65 MPH, exit right onto 61st street and go 2.0 miles at 30 MPH, then take Stewart Drive 4.3 miles at 45 MPH to your destination (point B). You application should ask the user for the number of legs (this case four (4)) then loop through four sets of questions to gather the necessary inputs for distance and speed.

YourCar should define, at least, the instance variables that hold the number of gallons in the tank (must be input at some point), the number of miles in the trip (starts at zero), current speed (starts at zero). YourCar should have the ability (methods) to change speed (increase, decrease), calculate time, calculate distance traveled, and determine if you need gas. Needs gas, implies you will need to calculate your fuel consumption, so assume your car averages 25 MPG at any speed. You may have to ask the user for amount of fuel (gallons) at the beginning of the trip. Constraint: Your gas tank cannot hold over 10 gallons.

**Code in Java**

What I have so far:

import java.util.*;

class YourCar

{
   //Variables
   double gallons = 0;
   double miles = 0;
   double speed = 0;

}


public class YourCarTrip

{

   public static void main(String[] args)

   {

       //Variables
       double time1 = 0;
       double time2 = 0;

       //Car Objects
       YourCar car1= new YourCar();
       YourCar car2= new YourCar();

   Scanner s=new Scanner(System.in);

   //Prompt user for the number of gallons in their car
   System.out.printf(" Enter the number of gallons in the tank for car 1: ");
   car1.gallons=s.nextDouble();
  
   System.out.printf(" Enter the number of gallons in the tank for car 2: ");
   car2.gallons=s.nextDouble();


   car1.miles=(3.5+26.3+2.0+4.3);
   time1=(3.5/30) + (26.3/65) + (6.3/45);
   time2=(3.5/40) + (26.3/75) + (6.3/55);
   car1.speed=(30+65+45)/3;
   car2.speed=(40+75+55)/3;

   System.out.println(" Route 1: Depart UHCL (point A), take a left on to Bay Area Boulevard for 3.5 miles at 30 MPH, take a left onto I45 South for 26.3 miles at 65 MPH, exit right onto 61st street, go 2.0 miles to Stewart Drive, take a right and go 4.3 miles at 45 MPH to your destination on the right (point B).");

   System.out.println(" Car 1: ");
   System.out.println("Total distance: "+ car1.miles+" miles");
   System.out.println("Total time: "+ time1+" hours");
   System.out.println("Total speed: "+ car1.speed+" MPH");
   System.out.println("Gas: "+ (car1.gallons-2 * (car1.miles/25))+" gallons");

   //If else statement for gas
   if((car1.gallons-2 * (car1.miles/25))>=0)
   {
       System.out.println("You don't need gas ");
   }
   else
       System.out.println("You do need gas ");

   System.out.println(" Route 2: Depart UHCL (point A), take a left on to Bay Area Boulevard for 3.5 miles at 40 MPH, take a left onto I45 South for 26.3 miles at 75 MPH, exit right onto 61st street, go 2.0 miles to Stewart Drive, take a right and go 4.3 miles at 55 MPH to your destination on the right (point B).");
  
   System.out.println(" Car 2: ");
   System.out.println("Total distance: "+ car1.miles+" miles");
   System.out.println("Total time: "+ time2+" hours");
   System.out.println("Total speed: "+ car2.speed+" MPH");
   System.out.println("Gas: "+ (car2.gallons-2 * (car1.miles/25))+" gallons");

   if((car2.gallons-2 * (car1.miles/25))>=0)
   {
       System.out.println("You don't need gas ");
   }
   else
       System.out.println("You do need gas ");
  
   }

}   

Explanation / Answer

import java.io.*;

import java.util.*;

public class YourCar() {

double gallons = 0;

double miles = 0;

double speed = 0; }

public class YourCarTrip() {

public static void main(String args[]) throws Exception {

double time1 = 0,time2 = 0;

String roadfree;

YourCar c= new YourCar();

YourCar c1 = new YourCar();

BufferedReader br = new BufferedReader( new InputStreamReader(System.in));

System.out.println(“Enter the fuel for tank in car1:”);

c.gallons = br.readLine();

System.out.println(“Enter the fuel for tank in car2:”);

c1.gallons = br.readLine();

c.miles = (3.5+26.3+2.0+4.3);

time1 = (3.5/30)+(26.3/65)+(6.3/45);

time2 =(3.5/40)+(26.3/75)+(6.3/55);

c.speed = (30+65+45)/3;

c1.speed = (40+75+55)/3;

System.out.println(“Enter Road free: Y/N”);

roadfree = br.readLine();

if(roadfree ==”YES”) {

System.ou.println(“Increase car speed”);

c.speed = br.readLine();


}else

{ System.out.println(“Decrease car speed”);

c.speed = br.readLine(); }

System.out.println(" Route 1: Depart UHCL (point A), take a left on to Bay Area Boulevard for 3.5 miles at 30 MPH, take a left onto I45 South for 26.3 miles at 65 MPH, exit right onto 61st street, go 2.0 miles to Stewart Drive, take a right and go 4.3 miles at 45 MPH to your destination on the right (point B).");

System.out.println(" Car 1: ");

System.out.println("Total distance: "+ c.miles+" miles");

   System.out.println("Total time: "+ time1+" hours");

   System.out.println("Total speed: "+ c.speed+" MPH");

   System.out.println("Gas: "+ (c.gallons-2 * (c.miles/25))+" gallons");

if((c.gallons-2 * (c.miles/25))>=0)

   {

       System.out.println("You don't need gas ");

   }

   else

       System.out.println("You do need gas ");

if(roadfree ==”YES”) {

System.ou.println(“Increase car speed”);

c1.speed = br.readLine();


}else

{ System.out.println(“Decrease car speed”);

c1.speed = br.readLine(); }

   System.out.println(" Route 2: Depart UHCL (point A), take a left on to Bay Area Boulevard for 3.5 miles at 40 MPH, take a left onto I45 South for 26.3 miles at 75 MPH, exit right onto 61st street, go 2.0 miles to Stewart Drive, take a right and go 4.3 miles at 55 MPH to your destination on the right (point B).");

   System.out.println(" Car 2: ");

   System.out.println("Total distance: "+ c.miles+" miles");

   System.out.println("Total time: "+ time2+" hours");

   System.out.println("Total speed: "+ c1.speed+" MPH");

   System.out.println("Gas: "+ (c1.gallons-2 * (c.miles/25))+" gallons");

   if((c1.gallons-2 * (c.miles/25))>=0)

   {

       System.out.println("You don't need gas ");

   }

   else

       System.out.println("You do need gas ");

   }   }  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote