Answer to Question #240659 in Java | JSP | JSF for Raj

Question #240659

Create a base class called shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called triangle and rectangle from the base shape. Add to the base class, a member function setData(int, int) to initialize base class data members and another member function displayArea( ) to compute and display the area of figures.


1
Expert's answer
2021-09-23T00:04:53-0400
public class Main
{
	public static void main(String[] args) {
		triangle t=new triangle();
		t.setData(9,5);
		System.out.println("Triangle");
		t.display();
		
		rectangle r=new rectangle();
		r.setData(23,15);
		System.out.println("Rectangle");
		r.display();
	}
}


class shape{
    private double x;
    private double y;
    
    public void setData(double a, double b){
        x=a;
        y=b;
    }
    public void display(){
        System.out.println("Area: "+(x*y));
    }
}


class triangle extends shape{
    
}


class rectangle extends shape{
    
}

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