Monday, January 5, 2015

First Java Code

Here is the code from last night

To Start a new program go to the FILE menu, choose NEW/JAVA PROJECT. Give the project a name and click FINISH.

To add a new class. Right Click on the SRC folder in the project and choose NEW/CLASS. Name the Package and the class. If you want this class to have the main method click the check box to add static void main

package com.sponger.MileageCalculator;

import java.util.Scanner;

public class MileageCalc {
 @SuppressWarnings("unused")
 Scanner scan = new Scanner(System.in);
 public static void main(String[] args) {
 
  // Input: how many miles
  //Input:how many gallons
  //Output: MPG
  // MPG=Miles/Gallons
  
  MileageCalc m = new MileageCalc();
  m.calcuateMPG();

 }
 
 private double getTotalMiles(){
  System.out.println("Enter the total miles traveled");
  double miles = scan.nextDouble();
  return miles;
 }
 
 private double  getGallonsUsed(){
  System.out.println("Enter the gallons used.");
  double gallons = scan.nextDouble();
  return gallons;
 }
 
 private void calcuateMPG(){
  double mpg =getTotalMiles()/getGallonsUsed();
  displayMPG(mpg);
 }
 
 private void displayMPG(double mpg){
  System.out.println("You mpg is " + mpg);
 }

}

No comments:

Post a Comment