Description: Update \"YourCar\" and \"YourCarTrip\" such that you use a two-dime
ID: 3703256 • Letter: D
Question
Description: Update "YourCar" and "YourCarTrip" such that you use a two-dimensional array to store each leg. Your application should store all legs for the trip in multiple arrays, including the distance and the speed limit for each leg of the trip. The rest of the requirements are the same as previous homework. Outputs should include, but are not limited to: time to travel each leg, total time, total distance, fuel consumption and remaining fuel.
Original
Description: Update "YourCar" and "YourCarTrip" such that you use multiple arrays to store each leg. Your application should store all legs for the trip in multiple arrays, including the distance and the speed limit for each leg of the trip. The rest of the requirements are the same as previous homework. 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.
Explanation / Answer
import java.util.*;
//creating name of the class as 'Yourcar'
public class YourCar{
double gallons = 0;
double miles = 0;
double speed = 0;
}
public class YourCarTrip{
public static void main(String[] args)
{
double time1 = 0;
double time2 = 0;
YourCar car1= new YourCar();//creating object
YourCar car2= new YourCar();
Scanner s=new Scanner(System.in);//taking innput
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();
System.out.printf(" Enter the number of legs for car 1: ");
int legs=s.nextInt();
double[] mi=new double[legs];
double[] sp1=new double[legs];
double[] sp2=new double[legs];
for(int i=0;i<legs;i++)
{
System.out.printf(" leg "+(i+1)+": miles: ");
mi[i]=s.nextDouble();
System.out.printf("car1 speed: ");
sp1[i]=s.nextDouble();
System.out.printf("car2 speed: ");
sp2[i]=s.nextDouble();
car1.miles+=mi[i];
car2.miles+=mi[i];
car1.speed+=sp1[i];
car2.speed+=sp2[i];
time1+=mi[i]/sp1[i];
time2+=mi[i]/sp2[i];
}
int j=0;
System.out.println(" route 1: depart UHCL(point A), take a left to bay area boulevard for "+mi[j]+" miles at "+sp1[j++]+" MPH, take a left onto I45 South for "+mi[j]+" miles at "+sp1[j++]+" MPH, exit right onto 61st street, go "+mi[j]+" miles at "+sp1[j++]+" MPH to Stewart Drive, take a right and go "+mi[j]+" miles at "+sp1[j++]+" 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");//condition given according to the question for caar 1
if((car1.gallons-2 * (car1.miles/25))>=0)
{
System.out.println("u don't need gas");
}
else
System.out.println("u need gas");
j=0;
System.out.println(" route 1: depart UHCL(point A), take a left to bay area boulevard for "+mi[j]+" miles at "+sp2[j++]+" MPH, take a left onto I45 South for "+mi[j]+" miles at "+sp2[j++]+" MPH, exit right onto 61st street, go "+mi[j]+" miles at "+sp2[j++]+" MPH to Stewart Drive, take a right and go "+mi[j]+" miles at "+sp2[j++]+" MPH to your destination on the right (point B).");
System.out.println(" car 2: ");
System.out.println("total distance: "+ car2.miles+" miles");
System.out.println("total time: "+ time2+" hours");
System.out.println("total speed: "+ car2.speed+" MPH");
System.out.println("gas: "+ (car2.gallons-2 * (car2.miles/25))+" gallons");//condition given according to the question foor car2
if((car2.gallons-2 * (car2.miles/25))>=0)
{
System.out.println("u don't need gas");
}
else
System.out.println("u need gas");
}
}
//I have written the code in detail so that if doesnt required comments and also i have put the comments at require part of code
//And also i ve compiled and runned the code.pls comment if u have any problem
//Thank You :)