Tuesday, February 11, 2014

gasApp

Programming Fundamentals
CS1336

Assignment #2 - Basic Calculations

Program #1

In that problem is described a car that can go 21.5 miles per gallon when driven in town and 26.8 miles per gallon when driven on the highway. The problem in the book then asks the programmer to calculate how far that car can go on one tank of gas under both conditions.

Let’s modify that problem in the following way. We’ll assume that the user of the program is going to take a trip, and that part of the trip will be on the highway and part of it will be in town. Have your program ask the user how far he has to go under each condition. Your program will then print out the total number of gallons required for the trip.

For example, let’s say the user plans to take a trip that will require 120 miles of in-town driving and 250 miles of highway driving (we’re not trying to be realistic :-)). Your program run might look like the following:

Please enter the number of in-town driving miles: 120
Please enter the number of highway driving miles: 250

The total number of gallons required is: 14.9 gal.


import java.util.Scanner;

public class GasApplicatoin {

 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);

  double townmiles, highwaymiles, total;

  System.out.print("Please enter the number of in-town driving miles: ");
  townmiles = input.nextInt();

  System.out.print("Please enter the number of highway driving miles: ");
  highwaymiles = input.nextInt();

  double tgallons = townmiles / 21.5;
  double hgallons = highwaymiles / 26.8;
  double total1 = tgallons + hgallons;

  System.out.println("The total number of gallons required is: " + total1);
 }

}

No comments:

Post a Comment