patternjavaMinor
Calculate total miles per gallon and average per trip
Viewed 0 times
totalpermilesaveragegalloncalculatetripand
Problem
Assignment: Develop a Java application that will input the number of miles driven and gallons used (both as integers) for each trip. The program should calculate and display the miles per gallon obtained for each trip and print the combined miles per gallon obtained for all trips up to this point. All averaging calculations should use floating point results.
See my implementation below.
YES the class name is weird but I didn't feel like changing it.
Does the code satisfy the assignment requirements?
Have I missed the target completely, and if so how far off was I?
```
import java.util.Scanner;
public class MagicNumber
{
public static void main(String[] args)
{
float AvgMPG = 0;
int MilesDriven = 0;
int totalTrips = 0;
int GallonsUsed = 0;
int totalMilesPerGallon = 0;
int MilesPerGallon = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter Miles Driven or -1 to quit: ");
MilesDriven = input.nextInt();
System.out.println("Enter Gallons used to fill tank or -1 to quit: ");
GallonsUsed = input.nextInt();
while ( MilesDriven != -1)
{
MilesPerGallon = MilesDriven / GallonsUsed;
System.out.println("Miles Per Gallon for this trip: " +MilesPerGallon);
totalMilesPerGallon = MilesPerGallon + totalMilesPerGallon;
totalTrips = totalTrips + 1;
System.out.println("Enter Miles Driven or -1 to quit: ");
MilesDriven = input.nextInt();
System.out.println("Enter Gallons used to fill tank or -1 to quit: ");
GallonsUsed = input.nextInt();
}
if (totalTrips != 0)
{
System.out.println("Number of trips taken: "+ totalTrips);
AvgMPG = (float) totalMilesPerGallon / totalTrips;
System.out.p
See my implementation below.
YES the class name is weird but I didn't feel like changing it.
Does the code satisfy the assignment requirements?
Have I missed the target completely, and if so how far off was I?
```
import java.util.Scanner;
public class MagicNumber
{
public static void main(String[] args)
{
float AvgMPG = 0;
int MilesDriven = 0;
int totalTrips = 0;
int GallonsUsed = 0;
int totalMilesPerGallon = 0;
int MilesPerGallon = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter Miles Driven or -1 to quit: ");
MilesDriven = input.nextInt();
System.out.println("Enter Gallons used to fill tank or -1 to quit: ");
GallonsUsed = input.nextInt();
while ( MilesDriven != -1)
{
MilesPerGallon = MilesDriven / GallonsUsed;
System.out.println("Miles Per Gallon for this trip: " +MilesPerGallon);
totalMilesPerGallon = MilesPerGallon + totalMilesPerGallon;
totalTrips = totalTrips + 1;
System.out.println("Enter Miles Driven or -1 to quit: ");
MilesDriven = input.nextInt();
System.out.println("Enter Gallons used to fill tank or -1 to quit: ");
GallonsUsed = input.nextInt();
}
if (totalTrips != 0)
{
System.out.println("Number of trips taken: "+ totalTrips);
AvgMPG = (float) totalMilesPerGallon / totalTrips;
System.out.p
Solution
One thing you need to do is methodically test your code. So far you have only tested your code to 'work', but the point of testing is to find what doesn't work (as intended).
It's something you can also hand in and it's a good practice to get into - you will need it if you program professionally. As your code grows you might need to look at splitting the user interface of your code from your business logic so that you can test them separately using a test program.
For small pieces of code a test plan is 'good enough' for your needs.
Logic testing - look at your looping and branching conditions to work out which inputs should bring them to each branch. For example your UI says that inputting
Validation testing - Could you get invalid inputs that make your code do something you didn't expect? For example is there an opportunity for
Computational testing - Do you get an appropriate level of accuracy when you are mixing data types? (another answer has pointed out a bug in that department.) Are the calculation results what you expect if you work them out manually? Do you get the correct output (for example at each loop the spec requests the average so far.)
Since your program is quite simple it would be very easy to test well. But each time you find a bug then don't consider it an isolated problem - when you have 1 bug it usually means you have found a class of bugs and you should check if that bug crops up elsewhere, it frequently does.
Looking at your variables in this code:
This class could use a more meaningful name than
well, choosing meaningful names is a clear indicator that you have
fully understood the problem or need your code is trying to address.
It's also a handy 'red-flag' that if you can't think up a name easily
that you may need to check that you understand what you want your code to do more
clearly before you write any (more).
Looking at code reuse, do you really need all your inputs listed twice? Is there another way it could be coded to avoid the repetition of the inputs?
Finally, you might want to double-check your formatting, your braces
All that should give you a good handle on what you're missing and how your code can improve for now.
It's something you can also hand in and it's a good practice to get into - you will need it if you program professionally. As your code grows you might need to look at splitting the user interface of your code from your business logic so that you can test them separately using a test program.
For small pieces of code a test plan is 'good enough' for your needs.
Logic testing - look at your looping and branching conditions to work out which inputs should bring them to each branch. For example your UI says that inputting
-1 gallons used will exit - does it? Could your program be given zero trips, how should it handle that?Validation testing - Could you get invalid inputs that make your code do something you didn't expect? For example is there an opportunity for
division by zero given bad input? If user input could generate an exception then it should be mitigated.Computational testing - Do you get an appropriate level of accuracy when you are mixing data types? (another answer has pointed out a bug in that department.) Are the calculation results what you expect if you work them out manually? Do you get the correct output (for example at each loop the spec requests the average so far.)
Since your program is quite simple it would be very easy to test well. But each time you find a bug then don't consider it an isolated problem - when you have 1 bug it usually means you have found a class of bugs and you should check if that bug crops up elsewhere, it frequently does.
Looking at your variables in this code:
totalTripsshould probably be justtripssince it's just a counter.
totalMilesPerGallonis just added to but you are adding an average. Either your naming is wrong or your logic is.
This class could use a more meaningful name than
MagicNumber aswell, choosing meaningful names is a clear indicator that you have
fully understood the problem or need your code is trying to address.
It's also a handy 'red-flag' that if you can't think up a name easily
that you may need to check that you understand what you want your code to do more
clearly before you write any (more).
Looking at code reuse, do you really need all your inputs listed twice? Is there another way it could be coded to avoid the repetition of the inputs?
Finally, you might want to double-check your formatting, your braces
{...} should match up if that's the convention you're following. Most IDE's will do this for you.All that should give you a good handle on what you're missing and how your code can improve for now.
Context
StackExchange Code Review Q#114531, answer score: 4
Revisions (0)
No revisions yet.