I need to update the classes to use a 2D array instead of ho
I need to update the classes to use a 2D array, instead of how the program currently works.
Using the following code update the \"YourCar\" and \"YourCarTrip\" classes so that you can use a 2D array to store the road segment information and read the road segment information from the 2D array.
Here is the code that was provided:
import java.util.Scanner;
class YourCar
{
//Variables
double gallons = 0; // this is gas, and the program does determine if you need fuel or not
double miles = 0;
double speed = 0;
String direction;
}
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();
YourCar dir = new YourCar();
Scanner input = new Scanner(System.in);
//Asks user for the number current of gallons in the car(s) they are using
System.out.printf(\"\ Enter the number of gallons for the first car: \"); //if the gallons are larger than 20 then the program will say you don\'t need gas
car1.gallons = input.nextDouble();
System.out.printf(\"\ Enter the number of gallons for the Second car: \");
car2.gallons = input.nextDouble();
System.out.printf(\"\ Enter the number of places that you plan on traveling to in your car: \"); //if its bigger than 10 the program will loop for a while.
int length = input.nextInt();
double[] mi = new double[length];
double[] sp1 = new double[length];
double[] sp2 = new double[length];
for(int i = 0; i < length; i++)
{
System.out.printf(\"\ How far do you want to travel?\ (\"+(i + 1)+\") miles: \");
mi[i] = input.nextDouble();
System.out.print(\"What Direction do you want to go?: \");
dir.direction = input.next();
System.out.printf(\"The Speed of the first car is: \");
sp1[i] = input.nextDouble();
System.out.printf(\"The Speed of the second car is: \");
sp2[i] = input.nextDouble(); //sp = speed or a abbrevation for speed
car1.miles += mi[i];
car2.miles += mi[i];
car1.speed += sp1[i]; //array calculation
car2.speed += sp2[i];
time1 += mi[i] / sp1[i];
time2 += mi[i] / sp2[i];
}
int j=0;
// outputs data for car 1
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\");
System.out.println(\"Direction: \" + dir.direction);
//Determining if you need gas or not for car 1
if((car1.gallons - 2 * (car1.miles / 25)) >= 0)
{
System.out.println(\"You don\'t need gas. \ \");
}
else
System.out.println(\"You do need gas! \ \");
j=0;
// outputs data for car 2
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\");
System.out.println(\"Direction: \" + dir.direction);
//Determining if you need gas or not for car 2
if((car2.gallons-2 * (car2.miles / 25)) >= 0)
{
System.out.println(\"You don\'t need gas\ \");
}
else
System.out.println(\"You do need gas\ \");
}
}
Solution
I could use 2D array only for speed purpose. Please let me know if you want to use for other purpose too. I couldn\'t refactore more than that.
import java.util.Scanner;
class YourCar {
// Variables
double gallons = 0; // this is gas, and the program does determine if you need fuel or not
double miles = 0;
double speed = 0;
String direction;
}
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 input = new Scanner(System.in);
// Asks user for the number current of gallons in the car(s) they are using
System.out.printf(\"\ Enter the number of gallons for the first car: \");
// if the gallons are larger than 20 then the program will say you don\'t need gas
car1.gallons = input.nextDouble();
System.out.printf(\"\ Enter the number of gallons for the Second car: \");
car2.gallons = input.nextDouble();
System.out.printf(\"\ Enter the number of places that you plan on traveling to in your car: \");
// if its bigger than 10 the program will loop for a while.
int length = input.nextInt();
double[] mi = new double[length];
double[][] sp = new double[2][length]; //Using 2D array for speed of car1 and car2.
for (int i = 0; i < length; i++) {
System.out.printf(\"\ How far do you want to travel?\ (\" + (i + 1) + \") miles: \");
mi[i] = input.nextDouble();
System.out.print(\"What Direction do you want to go?: \");
String direction = input.next();
car1.direction = direction;
car2.direction = direction;
System.out.printf(\"The Speed of the first car is: \");
sp[0][i] = input.nextDouble();
System.out.printf(\"The Speed of the second car is: \");
sp[1][i] = input.nextDouble(); // sp = speed or a abbrevation for speed
car1.miles += mi[i];
car2.miles += mi[i];
car1.speed += sp[0][i]; // array calculation
car2.speed += sp[1][i];
time1 += mi[i] / sp[0][i];
time2 += mi[i] / sp[1][i];
}
int j = 0;
// outputs data for car 1
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\");
System.out.println(\"Direction: \" + car1.direction);
// Determining if you need gas or not for car 1
if ((car1.gallons - 2 * (car1.miles / 25)) >= 0) {
System.out.println(\"You don\'t need gas. \ \");
} else
System.out.println(\"You do need gas! \ \");
j = 0;
// outputs data for car 2
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\");
System.out.println(\"Direction: \" + car2.direction);
// Determining if you need gas or not for car 2
if ((car2.gallons - 2 * (car2.miles / 25)) >= 0) {
System.out.println(\"You don\'t need gas\ \");
} else
System.out.println(\"You do need gas\ \");
input.close();
}
}



