Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class Car here.
- *
- * @author (Kelsey Manoogian)
- * @version (Fall 2011)
- */
- public class Car
- {
- private String makeModel;
- private int odometer;
- private int oilChangeMiles;
- private double gallonsInTank;
- private double mpg;
- private final double tank=12.5;
- private boolean engineOn;
- private boolean engineOff;
- public Car()
- {
- mpg=22.7;
- makeModel="Generic Car";
- gallonsInTank=12.5;
- odometer=0;
- oilChangeMiles=5000;
- }
- public Car (String pMakeModel, double pMpg)
- {
- makeModel=pMakeModel;
- mpg=pMpg;
- }
- public int checkOdometer ( )
- {
- return odometer;
- }
- public double checkGallonsInTank ( )
- {
- return gallonsInTank;
- }
- public void honkHorn ( )
- {
- System.out.println("Toot! Toot! Toot!");
- }
- public void addGas (double g)
- {
- gallonsInTank=gallonsInTank+g;
- System.out.println("The " + makeModel+" has "+gallonsInTank+" gallons of gas.");
- if(gallonsInTank>12.5)
- System.out.println(makeModel+" is overflowing with gas.");
- if(g<=0)
- System.out.println(makeModel+ " can not take in negative gas amounts.");
- }
- public void drive (int miles)
- {
- if(miles>=0)
- odometer=odometer+miles;
- else
- odometer=odometer;
- if(miles>=0)
- gallonsInTank=gallonsInTank-(miles/mpg);
- else
- gallonsInTank=gallonsInTank;
- if(miles<=0)
- System.out.println("Invalid entry, "+makeModel+" cannot go negative speed.");
- if(gallonsInTank==0)
- System.out.println(makeModel+" has run out of gas after driving "+odometer+" miles.");
- }
- public void changeOil()
- {
- oilChangeMiles=odometer+5000;
- System.out.println("The "+makeModel+" got an oil change and needs another at "+oilChangeMiles+" miles.");
- }
- public void checkOil()
- {
- System.out.println("The "+makeModel+"'s oil is OK");
- }
- public void startEngine()
- {
- engineOn=true;
- if(engineOn)
- System.out.println("The "+makeModel+" engine started.");
- }
- public void stopEngine()
- {
- engineOff=false;
- if(engineOff)
- System.out.println("The "+makeModel+" engine is off.");
- }
- public static void main (String[]args)
- {
- Car car1=new Car();
- Car car2=new Car("Ford Mustang",25.5);
- }
- }
Add Comment
Please, Sign In to add comment