Question: You will write a similar program to the above but call it Assignment3b. Here, you will compute a cars gas mileage and the amount you
You will write a similar program to the above but call it Assignment3b. Here, you will compute a cars gas mileage and the amount you spend per mile traveled. In particular, ask the user for the following: their first name (or their full name) (String), the price per gallon of gasoline paid at the time of the last fill-up (double), the total amount paid for the fill-up (double) an initial odometer reading (int), a final odometer reading (int). After inputting these values, compute the total miles drive, the number of gallons of gas used (total amount for fill-up / price per gallon), and the miles per gallon that the car achieved (total miles / number of gallons). Note that number of gallons and miles per gallon will both be doubles. Remember, that due to rounding errors, most floating-point numbers end up being slightly imprecise. Therefore, use the technique shown in the lectures to address this situation and check with a calculator the results to see if they match. If they dont, there is problem either in your programs logic (way of calculating the values) or in precision. Make sure to correct it. Print this information to the console using System.out.println statements. Below is an example of the input and the output when running this program. Enter your name: Sergio Enter price per gallon: 1.99 Enter total price for fill up: 12.84 Enter total price for fill up: 12.84 Enter initial odometer value: 51384 Enter final odometer value: 51577 Sergio, you drove 193 miles using 6.452261306532663 gallons with an mpg of 29.911993769470406 Write this program making sure you comment the code as you write it. Remember that all assignments must include a series of comments (header comment to identify your program, body comments explaining your code logic, and output comments at the end showing your program output) in order to be accepted. You will receive a zero otherwise. When you have your program written, save and compile it. If it does not compile, fix your syntax errors. Once it does compile, run it on the above input to see if you get the same output. If you get incorrect output, look through your code and try to fix whatever errors you might have. At this moment, the output is not particularly readable. Let's force the output to look nicer in two ways. First, we will output the number of gallons used as an int. Do this by changing your numberOfGallons variable (whatever you called it) to an int by casting the value you compute for this variable to an int. Second, use the printf command to format the mpg output so it shows only two decimals. Check the class lectures and your textbook for details on how to do this. Part 3: Test Your Program and submit it Run your program 4 times using the following data for each run: Name Price per gallon Price for fill up Initial odometer Final odometer Your name 2.84 20.25 6144 6279 Frank 1.72 31.40 21975 22496 Ruth 3.75 22.87 89108 89183 Gail 2.05 20.50 65380 65691 Submission Instructions: Add all necessary comments including the header comment Paste the test console output after running the program FOR ALL 4 TEST CASES as a comment at the bottom of your source code in the driver/main class Correctly name your assignment file, classes, methods, and variables Submit your source code file (Assignment3b.java) to Canvas corresponding assignment folder (do not submit Assignment3a.java) Test all files to make sure they work as intended Check the Assignments Grading Rubric for details on how your program will be graded Common mistakes to avoid: 1. Submitting the wrong file (e.g. sending the compiled .class file instead of the source .java file) 2. Not adding the header identification comments 3. Not adding the output from the console for all test cases as a multi-line comment at the end of the code 4. Not adding any comments in the body of the code 5. Wrongly name variables 6. Forgetting to use the appropriate casts 7. Not using parenthesis in calculations (wrong operand order) 8. Unprecise calculation results 9. Forgetting to format numbers and output correctly using p
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
