Thursday, January 15, 2015

The Vehicle Example

package com.spconger.VehicleExample;

public class Program {

 public static void main(String[] args) {
  Display d = new Display();
  
  
 }

}


package com.spconger.VehicleExample;

public class Vehicle {
 /*
  * Class can contain methods
  * Constants and variables (class level fields)
  * constructor
  */
 //private fields
 private int passengers;
 private int fuelCapacity;
 private int mpg;
 
 //public getter and setters
 //accessors and mutators
 public int getPassengers(){
  return passengers;
 }
 
 public void setPassengers(int passengers){
  this.passengers=passengers;
 }

 public int getFuelCapacity() {
  return fuelCapacity;
 }

 public void setFuelCapacity(int fuelCapacity) {
  this.fuelCapacity = fuelCapacity;
 }

 public int getMpg() {
  return mpg;
 }

 public void setMpg(int mpg) {
  this.mpg = mpg;
 }
 //public method
 public int calculateRange(){
  return getFuelCapacity() * getMpg();
 }
 
}


package com.spconger.VehicleExample;

import java.util.Scanner;



public class Display {
 
 private Vehicle vehicle;
 Scanner scan;
 
 public Display(){
  vehicle = new Vehicle();
  scan = new Scanner(System.in);
  getInputs();
  getOutput();
 }
 
 
 private void getInputs(){
  System.out.println("Enter the seating capacity of the vehicle");
  vehicle.setPassengers(scan.nextInt());
  System.out.println("Enter the Fuel capacity");
  vehicle.setFuelCapacity(scan.nextInt());
  System.out.println("Enter the mpg");
  vehicle.setMpg(scan.nextInt());
 }
 
 private void getOutput(){
  System.out.println("Your vehicle can hold " 
 + vehicle.getPassengers() + 
 " Passenger and had a range of "
 + vehicle.calculateRange());
 }

}

No comments:

Post a Comment