Question
this is the first part code : interface Shape { double getArea(); } class Rectangle implements Shape{ private double side ; public Rectangle(int side) {
this is the first part code :
interface Shape { double getArea(); }
class Rectangle implements Shape{ private double side ;
public Rectangle(int side) { this.side = side; } public double getSide() { return side; } public void setSide(double side) { this.side = side; } @Override public double getArea() { return getSide()* getSide(); } }
class Triangle implements Shape{ private double base ; private double height; public Triangle(int base, int height) { this.base = base; this.height = height; } public double getBase() { return base; } public void setBase(double base) { this.base = base; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } @Override public double getArea() { return 0.5 * getBase()*getHeight(); } }
public class shapeTest { public static void main(String[] args) { int result=0 ;
Shape shapes[]=new Shape[4]; shapes[0]=new Triangle(6,8); shapes[1]=new Triangle(5,3); shapes[2]=new Rectangle(5); shapes[3]=new Rectangle(7);
for(Shape S : shapes) { if(S instanceof Rectangle) { result+=S.getArea(); } } System.out.println("The total area of Rectangle : " +result); }
CAN YOU PLEASE HELP ME WITH THE SECOND PART (the post lab I mean )
(in java programing language) please help thanks,
Lab 7: Interfaces Given the following interface: interface: Shape 1 interface Shape Requirements i. Write the two classes: Rectangle and Triangle with suitable constructors and any needed ii. Implement method getArea on both classes such that its return the following double getArea); instance variables such that both implement Shape interface. Area of Rectangle - side side Area of Triangle : % * base* height ii. Create test class and do the following: Create an array of type Shape, which contains two instances of Rectangle and two of Triangle. a. b. Go over the whole array to calculate total areas of Rectangle. Then print the result Note: use instanceof operator Post-Lab: i. Update Shape interface such that it has in addition the following: a. Final char with name SYMBOL that's hold a symbol of your choice (eg. @, s. %, etc..) b. A default method to print a message of your choice c. Abstract method draw which is draw the shape by using the value of SYMBOL. Example Rectangle Symbol ' and side 4 Triangle Symbol = 'S' and base-5 s$ s$$ Ssss SSss d. A toString method to return the name of the shape followed by symbol, then the area calculated by method getArea. ii. Create test class and do the following: Create an array of type Shape, which contains four instances of Rectangle and four of Triangle Test the default method as well as toString method on one of the shapes. Go over the whole array to draw all rectangles with side greater than 3 and triangles with base less than 5 a. b. cStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started