Create a super class called car.
a. The car class has the following fields and methods.
- Intspeed:
-doubleregularPrice:
- stringcolor: doublegelSale Price()
b. Creote a sub class of car class and name It as pickup. The following fields and methods.
- intweight:
- doublegetSidePrice()
If weight is greater than 2000. 15% discount otherwise 25%
c. Create a sub class and name it as Nissan. The follwing fields and methods.
- Intyear;
-intmanufacturerDiscount:
-doublegetsdePrice()
From the sale price computed from car class. subtract the manufacturer discount
d. Create a sub class and name it is methods
-. Intlength:
-doublegetSalePrice()
If length is less than 20 feet. 10% discount. Otherwise, 15% discount.
e. Create a MyShop class which contains the main method. Perform the following within the main() method.
-Create an instance of Toyota class and initialize all the fields with appropriate values. Use super (...)method in the constructor for initializing the fields of the super class.
-Create two instances of the Nissan class and initialize all the fields with appropriate values. Use super(...) method in the constructor for initializing the fields of the super class.
-Create an instance of car class and initialize all the fields with appropriate values. Display the sale prices of all instance.
MyShop.java file
/**
* Create a MyShop class which contains the main method.
* Perform the following within the main() method.
*/
public class MyShop {
/**
* The start point of the program
* @param args
*/
public static void main(String[] args) {
//-Create an instance of Toyota class and initialize all the fields with appropriate values.
//Use super (...)method in the constructor for initializing the fields of the super class.
Toyota toyota =new Toyota(200, 40000, "Red", 15);
//-Create two instances of the Nissan class and initialize all the fields with appropriate values.
//Use super(...) method in the constructor for initializing the fields of the super class.
Nissan nissan=new Nissan(250, 60000, "Black",2015, 5);
Pickup pickup =new Pickup(180, 55000, "Yellow", 1500);
//-Create an instance of car class and initialize all the fields with appropriate values.
Car car =new Car(250, 35000, "Blue");
//Display the sale prices of all instance.
System.out.println(toyota.toString());
System.out.println("The sale price: $"+toyota.getSalePrice()+"\n\n");
System.out.println(nissan.toString());
System.out.println("The sale price: $"+nissan.getSalePrice()+"\n\n");
System.out.println(pickup.toString());
System.out.println("The sale price: $"+pickup.getSalePrice()+"\n\n");
System.out.println(toyota.toString());
System.out.println("The sale price: $"+toyota.getSalePrice()+"\n\n");
System.out.println("Csar\n"+car.toString());
System.out.println("The sale price: $"+car.getSalePrice()+"\n\n");
}
}
Car.java file
public class Car {
//The car class has the following fields and methods.
private int speed;
private double regularPrice;
private String color;
/**
* Constructor
* @param speed
* @param regularPrice
* @param color
*/
public Car(int speed, double regularPrice, String color) {
this.speed=speed;
this.regularPrice=regularPrice;
this.color=color;
}
/**
* Get regular price
* @return
*/
public double getSalePrice() {
return regularPrice;
}
/***
* Returns information
*/
public String toString() {
return "Speed: "+speed+
"\nRegular price: $"+regularPrice+
"\nColor: "+color;
}
}
Pickup.java file
public class Pickup extends Car{
private int weight;
/**
* Constructor
* @param speed
* @param regularPrice
* @param color
* @param weight
*/
public Pickup(int speed, double regularPrice, String color,int weight) {
super(speed, regularPrice, color);
this.weight=weight;
}
/**
* If weight is greater than 2000. 15% discount otherwise 25%
*/
@Override
public double getSalePrice() {
double salePrice=super.getSalePrice();
if(weight>2000) {
return salePrice-salePrice*0.15;
}
return salePrice-salePrice*0.25;
}
/***
* Returns information
*/
public String toString() {
return "Pickup\n"+super.toString()+"\nWeight: "+weight;
}
}
Nissan.java file
public class Nissan extends Car{
private int year;
private int manufacturerDiscount;
/**
* Constructor
* @param speed
* @param regularPrice
* @param color
* @param weight
*/
public Nissan(int speed, double regularPrice, String color,int year,int manufacturerDiscount) {
super(speed, regularPrice, color);
this.year=year;
this.manufacturerDiscount=manufacturerDiscount;
}
/**
* From the sale price computed from car class subtract the manufacturer discount
* @return
*/
@Override
public double getSalePrice() {
return super.getSalePrice()-(super.getSalePrice()*((double)manufacturerDiscount/100.0));
}
/***
* Returns information
*/
public String toString() {
return "Nissan\n"+super.toString()+"\nYear: "+year+"\nManufacturer discount: "+manufacturerDiscount+"%";
}
}
Toyota.java file
public class Toyota extends Car{
private int length;
/**
* Constructor
* @param speed
* @param regularPrice
* @param color
* @param weight
*/
public Toyota(int speed, double regularPrice, String color,int length) {
super(speed, regularPrice, color);
this.length=length;
}
/**
* If length is less than 20 feet. 10% discount. Otherwise, 15% discount.
*/
@Override
public double getSalePrice() {
double salePrice=super.getSalePrice();
if(length<20) {
return salePrice-salePrice*0.1;
}
return salePrice-salePrice*0.15;
}
/***
* Returns information
*/
public String toString() {
return "Toyota\n"+super.toString()+"\nLength: "+length+ " feet";
}
}
Output
Toyota
Speed: 200
Regular price: $40000.0
Color: Red
Length: 15 feet
The sale price: $36000.0
Nissan
Speed: 250
Regular price: $60000.0
Color: Black
Year: 2015
Manufacturer discount: 5%
The sale price: $57000.0
Pickup
Speed: 180
Regular price: $55000.0
Color: Yellow
Weight: 1500
The sale price: $41250.0
Toyota
Speed: 200
Regular price: $40000.0
Color: Red
Length: 15 feet
The sale price: $36000.0
Csar
Speed: 250
Regular price: $35000.0
Color: Blue
The sale price: $35000.0
Comments
Leave a comment