Answer to Question #179433 in Java | JSP | JSF for GILJUN L. PINOTE

Question #179433

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

◦intweight;


1
Expert's answer
2021-04-08T07:25:54-0400
package q179433;


public class Q179433 {


    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 void displayInfo() {
            System.out.println("Speed: " + speed);
            System.out.println("Regular price: " + regularPrice);
            System.out.println("Color: " + color);
        }


    }


    class Truck extends Car {


        private int weight;


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


        @Override
        public void displayInfo() {
            super.displayInfo();
            System.out.println("Weight: " + weight);


        }
    }
    Truck truck = new Truck(150, 36000, "Red", 23000);


    public static void main(String[] args) {
        Q179433 q179433 = new Q179433();
        q179433.truck.displayInfo();
    }


}

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