Complete the implementation of the following program. You should be able to figure out what's needed in each class based
Posted: Thu May 05, 2022 1:22 pm
Complete the implementation of the following program. You should
be able to figure out what's needed in each class based on the
driver program. (Write code in Test. java, Rectangle.java, and
Square.java)
Sample run
Input:
Output:
Given Code:
Test.java
import java.util.Scanner;
// your code starts here
// your code ends here
public class Test {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
// create a square
Shape sq = new Square(stdin.nextDouble());
System.out.println(sq.area());
System.out.println(((Rectangle)
sq).getLength());
((Rectangle)
sq).setLength(stdin.nextDouble());
System.out.println(sq.area());
System.out.println(((Rectangle)
sq).getLength());
}
}
Shape.java
package chapter9.shape;
public interface Shape {
double area();
}
Rectangle.java
package chapter9.shape;
public class Rectangle implements Shape {
// your code starts here
// your code ends here
}
Square.java
package chapter9.shape;
public class Square extends Rectangle {
public Square(double side) {
// your code starts here
// your code ends here
}
}
be able to figure out what's needed in each class based on the
driver program. (Write code in Test. java, Rectangle.java, and
Square.java)
Sample run
Input:
Output:
Given Code:
Test.java
import java.util.Scanner;
// your code starts here
// your code ends here
public class Test {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
// create a square
Shape sq = new Square(stdin.nextDouble());
System.out.println(sq.area());
System.out.println(((Rectangle)
sq).getLength());
((Rectangle)
sq).setLength(stdin.nextDouble());
System.out.println(sq.area());
System.out.println(((Rectangle)
sq).getLength());
}
}
Shape.java
package chapter9.shape;
public interface Shape {
double area();
}
Rectangle.java
package chapter9.shape;
public class Rectangle implements Shape {
// your code starts here
// your code ends here
}
Square.java
package chapter9.shape;
public class Square extends Rectangle {
public Square(double side) {
// your code starts here
// your code ends here
}
}