Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/** * Write a description of class Project9 here. * * @author () * @version (Nov.29,2017) */ import java.sql.Timestamp; import java.util.Scanner; import java.io.*; public class

/** * Write a description of class Project9 here. * * @author () * @version (Nov.29,2017) */ import java.sql.Timestamp; import java.util.Scanner; import java.io.*; public class project9 { public static void main (String [] args) { // Initialize variables and arrays Scanner myIn; Scanner stdIn = new Scanner (System.in); String inputFile = "p9inputfile.txt"; String name,dest,car; double miles,cpg,mpg,owc,twc; int [] item; // Read in destination and cost per gallon name = getString("Enter your name: "); dest = getString("Enter the destination: "); System.out.println(); System.out.println("You'll need to enter a positive number."); miles = getPosDoub("Enter miles to destination: "); System.out.println(); System.out.println("You'll need to enter a positive number."); cpg = getPosDoub("Enter cost per gallon: "); System.out.println();

// Read in values into car and MPG arrays (parallel arrays) try { // Open the input file declared as: inputFile System.out.println("Opening the input file for car data"); myIn = new Scanner (new FileInputStream("p9inputfile.txt")); Timestamp timestamp = new Timestamp(System.currentTimeMillis()); // Reading in the values into the arrays int i = 0; for (;myIn.hasNext();){ String line = myIn.nextLine(); String[] data = line.split(" "); car = myIn.next(); mpg = myIn.nextDouble(); display(car, mpg); } System.out.println("Closing the input file: "+timestamp); } catch (IOException e) { } // Display Header displayHeader(name,dest,miles,cpg); try { // Open the input file declared as: inputFile myIn = new Scanner (new FileInputStream("p9inputfile.txt")); // Reading in the values into the arrays int i = 0; for (;myIn.hasNext();) { String line = myIn.nextLine(); String[] data = line.split(" "); car = myIn.next(); mpg = myIn.nextDouble(); owc = (miles/mpg)* cpg; twc = (miles*2)/mpg*cpg; display(car, mpg, owc, twc); } } catch (IOException e) { } } public static void display(String n, double mpg) { System.out.println(" " + n + " " + mpg); }

public static void display(String car, double mpg, double owc, double twc) { System.out.println(" " + car + " " + mpg+ " " + owc+ " " + twc); } public static int getSmallest(int [] arr) { int indexSmallest = 0; int valueSmallest = 11; for(int i = 0; i < arr.length; i++){ if (arr[i] < valueSmallest){ valueSmallest = arr[i]; indexSmallest = i; } } return indexSmallest; } /** * getPosDoub will read in a positive double value. It will not * allow a negative number to be entered. * * @param str This is the description of what to enter * @return The double value entered by the user */ public static double getPosDoub(String str){ Scanner stdIn = new Scanner (System.in); System.out.print(str); double value = stdIn.nextDouble(); while (value < 0){ System.out.println("You must enter a postive number."); System.out.print("Enter a positive number: "); value = stdIn.nextDouble(); } return value; } /** * Get String will show the string entered by user * * * @param Display the description of what to enter * @return The string entered by the user */ public static String getString(String str){ Scanner stdIn = new Scanner (System.in); String response = ""; while (response.length()==0){ System.out.print(str); response = stdIn.nextLine(); if (response.length()== 0){ System.out.println("Error: Enter something please..."); } } return response; } /** * displayHeader will display chart of cars,mpg,cost,and round trip cost * * * @param Display car names,mpg,cost,and round trip cost * @return */ public static void displayHeader(String name,String dest,double miles, double cpg) { System.out.println(); System.out.println(); System.out.println("==========================================================================="); System.out.println(); System.out.print(" Comparison Chart Made For: " + name); System.out.println(); System.out.print(" Destination: " + dest); System.out.println(); System.out.print(" Miles to Destination: " + miles); System.out.println(); System.out.print(" Based on Cost per Gallon: " + cpg); System.out.println(); System.out.println(); System.out.println(" Car" +" "+"MPG"+" "+ " Cost one way" +" "+ "Cost round trip"); System.out.println("===========================================================================");

} }

Input file I am using:

Subaru_Outback 30

Explorer 20

Hummer 8

Ford_F150 15

Volkswagon_Beetle 30

Ford_Mustang 15

Toyota_Prius 46

Honda_Accord 33

Honda_Odyssey 22

Here is what the output should be:

Enter your name: Blah

Enter the destination: Walt Disney World

You'll need to enter a positive number.

Enter miles to destination: 1200.0

You'll need to enter a positive number.

Enter cost per gallon: 2.69

Opening the input file for car data

0: Subaru_Outback 30.0

1: Explorer 20.0

2: Hummer 8.0

3: Ford_F150 15.0

4: Volkswagon_Beetle 30.0

5: Ford_Mustang 15.0

6: Toyota_Prius 46.0

7: Honda_Accord 33.0

8: Honda_Odyssey 22.0

Closing the input file: 2017-11-28 15:44:12.027

=========================================================================

Comparison chart made for: Blah

Destination: Walt Disney World

Miles to Destination: 1200.0

Based on Cost per Gallon: 2.69

Car MPG Cost one way Cost round trip

=========================================================================

Subaru_Outback 30.0 107.60 215.20

Explorer 20.0 161.40 322.80

Hummer 8.0 403.50 807.00

Ford_F150 15.0 215.20 430.40

Volkswagon_Beetle 30.0 107.60 215.20

Ford_Mustang 15.0 215.20 430.40

Toyota_Prius 46.0 70.17 140.35

Honda_Accord 33.0 97.82 195.64

Honda_Odyssey 22.0 146.73 293.45

This is my program for programming class. I need help formatting the MPG, Cost One Way, and Cost Round Trip so that there are only two decimal places after. Also, when I open the input file I am using it does not display "Subaru_Outback" and there is no incrementer from 0-8 along side the cars name. In bold is what I have typed in.

Enter your name: Blah

Enter the destination: Walt Disney World

You'll need to enter a positive number. Enter miles to destination: 1200

You'll need to enter a positive number. Enter cost per gallon: 2.69

Opening the input file for car data Explorer 20.0 Hummer 8.0 Ford_F150 15.0 Volkswagon_Beetle 30.0 Ford_Mustang 15.0 Toyota_Prius 46.0 Honda_Accord 33.0 Honda_Odyssey 22.0 Closing the input file: 2017-12-03 23:02:03.787

===========================================================================

Comparison Chart Made For: Chad Fedie Destination: Walt Disney World Miles to Destination: 1200.0 Based on Cost per Gallon: 2.69

Car MPG Cost one way Cost round trip =========================================================================== Explorer 20.0 161.4 322.8 Hummer 8.0 403.5 807.0 Ford_F150 15.0 215.2 430.4 Volkswagon_Beetle 30.0 107.6 215.2 Ford_Mustang 15.0 215.2 430.4 Toyota_Prius 46.0 70.17391304347825 140.3478260869565 Honda_Accord 33.0 97.81818181818183 195.63636363636365 Honda_Odyssey 22.0 146.72727272727272 293.45454545454544

Thanks!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

Is each step numbered, if appropriate? (456)

Answered: 1 week ago

Question

=+1. Define net exports and net capital outflow. Explain

Answered: 1 week ago