File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Code Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Code Help" Watch "Code Help" New topic
Author

Code Help

Muhammad Aasim
Greenhorn

Joined: Oct 16, 2009
Posts: 9
Write the code to implement the concept of inheritance for Vehicles. You are required to implement inheritance between classes. You have to write four classes in java i.e. one super class, two sub classes and one driver class.

Vehicle is the super class whereas Bus and Truck are sub classes of Vehicle class. Transport is a driver class which contains main method.


Detailed description:


Detailed description of Vehicle (Super class):

The class Vehicle must have following attributes:

1. Vehicle model
2. Registration number
3. Vehicle speed (km/hour)
4. Fuel capacity (liters)
5. Fuel consumption (kilo meters/liter)

The Vehicle class must have following methods:

1. Parameterized constructor that will initialize all the data members with the given values.
2. Getters and Setters for each data member that will get and set the values of data members of class.
3. A method fuelNeeded() that will take distance (in kilo meter) as an argument. It will calculate the amount of fuel needed for the given distance and will return the value of fuel needed for given distance. You can use the attributes ‘Fuel consumption’ defined within above Vehicle class to determine the fuel needed for the given distance. You are required to implement this functionality by yourself.
4. A method distanceCovered() that will take time (in hours) as an argument. It will calculate the distance for the given time and speed and returns the value of distance. The formula to calculate speed is given as speed = distance/time. You can use this formula to calculate the distance.
5. A display() method that will display all the information of a vehicle.


Detailed description of Truck (Sub class):

The class Truck must have following attribute:
Cargo weight limit (Kilo grams)

The above class must have following methods:

1. Parameterized constructor that will initialize all data members with the given values.
2. Getters and setters for each data member that will get and set the values of data members of class.
3. It must also override the display() method of Vehicle class and must call display() method of super class within overridden method.


Detailed description of Bus (Sub class):

The class Bus must have following attribute:
No of passengers


The above class must have following methods:

1. Parameterized constructor that will initialize all the data members with given values.
2. Getters and setters that will get and set the value of each data member of class.
3. It must also override the display() method of Vehicle class and must call display method of super class within overridden method.

Create a class Transport which contains the main method. Perform the following within main method:

• Create an instance of class Truck and initialize all the data members with proper values.
• Create an instance of class Bus and initialize all the data members with proper values.
• Now, call fuelNeeded(), distanceCovered() and display() methods using objects of these classes.


EveRy THinG is pOssiBle bUt LovE caN NaveR FOr Me...
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi,

Your requirement seems to be clearly stated and straight forward.

So where are you stuck?


Priety.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

"Bwp Guy",
Please check your private messages for an important administrative matter.

Here at the Ranch, we do not just hand out code. http://faq.javaranch.com/java/DoYourOwnHomework
To understand, how you can get the most out of the Ranch, please read http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch

Like Priety said, if you show us what have you got so far and tell us where you are stuck, we can help point you in the right direction.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
And always tell people what the thread is about.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That went well.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

David Newton wrote:That went well.


Yeah... that did cross the line a bit. I understand the challenge for the naming violation, but some of the other stuff can be interpreted as "not nice" -- particularly for a newbie to these forums.


Anyway, "Bwp Guy", welcome to the JavaRanch. We are normally much nicer -- I swear...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
rehman ali
Greenhorn

Joined: Oct 18, 2009
Posts: 3
i m having problem in these steps.

3. A method fuelNeeded() that will take distance (in kilo meter) as an argument. It will calculate the amount of fuel needed for the given distance and will return the value of fuel needed for given distance. You can use the attributes ‘Fuel consumption’ defined within above Vehicle class to determine the fuel needed for the given distance. You are required to implement this functionality by yourself.
4. A method distanceCovered() that will take time (in hours) as an argument. It will calculate the distance for the given time and speed and returns the value of distance. The formula to calculate speed is given as speed = distance/time. You can use this formula to calculate the distance.

in step 3:

there is not distance declared in data members so who we will pass the argument to fuelNeeded() method.


in step 4:

similary in step 4 for time.

how to calculate the fuelNeeded() & distanceCovered();

kindly guide me codeing these two step.
rehman ali
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

rehman ali wrote:i m having problem in these steps.

3. A method fuelNeeded() that will take distance (in kilo meter) as an argument. It will calculate the amount of fuel needed for the given distance and will return the value of fuel needed for given distance. You can use the attributes ‘Fuel consumption’ defined within above Vehicle class to determine the fuel needed for the given distance. You are required to implement this functionality by yourself.

in step 3:

there is not distance declared in data members so who we will pass the argument to fuelNeeded() method.


That's the requirement, the distance will be passed as an argument.... so whoever is going to be using your class, will be passing the distance value to you.

And of course, you need to test your class, so you will probably have some testing code that uses your class right?

Henry
rehman ali
Greenhorn

Joined: Oct 18, 2009
Posts: 3


kindly check these method and tell me the logical & stynax mistake in it.

regards
rehman
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

rehman ali wrote:
kindly check these method and tell me the logical & stynax mistake in it.


Isn't it easier to just use a compiler?

Henry
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.

rehman ali
Greenhorn

Joined: Oct 18, 2009
Posts: 3
David Newton wrote:Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.






the code seems like code now!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code Help
 
Similar Threads
Abstract classes vs polymorphism
please help me
method Overriding - Class Bus extends Vehicle, Class Car extends Vehicle
Querying collections (interfaces)
"is a" and "has a"