4
\$\begingroup\$

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.println("Total Miles Per Gallon for all trips is :"             +totalMilesPerGallon);               
          System.out.println("Average Miles Per Gallon Per Trip is :"  +AvgMPG);

                }
                else
                System.out.println("No data entered");



        }    
    }
\$\endgroup\$
  • 2
    \$\begingroup\$ We are happy to point out possible improvements in the code, but you must be reasonably sure that it works, we do not check if a code works or not. \$\endgroup\$ – Caridorc Dec 20 '15 at 11:24
  • \$\begingroup\$ The code works I've tested it. I just wanted to see if there were things that I missed. \$\endgroup\$ – user92749 Dec 20 '15 at 11:32
4
\$\begingroup\$

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 -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:

  • totalTrips should probably be just trips since it's just a counter.
  • totalMilesPerGallon is 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 as 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 {...} 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.

\$\endgroup\$
2
\$\begingroup\$

Bug for inefficient vehicles

Enter Miles Driven or -1 to quit:  
12
Enter Gallons used to fill tank or -1 to quit:  
31
Miles Per Gallon for this trip:  0

Well, I made less than one mile per gallon, but surely more than zero.

This conflicts with the spec:

All averaging calculations should use floating point results

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.