Total de visualizações de página

sábado, 11 de dezembro de 2010

Using Classes and Objects

Using Classes and Objects
Car.java (1.499 Kb)
index.html (1.243 Kb)

Assignment #4

Objective

In this assignment you will create a test program that uses the class Car provided to you.  Your test program will create Car objects and manipulate them. 

Project Specifications

1.      Download the Car class provided above and save it in a new folder.
2.      Start DrJava and open the class Car. 
3.      Click on the right-most button Javadoc and click ok when the Select Javadoc Destination window appears.  This utility will create the help documentation for Car class and save it in your folder.
4.      Review the help to check for data, constructors, and all the methods available for this class.
5.      Provide an application called CarTester in the same folder where you placed the Car class.
6.      Use comments to show your name, the date and the assignment number.
7.      Create at least two different car objects by using each constructor once.
8.      Print each object after it is created to display its state.
9.      Use each method provided by the class at least once per object. 
10. Make sure you print the object each time you change the state.
11. Manipulate the cars so that in the end they all have travelled a distance of 1000 miles.  
12. Display which car uses the least amount of gas (is more efficient).
13. Do NOT modify the Car class.

Due Date: 

Submit your *.java file through Blackboard no later than Oct 26 by 11:59 pm.

Item A4

>> View/Complete Assignment: A4
Item Solution
/**
   This program tests the Car class.
*/
public class CarTest
{
   public static void main(String[] args)
   {
      Car c1 = new Car(50, 0);
      Car c2 = new Car();
     
      System.out.println("Car 1 has " + c1.getMileage() + " miles and " + c1.getGas() + " gallons of gas." );
     
      c1.pumpGas( 100 );
      System.out.println("Car 1 has " + c1.getMileage() + " miles and " + c1.getGas() + " gallons of gas." );
      c1.drive(500);
      System.out.println("Car 1 has " + c1.getMileage() + " miles and " + c1.getGas() + " gallons of gas." );
      c1.drive(500);
      System.out.println("Car 1 has " + c1.getMileage() + " miles and " + c1.getGas() + " gallons of gas." );
     
      System.out.println("\nCar 2 has " + c2.getMileage() + " miles and " + c2.getGas() + " gallons of gas." );
     
      c2.pumpGas( 100 );
      System.out.println("Car 2 has " + c2.getMileage() + " miles and " + c2.getGas() + " gallons of gas." );
      c2.drive(500);
      System.out.println("Car 2 has " + c2.getMileage() + " miles and " + c2.getGas() + " gallons of gas." );;
      c2.drive(500);
      System.out.println("Car 2 has " + c2.getMileage() + " miles and " + c2.getGas() + " gallons of gas." ); 
     
      System.out.println("Both cars have travelled 1000 miles.  The most efficienct car has " + Math.max(c1.getGas(), c2.getGas()) + " gas.");
     
      if ( c1.getGas() > c2.getGas() )
      System.out.println("The most efficient car is Car 1");
      else if  ( c1.getGas() < c2.getGas() )
       System.out.println("The most efficient car is Car 2");    
       else
       System.out.println("They both have same efficiency");    
   }
}









Car.java (class)
/**
 A car can drive and consume fuel based on its efficiency. 
 */ 
public class Car {
  private int mileage;
  private int gas;
  private double efficiency;
  /** 
   Constructs a car with a given fuel efficiency. 
   */ 
  public Car()
  {
    efficiency = 15;
    mileage = 0;
    gas = 0;
  }
  /** 
   Constructs a car with a given fuel efficiency and some gas. 
   @param anEfficiency the fuel efficiency of the car 
   @param igas initial amount of gas in the car 
   */ 
  public Car(double anEfficiency, int igas) 
  {
    efficiency = anEfficiency;
    mileage = 0;
    gas = igas;
  }
  /** Adds a certain amount of gas to the tank. 
    @param amount the amount of fuel to add 
    */
  public void pumpGas( int amount) { 
    gas += amount; 
  }
  /** 
   Returns the amount of gas left in the tank. 
   @return the amount of gas 
   */  
  public int getGas(){ 
    return gas; 
  }
  /** 
   Returns the amount of miles the car has driven. 
   @return the mileage 
   */  
  public int getMileage(){
    return mileage;
  }
  /** 
   Drives a certain distance and consumes gas. 
   @param distance the distance driven 
   */ 
  public void drive( int distance ){
    mileage += distance;
    gas -= distance/efficiency;
  } 
/** 
   Returns the state of the object. 
   @return the state of the object as a string 
   */  
  public String toString(){ 
    return " Car: [ efficiency = " + efficiency + " gas = " + gas + " mileage = " + mileage; 
  }  
} 
 


Class Car

java.lang.Object extended by Car

public class Car
extends Object
A car can drive and consume fuel based on its efficiency.




Constructor Summary
Car()
          Constructs a car with a given fuel efficiency.
Car(double anEfficiency, int igas)
          Constructs a car with a given fuel efficiency and some gas.
 
Method Summary
 void drive(int distance)
          Drives a certain distance and consumes gas.
 int getGas()
          Returns the amount of gas left in the tank.
 int getMileage()
          Returns the amount of miles the car has driven.
 void pumpGas(int amount)
          Adds a certain amount of gas to the tank.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Constructor Detail

Car

public Car()
Constructs a car with a given fuel efficiency.

Car

public Car(double anEfficiency, int igas)
Constructs a car with a given fuel efficiency and some gas.
Parameters:
anEfficiency - the fuel efficiency of the car
igas - initial amount of gas in the car
Method Detail

pumpGas

public void pumpGas(int amount)
Adds a certain amount of gas to the tank.
Parameters:
amount - the amount of fuel to add

getGas

public int getGas()
Returns the amount of gas left in the tank.
Returns:
the amount of gas

getMileage

public int getMileage()
Returns the amount of miles the car has driven.
Returns:
the mileage

drive

public void drive(int distance)
Drives a certain distance and consumes gas.
Parameters:
distance - the distance driven


 

Nenhum comentário:

Postar um comentário