Miles per gallon with arrays: Declare arrays User prompt -> get the size of the
ID: 3710830 • Letter: M
Question
Miles per gallon with arrays:
Declare arrays
User prompt -> get the size of the arrays (how many weeks) and then create the arrays
User prompt -> get the values for the Miles array.
User prompt -> get the values for the Gallons array.
Calculate values for the MPG Array.
Display the three arrays as so:
package milespergallon;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author x
*/
public class MilesPerGallon {
private static int[] result;
public static int[] calculateMpg(int[] miles, int[] gallons) {
for (int i = 0; i < miles.length; i++) {
for (int j = 0; j < gallons.length; j++) {
int result;
result = miles[i] / gallons[j];
}
}
return result;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int[] miles;
int[] gallons;
int[] mpg;
int numOfWeeks;
System.out.println("How many weeks did you track? ");
numOfWeeks = stdin.nextInt();
miles = new int[numOfWeeks];
gallons = new int[numOfWeeks];
for (int i = 0; i < numOfWeeks; i++) {
System.out.println("How many miles did you drive in week " + (i + 1));
miles[i] = stdin.nextInt();
System.out.println("How many gallons did you use in week " + (i + 1));
gallons[i] = stdin.nextInt();
}
mpg = calculateMpg(miles, gallons);
System.out.println(" Miles Gallons MPG");
System.out.println(" ----- ------- ---");
System.out.println("Week 1: "
+ miles[0]
+ " " + gallons[0]
+ " " + mpg[0]);
System.out.println("Week 2: "
+ miles[1]
+ " " + gallons[1]
+ " " + mpg[1]);
}
}
Explanation / Answer
Answer:
MilesPerGallon.java:
package milespergallon;
import java.util.Scanner;
/**
*
* @author x
*/
public class MilesPerGallon {
private static int[] result;
public static int[] calculateMpg(int[] miles, int[] gallons) {
result = new int[miles.length]; // Assigning the length of result array to size of miles array / no of weeks
for (int i = 0; i < miles.length ; i++) {
result[i] = miles[i] / gallons[i]; // assigning the result to result array.
}
return result;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int[] miles;
int[] gallons;
int[] mpg;
int numOfWeeks;
System.out.println("How many weeks did you track? ");
numOfWeeks = stdin.nextInt();
miles = new int[numOfWeeks];
gallons = new int[numOfWeeks];
for (int i = 0; i < numOfWeeks; i++) {
System.out.println("How many miles did you drive in week " + (i + 1));
miles[i] = stdin.nextInt();
System.out.println("How many gallons did you use in week " + (i + 1));
gallons[i] = stdin.nextInt();
}
mpg = calculateMpg(miles, gallons);
System.out.println(" Miles Gallons MPG");
System.out.println(" ----- ------- ---");
for(int i =0; i< numOfWeeks; i++){ // added loop for dynamically printing the result
System.out.println("Week "+ (i+1) +": "
+ miles[i]
+ " " + gallons[i]
+ " " + mpg[i]);
}
}
}
Output:
How many weeks did you track?
3
How many miles did you drive in week 1
250
How many gallons did you use in week 1
15
How many miles did you drive in week 2
260
How many gallons did you use in week 2
17
How many miles did you drive in week 3
280
How many gallons did you use in week 3
20
Miles Gallons MPG
----- ------- ---
Week 1: 250 15 16
Week 2: 260 17 15
Week 3: 280 20 14