Using Inheritance in Java

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 4312 Views - Last Post: 07 October 2010 - 10:09 AM Rate Topic: -----

#1 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Using Inheritance in Java

Posted 07 October 2010 - 04:08 AM

Your task is to create a programming system for a ferry. The ferry transports passengers and vehicles (cars, busses, lorries and bicycles). The ferry has space for 200 passengers and 40 cars. A lorry need as much space as two busses or 8 cars. A car need as much space as 5 bicycles. There are different fees for different vehicles and an extra fee might be added for passengers. Use the following fees:

Passenger without vehicle, 20 $.
Bicycle 40$ (passenger included).
Car 100$ + 15 $/passenger. (maximum 4 passengers)
Bus 200 $ + 10 $/ passenger. (maximum 20 passengers)
Lorry 300 $ + 15 $/ passenger. (maximum 2 passengers)
Every type of vehicle (car, bus, lorry, bicycle) will inherit from the class Vehicle. The functionality of the ferry is given by the interface Ferry :
public interface Ferry  {
   int countPassengers();             // Number of passengers on board
   int countVehicleSpace();           // Used vehicle space. One car is 1.
   int countMoney();                  // Earned money
   Iterator iterator();               // Vehicle iterator   
   void embark(Vehicle v);            // Embark vehicle, warning if not enough space
   void embark(Passenger p);          // Embark passenger, warning if not enough room
   void disembark();                  // Clear (empty) ferry. The money earned remains, 
                                      // i.e., is not reset to zero 
   boolean hasSpaceFor(Vehicle v);    // true if we can embark vehicle v
   boolean hasRoomFor(Passenger p);   // true if we can embark passenger p
   String toString();                 // Nice looking ferry status print out

}


A vehicle cannot leave the ferry until the ferry has been disembarked and the same vehicle cannot embark twice. The ferry iterator should iterate over all vehicles embarked (not the passengers). Also write a program FerryMain.java, embarking a number of vehicles and passengers, showing the functionality of the methods.

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by codebuzz: 07 October 2010 - 06:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using Inheritance in Java

#2 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 178
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Using Inheritance in Java

Posted 07 October 2010 - 04:55 AM

I think what you posted explains it pretty well. Go grab a pen and a piece of paper and mull it over for a little while, see what you come up with and then come back.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,210
  • Joined: 23-August 08

Re: Using Inheritance in Java

Posted 07 October 2010 - 05:04 AM

You've been given this assignment because you're expected to know how to do it. If you do not, then explaining that is a job for your instructor, as noted above. We're neither a free homework service, nor personal tutors. If you give it your BEST EFFORT and have problems, come back with the code -- IN CODE TAGS -- and we'll try to help.

:code:
Was This Post Helpful? 0
  • +
  • -

#4 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Re: Using Inheritance in Java

Posted 07 October 2010 - 05:12 AM

View PostJambr, on 07 October 2010 - 03:55 AM, said:

I think what you posted explains it pretty well. Go grab a pen and a piece of paper and mull it over for a little while, see what you come up with and then come back.


well I have created three classes at the moment..namely Ferry.java which is the interface that describes the functionality of the task to be performed, FerryMain.java that will perform the operation in each method and Vehicle.java.
public abstract class Vehicle implements Ferry  {
	
	public int car;
	public int lorry;
	public int  bus;
	public int bicycles;
	public int passengers;
	
	public Vehicle(int p, int numCar,int numBus, int numLorry,int numBicycles ){
		 passengers = p;
		 car = numCar;
		 bus = numBus;
		 lorry = numLorry;
		 bicycles = numBicycles;
	}		

public int countPassengers() {
	      if (passengers <= 200){
	    	  countPassengers();
	      }
		return passengers;
}


/public int countVehicleSpace() {
	
	return 0;
}

public int countMoney() {
	
	return 0;
}


/*public Iterator iterator() {
	
	return null;
}


/public void embark(Vehicle v) {
		
}

public void embark(Passenger p) {
		
}

public void disembark() {
		
}

public boolean hasSpaceFor(Vehicle v) {
	
	return false;
}


public boolean hasRoomFor(Passenger p) {
	
	return false;
}}



Edited by macosxnerd101: Please, :code:.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10179
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Using Inheritance in Java

Posted 07 October 2010 - 05:19 AM

So what problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#6 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 178
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Using Inheritance in Java

Posted 07 October 2010 - 05:24 AM

How will this program be tested? As in: Does it have to work for a set number of passengers/vehicles, or does it have to work for multiple sets of numbers of passengers and vehicles?
Was This Post Helpful? 0
  • +
  • -

#7 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Re: Using Inheritance in Java

Posted 07 October 2010 - 05:24 AM

View Postmacosxnerd101, on 07 October 2010 - 04:19 AM, said:

So what problems or errors are you encountering?

the Vehicle types busses,cars,lorries and bicycles , I need to make them inherit from the super class Vehicle so that all the methods in Vehicle class could be used by them. You will understand more by reading the question I posted.
Was This Post Helpful? 0
  • +
  • -

#8 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Using Inheritance in Java

Posted 07 October 2010 - 05:28 AM

View Postcodebuzz, on 07 October 2010 - 12:24 PM, said:

View Postmacosxnerd101, on 07 October 2010 - 04:19 AM, said:

So what problems or errors are you encountering?

the Vehicle types busses,cars,lorries and bicycles , I need to make them inherit from the super class Vehicle so that all the methods in Vehicle class could be used by them. You will understand more by reading the question I posted.


Okay? Where is your attempt at that? Hint "extends".
Was This Post Helpful? 0
  • +
  • -

#9 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Re: Using Inheritance in Java

Posted 07 October 2010 - 05:36 AM

View PostJambr, on 07 October 2010 - 04:24 AM, said:

How will this program be tested? As in: Does it have to work for a set number of passengers/vehicles, or does it have to work for multiple sets of numbers of passengers and vehicles?

The ferry has space for 200 passengers and 40 cars. A lorry need as much space as two busses or 8 cars. A car need as much space as 5 bicycles. There are different fees for different vehicles and an extra fee might be added for passengers. Use the following fees:

Passenger without vehicle, 20 $.
Bicycle 40 $ (passenger included).
Car 100 $ + 15 $/passenger. (maximum 4 passengers)
Bus 200 $ + 10 $/ passenger. (maximum 20 passengers)
Lorry 300 $ + 15 $/ passenger. (maximum 2 passengers)
Every type of vehicle (car, bus, lorry, bicycle) will inherit from the class Vehicle. The functionality of the ferry is given by the interface Ferry :

This post has been edited by codebuzz: 07 October 2010 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#10 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 178
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Using Inheritance in Java

Posted 07 October 2010 - 05:39 AM

I read that already, but how will your instructor test that your program works correctly?
Was This Post Helpful? 0
  • +
  • -

#11 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Re: Using Inheritance in Java

Posted 07 October 2010 - 05:47 AM

View PostJambr, on 07 October 2010 - 04:39 AM, said:

I read that already, but how will your instructor test that your program works correctly?


By using the contract stated in the Interface Class which I have stated above in the question or you can check them within the code tags.
Was This Post Helpful? 0
  • +
  • -

#12 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Re: Using Inheritance in Java

Posted 07 October 2010 - 06:06 AM

View PostJambr, on 07 October 2010 - 04:39 AM, said:

I read that already, but how will your instructor test that your program works correctly?

By using the contract stated in the Interface Class which I have stated above in the question or you can check them within the code tags.
Was This Post Helpful? 0
  • +
  • -

#13 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 178
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Using Inheritance in Java

Posted 07 October 2010 - 06:59 AM

I think you are misunderstanding me. I understand that 200 passengers and 40 cars is the maximum amount that the ferry can carry. Will your instructor give you a case where 250 passengers and 48 cars want to get on the ferry? (Or a similar case) And see how your program handles it?
Was This Post Helpful? 0
  • +
  • -

#14 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2495
  • View blog
  • Posts: 15,450
  • Joined: 24-September 08

Re: Using Inheritance in Java

Posted 07 October 2010 - 07:19 AM

Okay, what do all of these have in common? They're vehicles and they have a certain capacity (number of people who can occupy said vehicle). You would probably make an abstract class or such with at least the capacity mutator methods, a default constructor to set default values for capacity values and such. Try thinking about this first.

(from what I've skimmed, you're still clueless as to how to go about it, so I'm trying to hint at it)
Was This Post Helpful? 0
  • +
  • -

#15 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Re: Using Inheritance in Java

Posted 07 October 2010 - 07:23 AM

View PostJambr, on 07 October 2010 - 05:59 AM, said:

I think you are misunderstanding me. I understand that 200 passengers and 40 cars is the maximum amount that the ferry can carry. Will your instructor give you a case where 250 passengers and 48 cars want to get on the ferry? (Or a similar case) And see how your program handles it?


The program is to show the functionality of the methods as described in the Ferry class Interface. They want to test us on how to use inheritance in Java that is why the Vehicle types like Busses, Cars,Lorries and Bicycles must inherit from the Class Vehicle. We are not given special case in this regard but the price to carry each vehicle types was written in the question that will determine how many passengers , bicycles,cars,busses and lorries the ferry can carry up to its maximum capacity. Please read the question again to understand me better.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2