Using what you have learned in class, Design 4 classes: \' Car ‘ Porshe \' Prius
ID: 3679000 • Letter: U
Question
Using what you have learned in class, Design 4 classes:
' Car
‘ Porshe ' Prius
' Ford
Decide which class should inherit from with class, what attributes they should all have especially your static or non-static members. You can either draw the boxes or write the actual code. Either way, your design has to be very clear. (For e.g., If you make me search for it, you will lose points...)
[You don't have to provide the complete code. But if it will help you, you can provide the code. Typically, if you code before designing it, you will end up with a disaster.]
Indicate which attributes are static, which are non-static members in your design. Which attributes should be inherited and which should not? Which attributes that is shared by all and which cannot be shared? Which attribute belongs specifically to the actual object itself and not the class?
(Remember to use final if you want to use a constant or it will not count)
*Provide sample code for the constructors and also the code to (create) instantiate the object of your class.
For e.g., can a user build a car (instantiate your object) using this code: Car a = new Car() be used? Why and Why not? Does it make sense? Provide your assumptions.
Other things to think about, if user wants to know the price of a Porshe car, does it make sense that you need to build the Porshe car first(instantiate the object) before the user can know its worth?
Explanation / Answer
class Car
{
private static int NoOfCar=0;
public Sring carModel;
public int releaseYear ;
public String registrationNo;
public Car(String cm,int ry,String rn)
{
carModel=cm;
releaseYear=ry;
registrationNo=rn;
NoOfCar++;
}
}
class Porshe extends Car
{
private int NoOfPorsheCar;
public Porshe(String cm,int ry,String rn)
{
super(cm,ry,rn);
NoOfPorsheCar++;
}
}
class Prius extends Car
{
private int NoOfPriusCar;
public Prius(String cm,int ry,String rn)
{
super(cm,ry,rn);
NoOfPriusCar++;
}
}
class Ford extends Car
{
private int NoOfFordCar;
public Ford(String cm,int ry,String rn)
{
super(cm,ry,rn);
NoOfFordCar++;
}
}
instantiate your object:
Car porshe=new Porshe("777x",2009,"AP 31 CW 2546");
Car prius=new Prius("666x",2006,"AP 31 CW 2946");
Car ford=new Ford("555x",2004,"AP 31 CW 2524");
Static variales:
int NoOfCar in Car
int NoOfPorsheCar in Porshe
int NoOfPriusCar in Prius
int NoOfFord Car in Ford
Non Static Variables:int releaseYear in Car
string carModel in Car
Inherited Variables from Car:
public Sring carModel;
public int releaseYear ;
public String registrationNo;
Variables which are not inherited from Car:
private static int NoOfCar;
non Shared Variables from Car:
public Sring carModel;
public int releaseYear ;
public String registrationNo;
Shared Variables from Car:
private static int NoOfCar;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.