Question:
Java (version 1.4) class exercise?
Roel
2009-06-03 13:30:15 UTC
He can anyone do this exercise for me. I've got some more to do as well and I'm running out of time.
Thanks a lot if you would help.

You are requested to construct a Car class containing the following attributes:
- a double variable containing the size of the engine (e.g. 1.8 litres),
- a String variable indicating the kind of fuel (diesel, regular, super),
- a counter mimicking the total number of kilometres driven by the car,
- a fuel indicator indicating the amount of litres in the fuel tank.

Cars can be created having values for each of these variables.
The services available on the Car class are:
- a constructor creating an object with sensible default values for the attributes,
- a constructor creating a car object with user defined values for the attributes,
- a toString() method printing the values of all the attributes of a car,
- an assignment method that performs an assignment of all the attributes of car1 to car2.
- a method to make a trip (adding the number of kilometres driven to the counter, and subtracting an amount of gas from the tank; the amount being linearly dependent upon the kilometres driven.).
- an equals()-method that checks whether two cars are 'the same'. Two cars are defined to be the same if all their attributes, except for the counter and the fuel indicator, are identical.

Create a class TestCars to test the system. This class will contain the main( ) method. In this main method several cars will be constructed using both forms of the constructor.
- try out the equals()-method on these cars,
- assign some car to another car,
- again try out the equals() method between both cars,
- drive various distances with some cars and print all the attributes after each trip made.
Four answers:
Matt
2009-06-03 15:02:19 UTC
I put a 5 minute limit on this, so I'm not testing it.



public class car{



private double maxLitres,litresLeft;

private String fuel;

private double distance;



public car(){

maxLitres=litresLeft=50;

fuel="regular";

distance=0;

}



public car(double maxLitres, double litresLeft,String fuel,double distance){

this.maxLitres=maxLitres;

this.litresLeft=litresLeft;

this.fuel=fuel;

this.distance=distance;

}



public String toString(){

return ("capacity: " + maxLitres +"\nLitres left: " + litresLeft + "\nFuel Type: " + fuel + "\nDistance travelled: " + distance + "\n");

}

public void copy(car that){

that.maxLitres=this.maxLitres;

that.litresLeft=this.litresLeft;

that.fuel=this.fuel;

that.distance=this.distance;

}

public void travel(double distance){

this.distance+=distance;

final double LITRES_USED_PER_DISTANCE_UNIT = 0.5;//I really have no idea, you need to choose this value

litresLeft-=(distance*LITRES_USED_PER_DISTANCE_UNIT);

}

public boolean equals(car that){

if(that.maxLitres==this.maxLitres&&that.litresLeft==this.litresLeft&&that.fuel==this.fuel&&that.distance==this.distance){

return true;

}

return false;

}

}
benita
2016-05-24 03:55:58 UTC
I assume you mean "Generics". Generics were introduced in Java 5 and did not exist in any other form prior to Java 5. Also, Generics are a feature of the Java language and so are not derived from a superclass.
elven_rangers
2009-06-03 13:40:13 UTC
maybe if you pay. Can't really imagine anyone actually spending the time to write a test app for a class just for kicks. Used to do that sometime ago ... not anymore though.
anonymous
2009-06-03 13:43:13 UTC
// attributes

double size;

String fuelType;

int kilometers;

int fuel;



The rest I leave to you.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...