anyone got answers to this one ?
Question 16 (40 marks)
Samantha Sunshine owns a fleet of bright yellow solar-powered taxi cars. She also owns a small garage where only one taxi can be checked or serviced at a time. Each taxi is identified by a name (such as Solar Wind) chosen by Samantha. She has asked you to write a program to manage information kept on her taxis. As part of the project, your task is to design and implement two classes: (1) Class Taxi to represent a taxi, and (2) Fleet to represent the taxi fleet. Each taxi has a name, the number of kilometers the taxi has traveled since its last service, and the number of trips it has done since its last service. The data kept on a taxi is updated as soon as it returns from a trip.
Definition of class Taxi Based on the above information, it is decided that class Taxi is to have three attributes declared as follows:
private String name;
private double mileage;
private int trips;
where mileage is the number of kilometers traveled since last service.
a) Define the constructor for the class. The constructor should take only one argument, the
name of the taxi.
b) Define the toString method that can be used to facilitate the testing of the class.
c) Define the update method that will be used to update the information of the taxi after
each trip it completes.
d) Define the reset method that will be used to reset the values of mileage and
trips each time the taxi is serviced.
e) Define all the get methods.
Definition of Class Fleet
Assume that class Taxi has been developed and thoroughly tested. You are now in a position
to develop class Fleet. This class stores and manages information about the fleet of taxis.
Functionality provided by this class should include methods to do the following:
?Add a taxi to the fleet
?Remove a taxi from the fleet
?Update the information about a particular taxi
?To reset the number of trips and the number of kilometers traveled for a particular taxi
?Determine the taxi to service next (see more information below)
You are told that the fleet is not expected to have more than 50 taxis. Using this information,
the attributes for class Fleet are declared as follows:
public static final MAX_FLEET_SIZE = 50;
private Taxi [] taxis;
private int taxiCount;
where taxiCount is the number of taxis in the current fleet.
f) Define the constructor for class Fleet.
g) Define method search( String name ).
This method takes as argument the name of the taxi to search for. If a taxi with that
name is found, the method should return the index of the taxi. Otherwise, it should
return -1.
(This method will be used by other methods of the class, for example, the addTaxi
that is defined next.)
h) Define method addTaxi( String name ).
This method takes as an argument the name of the new taxi and adds the taxi to the
fleet. It should check that the name is indeed new. To do this, it should make use of the
method search described above.
[4 marks]
i) Define a method to delete a taxi from the fleet.
[4 marks]
j) Define a method to update the information about a particular taxi
[4 marks]
k) Define a method to reset the number of trips and the number of kilometers traveled for
a particular taxi.
[4 marks]
l) Define a method to determine the taxi to service next.
As the garage can only service one taxi at a time, the taxi with the highest mileage since
its last service will be chosen for the next service. In cases of ties, one of the taxis will
be chosen. The method should return the name of the taxi to be serviced next.
[4 marks]
[3830 byte] By [
banjamima] at [2007-10-2 22:06:28]

Nice assignment; given the description of the problem it almostprograms itself. I'd say: go for it.kind regards,Jos
hi :-)just do it ;-)regards,
looks like a good question - to get you started:class Taxi {//code}andclass Fleet extends Taxi {//code}
> class Fleet extends Taxi {> //code> }That would be a very bad idea.A fleet is not a particular kind of taxi. It doesn't make sense to use a fleet where a taxi is expected. Therefore, Fleet should not extend Taxi.
public class Taxi
{
private String name;
private double mileage;
private int trips;
public static void main(String[] args)
{
//constructor
public Taxi(String name)
{
this.name = name;
}
//public void update()
public String toString()
{
return " name "+name+" mileage " +mileage+ " trips " + trips;
}
}
}
ive done this much so far but it doesnt compile
also i dont know how to make a class to update the information of the taxi
thanks ppl
>> ive done this much so far but it doesnt compile
What specific errors are you getting and on which lines?
When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.
my error message is :20> javac Taxi.javaTaxi.java:22: illegal start of expressionpublic Taxi(String name)^Taxi.java:34: ';' expected^2 errors
oh i fixed it it compiles nowneeded to get rid of the "main" decleration thing
anyone have any idea on how to create an update method for this ?
Answer the following questions:
1) Which values have to be changed in this update method?
2) How are those values changed?
3) Do you expect an answer from this update?
The answer to question 1 will tell you what happens within the method, the answer to question 2 the formal parameter list and the last answer the return type of this method...
ah k thanks ... Is this pseudocode correct ?[code]public void update(){trips = trips++;mileage = mileage + kmtraveled;return trips;return mileage;}[code]
public void update()
{
trips = trips++;
mileage = mileage + kmtraveled;
return trips;
return mileage;
}
Good direction, some problems. First of all, this code won't compile due to several reasons:
- You are using an unknown variable (kmtraveled). Maybe proving it as a paramter could solve this problem *g*
- You chose void as the method's return type, but also implemented a return statement. Either or. Hint: do you need a return value?
- You implemented two return statements.
public class Taxi
{
private String name;
private double mileage;
private int trips;
private double kmtraveled;
//constructor
public Taxi(String name)
{
this.name = name;
}
public void update()
{
trips = trips++;
mileage = mileage + kmtraveled;
//return trips;
//return (int)mileage;
}
public String toString()
{
return " name "+name+" mileage " +mileage+ " trips " + trips;
}
}
ah ok... ive changed it .. now it compiles.. the return of the values is done by the toString method so i axed the others..
now i need to create a method to reset the values of 'mileage' and 'trips' each time the taxi is serviced ... do you know how to do this ?
> now i need to create a method to reset the values of
> 'mileage' and 'trips' each time the taxi is serviced
> ... do you know how to do this ?
What is the problem? You do know that you need a method, and you do know what to name it (either service, or reset). You do also know what the method should do, reset some of the attributes of the class.
Kaj
ah k so will this do the job. public void reset(){ trips = 0; mileage = 0;}
> ah k so will this do the job. > public void reset()>{>trips = 0;>mileage = 0;> }> Yes
awesome...
:( my code doesnt compile
can anyone tell me how to fix it ?
40> javac Taxi.java
Taxi.java:53: missing method body, or declare abstract
public int getTrips();
^
Taxi.java:55: return outside method
return trips;
^
2 errors
public void update()
{
trips = trips++;
mileage = mileage + kmtraveled;
//return trips;
//return (int)mileage;
}
public void reset()
{
trips = 0;
mileage = 0;
}
public String getName()
{
return name;
}
public double getMileage()
{
return mileage;
}
public int getTrips();
{
return trips;
}
public String toString()
{
return " name "+name+" mileage " +mileage+ " trips " + trips;
}
}
i got itt... removed the' ; '
can anyone explain to me how to create a class Fleet ?
> can anyone explain to me how to create a class Fleet ?
Sure, here you go:
Definition of Class Fleet
Assume that class Taxi has been developed and thoroughly tested. You are now in a position
to develop class Fleet. This class stores and manages information about the fleet of taxis.
Functionality provided by this class should include methods to do the following:
Add a taxi to the fleet
Remove a taxi from the fleet
Update the information about a particular taxi
To reset the number of trips and the number of kilometers traveled for a particular taxi
Determine the taxi to service next (see more information below)
You are told that the fleet is not expected to have more than 50 taxis. Using this information,
the attributes for class Fleet are declared as follows:
public static final MAX_FLEET_SIZE = 50;
private Taxi [] taxis;
private int taxiCount;
where taxiCount is the number of taxis in the current fleet.
f) Define the constructor for class Fleet.
g) Define method search( String name ).
This method takes as argument the name of the taxi to search for. If a taxi with that
name is found, the method should return the index of the taxi. Otherwise, it should
return -1.
(This method will be used by other methods of the class, for example, the addTaxi
that is defined next.)
h) Define method addTaxi( String name ).
This method takes as an argument the name of the new taxi and adds the taxi to the
fleet. It should check that the name is indeed new. To do this, it should make use of the
method search described above.
[4 marks]
i) Define a method to delete a taxi from the fleet.
[4 marks]
j) Define a method to update the information about a particular taxi
[4 marks]
k) Define a method to reset the number of trips and the number of kilometers traveled for
a particular taxi.
[4 marks]
l) Define a method to determine the taxi to service next.
As the garage can only service one taxi at a time, the taxi with the highest mileage since
its last service will be chosen for the next service. In cases of ties, one of the taxis will
be chosen. The method should return the name of the taxi to be serviced next.
[4 marks]
> > can anyone explain to me how to create a class Fleet ?> > Sure, here you go: <snip/>Who'd have thought it?! ;-)kind regards,Jos
> Who'd have thought it?! ;-)> > kind regards,> > JosMornin' Jos.Yeah, it was a wild guess, but I think I'm pretty close!; )Regards,Bart.
> > Who'd have thought it?! ;-)
>
> Mornin' Jos.
Mornin' Bart.
> Yeah, it was a wild guess, but I think I'm pretty close!
> ; )
You must be psychic just like me! I thought exactly the same, but
being the slowest old sod I am I just waited a bit ... ;-)
kind regards,
Jos
> You must be psychic just like me! I thought exactly
> the same, but
> being the slowest old sod I am I just waited a bit
> ... ;-)
>
> kind regards,
>
> Jos
I think my weekend-psychicism is starting to spread; it's starting on Friday nowadays (but you already knew that, which I knew you knew of course...).
; )
> > You must be psychic just like me! I thought exactly the same, but
> > being the slowest old sod I am I just waited a bit ... ;-)
> I think my weekend-psychicism is starting to spread;
> it's starting on Friday nowadays (but you already
> knew that, which I knew you knew of course...).
> ; )
Sure, just like you knew already that the OP is going to fail miserably
if he doesn't start reading his own assigment text carefully ;-)
kind regards,
Jos