Create a super class called car.
a. The car class has the following fields and methods.
-Intspeed;
-doubleregularPrice;
-stringcolor;
-doublegetSalePrice();
b. Create a sub class of car class and name it as pickup. The following fieldsand methods.
-Intweight;
-doublegetSalePrice()
*** If weight is greater than 2000, 15% discount otherwise 25%
c. Create a sub class and name itas Nissan. The following fields and methods.
-Intyear;
-intmanufacturerDiscount;
-doublegetSalePrice()
***From the sale price computed from car class, subtract the manufacturer discount.
d. Create a sub class and name it as Toyota. The following fieldsand 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.
public class Car {
private int speed;
private double regularPrice;
private String color;
public Car(int speed, double regularPrice, String color) {
this.speed = speed;
this.regularPrice = regularPrice;
this.color = color;
}
public double getSalePrice() {
return regularPrice;
}
}
public class Pickup extends Car {
private int weight;
public Pickup(int speed, double regularPrice, String color, int weight) {
super(speed, regularPrice, color);
this.weight = weight;
}
public double getSalePrice() {
return super.getSalePrice() * (weight > 2000 ? 0.85 : 0.75);
}
}
public class Toyota extends Car {
private int length;
public Toyota(int speed, double regularPrice, String color, int length) {
super(speed, regularPrice, color);
this.length = length;
}
public double getSalePrice() {
return super.getSalePrice() * (length < 20 ? 0.9 : 0.85);
}
}
public class Nissan extends Car {
private int year;
public int manufacturerDiscount;
public Nissan(int speed, double regularPrice, String color, int year, int manufacturerDiscount) {
super(speed, regularPrice, color);
this.year = year;
this.manufacturerDiscount = manufacturerDiscount;
}
public double getSalePrice() {
return super.getSalePrice() - manufacturerDiscount;
}
}
public class MyShop {
public static void main(String[] args) {
Toyota toyota = new Toyota(500, 500, "red", 30);
Nissan nissanOne = new Nissan(300, 300, "black", 2021, 35);
Nissan nissanTwo = new Nissan(250, 250, "yellow", 2020, 40);
Car car = new Car(100, 100, "white");
System.out.println("toyota "+toyota.getSalePrice());
System.out.println("nissanOne "+nissanOne.getSalePrice());
System.out.println("nissanTwo "+nissanTwo.getSalePrice());
System.out.println("car "+car.getSalePrice());
}
}
Comments
Leave a comment