我的代码如下
package test;
public abstract class Shape {
public Shape(){
System.out.println("形状信息是:");
}
public abstract void area();
public abstract void perimeter();
}
package test;
public class Rectangle extends Shape {
private int ln=0;
private int width=0;
public Rectangle (int l, int w) {
this.width = w;
this.ln = l;
System.out.println("此长方形长是:"+this.ln);
System.out.println("此长方形宽是:"+this.width);
}
@Override
public void area() {
// TODO Auto-generated method stub
int ar=ln*width;
System.out.println("此长方形面积是:"+ar);
}
@Override
public void perimeter() {
// TODO Auto-generated method stub
int pe = 2*(ln+width);
System.out.println("此长方形的周长是:"+pe);
}
}
package test;
public class Cricle extends Shape {
int radius;
double pi=3.14;
public Cricle (int r){
this.radius=r;
System.out.println("此圆形半径是:"+this.radius);
}
@Override
public void area() {
// TODO Auto-generated method stub
double ar=pi*radius;
System.out.println("此圆形面积是:"+ar);
}
@Override
public void perimeter() {
// TODO Auto-generated method stub
double pe=2*pi*radius;
System.out.println("圆形周长是:"+pe);
}
}
package test;
import java.util.Scanner;
public class Inital {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入你要选择的形状:1.圆形 2.长方形");
int num = input.nextInt();
if(num==1){
Scanner input2 = new Scanner(System.in);
System.out.println("请输入圆形半径");
int r = input2.nextInt();
Cricle cricle = new Cricle(r);
cricle.area();
cricle.perimeter();
}
else{
Scanner input3 = new Scanner(System.in);
Scanner input4 = new Scanner(System.in);
System.out.println("请输入长方形的 长和宽");
int l = input3.nextInt();
int w = input4.nextInt();
Rectangle rectangle = new Rectangle(l,w);
rectangle.area();
rectangle.perimeter();
}
}
}