import java.util.Scanner;
//定义接口Shape
interface Shape{
double area();
double perimeter();
}
//定义Circle类
class Circle implements Shape{
int radius;
public Circle(){
this.radius=1;
}
public Circle(int r){
this.radius=r;
}
public double area(){
return radius*radius*Math.PI;
}
public double perimeter(){
return 2*Math.PI*radius;
}
}
//定义Rectangle类
class Rectangle implements Shape{
int width;
int length;
public Rectangle(){
this.length=1;
this.width=1;
}
public Rectangle(int w,int l){
this.length=l;
this.width=w;
}
public double area(){
return width*length;
}
public double perimeter(){
return 2*(width+length);
}
}
class Show{
public static void showArea(Shape s){
System.out.println(s.area());
}
public static void showPerimeter(Shape s){
System.out.println(s.perimeter());
}
}
public class APP {
public static void main(String[] args) {
int n;
Scanner scan=new Scanner(System.in);
n=scan.nextInt();
Shape s;
switch(n){
case 1:
System.out.println(new Circle().area());
break;
case 2:
System.out.println(new Circle(2).perimeter());
break;
case 3:
System.out.println(new Rectangle().area());
break;
case 4:
System.out.println(new Rectangle(3,4).perimeter());
break;
case 5:
s=new Circle();
System.out.println(s.area());
break;
case 6:
s=new Circle(2);
Show.showArea(s);
Show.showPerimeter(s);
break;
case 7:
s=new Rectangle();
System.out.println(s.area());
case 8:
s=new Rectangle(2, 6);
Show.showArea(s);
Show.showPerimeter(s);
break;
case 9:
s=new Rectangle();
Show.showArea(s);
Show.showPerimeter(s);
break;
case 10:
System.out.println("compile pass!");
}
scan.close();
}
}
Java Shape类
最新推荐文章于 2023-04-22 20:39:55 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)