Expand|Select|Wrap|Line Numbers
- /**
- * Class to determine Car's range of miles.
- *
- * @author Kelvynn Cayanan
- * @version 2/2/2014
- */
- public class Car
- {
- // instance variables -
- private double miles;
- private double gallons;
- private double gas;
- /**
- * Constructor for objects of class Car
- */
- public Car()
- {
- }
- public Car(double initialGas)
- {
- gas = initialGas;
- }
- public void addGas(double gas)
- {
- // Increases amount of gas in gas tank.
- gallons = gallons + gas;
- }
- public void drive(double drive)
- {
- // Decreases amount of gas in gas tank.
- double newdrive = (drive/miles) - gas;
- drive = newdrive;
- }
- public double range(double range)
- {
- //**calculates range, the number of miles the car can travel until the gas tank is empty */
- double newrange = miles * gas;
- range = newrange;
- return range;
- }
- }
- here is a class that i am supposed to implement with the class i made above.
- /**
- * Uses Cars.
- *
- * @author Anthony W. Smith
- * @version 6/15/2009
- */
- public class CarUser
- {
- /**
- * Constructor for objects of class CarUser
- */
- public CarUser()
- {
- Car honda = new Car(30.0); // 30 miles per gallon
- honda.addGas(9.0); // add 9 more gallons
- honda.drive(210.0); // drive 210 miles
- // print range remaining
- System.out.println("Honda range remaining: " + honda.range());
- Car toyota = new Car(26.0); // 26 miles per gallon
- toyota.addGas(4.5); // add 4.5 more gallons
- toyota.drive(150.0); // drive 150 miles
- // print range remaining
- System.out.println("Toyota range remaining: " + toyota.range());
- }
- }