In: Computer Science
There are two errors in this code. Identify the errors and give the correct code that will make the program to display the following output:
Rectangle: height 2.0 width 4.0
Area of the Rectangle is 8.0
-----
public interface Shape {
public double getArea();
}
class Rectangle implements Shape {
double height;
double width;
public Rectangle(double height, double width) {
this.height=height;
this.width=width;
}
public double getArea() {
return height*width;
}
public String toString() {
return "Rectangle: height "+height+" width "+width;
}
}
class Main {
public static void main(String[] args) {
Shape r = new Rectangle(2.0,4.0);
System.out.println(r.toString());
System.out.println("Area of the rectangle is "+printArea(r));
}
public static double printArea(Shape s) {
if (s instance of Shape)
return s.getArea();
}
}
interface Shape {
public double getArea();
}
class Rectangle implements Shape {
double height;
double width;
public Rectangle(double height, double width) {
this.height = height;
this.width = width;
}
public double getArea() {
return height * width;
}
public String toString() {
return "Rectangle: height " + height + " width " + width;
}
}
class Main {
public static void main(String[] args) {
Shape r = new Rectangle(2.0, 4.0);
System.out.println(r.toString());
System.out.println("Area of the rectangle is " + printArea(r));
}
public static double printArea(Shape s) {
// ERROR1: instanceof is single word
if (s instanceof Shape)
return s.getArea();
//ERROR2:
//missed return value
return 0;
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME