Answer to Question #195699 in Java | JSP | JSF for Hamza baig

Question #195699
Consider a class Computer having . Two fields ( i.e. companyName , price ) and A single function named show ( ) A class named Desktop inherits Computer class and adds fields representing color , monitor size , and processor type and Override function named show ( ) to display values of its all attributes . A class named Laptop inherits Computer class and adds fields representing color , size , weight , and processor type and Override function named show ( ) to display values of its all attributes . Write a main ( ) function that instantiates objects of derived classes to access respective show ( ) function using dynamic binding .
1
Expert's answer
2021-05-19T15:51:27-0400
class Computer
{
    private String companyName;
    private double price;

    public Computer(String companyName, double price)
    {
        this.companyName = companyName;
        this.price = price;
    }
    public void show()
    {
        System.out.println("Company name : "+companyName);
        System.out.println("Price : "+price);
    }
}

class Desktop extends Computer
{
    String color;
    double monitorSize;
    String processorType;

    public Desktop(String companyName, double price, String color, double monitorSize, String processorType)
    {
        super(companyName, price);
        this.color = color;
        this.monitorSize = monitorSize;
        this.processorType = processorType;
    }

    @Override
    public void show() {
        super.show();
        System.out.println("Color: "+color);
        System.out.println("Monitor size: "+monitorSize);
        System.out.println("Processor type: "+processorType);
    }
}

class Laptop extends Computer
{
    String color;
    double size;
    double weight;
    String processorType;

    public Laptop(String companyName, double price, String color, double size, double weight, String processorType)
    {
        super(companyName, price);
        this.color = color;
        this.size = size;
        this.weight = weight;
        this.processorType = processorType;
    }

    @Override
    public void show() {
        super.show();
        System.out.println("Color: "+color);
        System.out.println("Size: "+size);
        System.out.println("Weight: "+weight);
        System.out.println("Processor type: "+processorType);
    }
}

public class Main {
    public static void main(String[] args)
    {
        Computer computer = new Computer("Company",1000);
        computer.show();

        System.out.println();
        Computer desktop = new Desktop("Company2",1500,"black",105,"Intel");
        desktop.show();

        System.out.println();
        Computer laptop = new Laptop("Company3",1200,"white",55,1,"Processor");
        laptop.show();
    }
}

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