Write the code for the classes in Java program?

2008-04-18 8:01 am
Vehicle class, which has member variables no_of_wheels, license_no, color and a member method call GetNoOfWheels will return the no of wheels. Moreover, it also has a concrete member method call ShowInfo which show the employee basic information.

Rank________________No of wheels
MotorCycle______________2
Van___________________ 4
MiniBus________________ 6

Write the code for the classes MotorCycle, Van and MiniBus. All of them are devised from the Vehicle class.
The code should show the concept of Encapsulation
The code should show the concept of Inheritance
Bonus will give to the code that show the concept of Polymorphism
(Hint: you may assume you have 2 motorcycles, 3 vans and 4 minibuses and then how can you calculate the total of the wheels)

回答 (2)

2008-04-18 8:27 am
✔ 最佳答案
This related problem should show you how to do encapsulation and inheritance. It should help you implement the vehicle classes.

This creates a parent class Employee (like Vehicle). It has member variables salary, name, and age. The classes Manager and Intern are derived from Employee and have different salaries (number of wheels).

public class Employee
{
-- protected double salary;
-- protected String name;
-- protected int age;
-- public Employee(double salary)
-- {
---- this.salary = salary;
-- }
-- public double getSalary()
-- {
---- return salary;
-- }
}

class Manager extends Employee
{
-- public Manager()
-- {
---- super(20000.00);
-- }
}
class Intern extends Employee
{
-- public Intern()
-- {
---- super(100.00);
-- }
}
2016-05-29 5:51 pm
The problem is that you've got methods mixed through methods while trying to strangely insert variables here and there without purpose... I"m not exactly sure what you're getting at, there are way too many errors for me to help you. Logically look at your code and try to clean it up, then re-ask the question, maybe then someone will understand what your code is supposed to do. Good luck. PS: Yes I know it's supposed to print the sort of ice cream the chooser chooses, but I'm pretty sure I will need to write an entirely new code, because nothing makes sense in this one. No offense.


收錄日期: 2021-05-02 19:04:37
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080418000124AAvQypV

檢視 Wayback Machine 備份