编程题:Shape and Polymorphism
We have Shape class 、Circle class and Rectangle class, that Circle class and Rectangle class inherit from Shape Class.
There is a method of getArea in Shape class . please read and finish the code as follow
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int radius = input.nextInt();
int height = input.nextInt();
int width = input.nextInt();
//shape1 refer to a Circle object
Shape shape1 =【】;
//shape2 refer to a Rectangle object
Shape shape2 = 【】;
//call getArea method with shape1 and shape2 then output
System.out.printf("the area of circle :%.2f\n",【】);
System.out.printf("the area of Rectangle :%.2f\n",【】);
}
}
class Shape{
public double getArea(){
return 0;
}
}
class Circle 【】{
private int radius;
@Override
//override method getArea
public Circle(int radius) {
super();
this.radius = radius;
}
}
class Rectangle【】{
private int height,width;
//override method getArea
【】
public Rectangle(int height, int width) {
super();
this.height = height;
this.width = width;
}
}
### 输入格式:
input radius ,height and width at a line
### 输出格式:
output the area of circle and rectangle at different lines.
### 输入样例:
在这里给出一组输入。例如:
in
5 2 3
### 输出样例:
在这里给出相应的输出。例如:
out
the area of circle :78.54
the area of Rectangle :6.00
答案:若无答案欢迎评论
There is a method of getArea in Shape class . please read and finish the code as follow
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int radius = input.nextInt();
int height = input.nextInt();
int width = input.nextInt();
//shape1 refer to a Circle object
Shape shape1 =【】;
//shape2 refer to a Rectangle object
Shape shape2 = 【】;
//call getArea method with shape1 and shape2 then output
System.out.printf("the area of circle :%.2f\n",【】);
System.out.printf("the area of Rectangle :%.2f\n",【】);
}
}
class Shape{
public double getArea(){
return 0;
}
}
class Circle 【】{
private int radius;
@Override
//override method getArea
public Circle(int radius) {
super();
this.radius = radius;
}
}
class Rectangle【】{
private int height,width;
//override method getArea
【】
public Rectangle(int height, int width) {
super();
this.height = height;
this.width = width;
}
}
### 输入格式:
input radius ,height and width at a line
### 输出格式:
output the area of circle and rectangle at different lines.
### 输入样例:
在这里给出一组输入。例如:
in
5 2 3
### 输出样例:
在这里给出相应的输出。例如:
out
the area of circle :78.54
the area of Rectangle :6.00
答案:若无答案欢迎评论