//  Scott DeRuiter      9/24/2002
//  Car.java
//  A class that gives some important info about a car, including miles per gallon,
//  and the estimated value.

public class Car    
{

	private String make, model;
	private int year;
	private double odometernew, odometerold;
	private double tankcapacity;
	private double newcarvalue;

	public Car ( )   
	{
		make = "Ford";
		model = "Edsel";
		year = 1952;
		odometernew = odometerold = 0.0;
		tankcapacity = 16.2;
		newcarvalue = 698.24;
	}

	public Car ( String ma, String mo, int yr, double od, double tc, double ncv )   
	{
		make = ma;
		model = mo;
		year = yr;
		odometernew = odometerold = od;
		tankcapacity = tc;
		newcarvalue = ncv;
	}

	public void Output ( )   
	{
		System.out.println ( "\nMAKE: " + make + "     MODEL: " + model );
		System.out.println ( "YEAR: " + year );
		System.out.println ( "ODOMETER READING: " + odometernew );
		System.out.println ( "TANK CAPACITY: " + tankcapacity + "\n" );
	}

	public double CalcCarValue ( int currentyear )   
	{
		double carvalue = 0.0;
		carvalue = newcarvalue * Math.pow ( 1.0 - 0.05, currentyear - year );
		return carvalue;
	}
	
	public double MPG ( double od )   
	{
		odometerold = odometernew;
		odometernew = od;
		double mpg = 0.0;
		mpg = ( odometernew - odometerold ) / tankcapacity;
		return mpg;
	}
}

//  Scott DeRuiter      9/24/2002
//  CarTest.java
//  Tests the car class.

public class CarTest   
{
	public static void main ( String [] args )   
	{
		Car mycar = new Car ( );
		Car wifescar = new Car ( "Toyota", "Camry", 1998, 91234.5, 14.2, 10000.0 );

		mycar.Output ( );

		wifescar.Output ( );
		System.out.println ( "Wife's car is worth: " + wifescar.CalcCarValue ( 2002 ) + "\n\n" );
		System.out.println ( "The miles per gallon for this trip is: " +
			wifescar.MPG ( 91407.2 ) + "\n\n" );
		System.out.println ( "The miles per gallon for this trip is: " +
			wifescar.MPG ( 91708.2 ) + "\n\n" );
	}
}

Back to Lesson 5 Examples

Back to Java Main Page