Computer Science 202
Introduction to Programming

Fall 2012, The College of Saint Rose

MilesPerGallon BlueJ Project

Click here to download a BlueJ project for MilesPerGallon.


Download file: MilesPerGallon.vls


MilesPerGallon Source Code

The Java source code for MilesPerGallon is below. Click on a file name to download it.


MilesPerGallon.java


/**
 * Compute miles per gallon from input.
 * 
 * Example developed in class.  CSC 202.  Fall 2012.
 *
 * @author Jim Teresco
 * 9/11/12
 *
 * $Id$
 *
 */

import java.util.Scanner;

public class MilesPerGallon
{

    public static void main(String args[]) {
        
        Scanner numInput = new Scanner(System.in);
        
        // read miles
        System.out.print("How many miles did you drive? ");
        double miles = numInput.nextDouble();
        
        // read gallons
        System.out.print("How many gallons of gas did you use? ");
        double gallons = numInput.nextDouble();
        
        // compute mpg
        double mpg = miles / gallons;
        
        // report result
        System.out.println("Your MPG is " + mpg);
        
    }
}