Answer to Question #239626 in Java | JSP | JSF for asmi

Question #239626

Create a class called Car. The Car class has the following fields: int speed, double

regularPrice, String color.

Create a sub class of Car class and name it as Truck. The Truck class has the

following fields and methods: int weight, double getSalePrice(); //If

  1. weight>2000,10%discount.Otherwise,20%discount.
1
Expert's answer
2021-09-20T11:09:04-0400


class Car {
	private int speed;
	private double regularPrice;
	private String color;


	public Car() {
	}


	public Car(int speed, double regularPrice, String color) {
		this.speed = speed;
		this.regularPrice = regularPrice;
		this.color = color;
	}


	public String toString() {
		return "Speed: " + speed + "\n" + "Regular price: " + regularPrice + "\n" + "Color: " + color + "\n";


	}


	/**
	 * @return the speed
	 */
	public int getSpeed() {
		return speed;
	}


	/**
	 * @param speed the speed to set
	 */
	public void setSpeed(int speed) {
		this.speed = speed;
	}


	/**
	 * @return the regularPrice
	 */
	public double getRegularPrice() {
		return regularPrice;
	}


	/**
	 * @param regularPrice the regularPrice to set
	 */
	public void setRegularPrice(double regularPrice) {
		this.regularPrice = regularPrice;
	}


	/**
	 * @return the color
	 */
	public String getColor() {
		return color;
	}


	/**
	 * @param color the color to set
	 */
	public void setColor(String color) {
		this.color = color;
	}


}


class Truck extends Car {
	private int weight;


	public Truck() {
	}


	public Truck(int speed, double regularPrice, String color, int weight) {
		super(speed, regularPrice, color);
		this.weight = weight;
	}


	/**
	 * If weight <2000,10%discount.Otherwise,20%discount.
	 * 
	 * @return
	 */
	public double getSalePrice() {
		if (weight < 2000) {
			return getRegularPrice() - (getRegularPrice() * 0.1);
		}
		return getRegularPrice() - (getRegularPrice() * 0.2);
	}


	public String toString() {
		return super.toString() + "Weight: " + weight + "\nSale price: " + getSalePrice() + "\n";


	}


	/**
	 * @return the weight
	 */
	public int getWeight() {
		return weight;
	}


	/**
	 * @param weight the weight to set
	 */
	public void setWeight(int weight) {
		this.weight = weight;
	}


}


public class Main {


	public static void main(String[] args) {


		Truck Truck1 = new Truck(100, 50000, "Red", 5000);
		Truck Truck2 = new Truck(90, 2000, "Blue", 2000);


		System.out.println(Truck1.toString());
		System.out.println(Truck2.toString());


	}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS