2. 定义一个抽象类shape,它有两个抽象方法 area() 和 perimeter(), 定义一个圆和一个正方形,分别集成shape, 定义一个主类,在其中计算圆和正方形的面积与周长。
最好能够带上运行界面的截图,谢谢
第一题
import java.util.Scanner;
public class First
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("请输入一个数字");
int n=in.nextInt();
System.out.println(n+"的阶乘是:"+factorial(n));
}
public static int factorial(int m)
{
if(m==1)
return(1);
else
return(factorial(m-1)*m);
}
}
第二题
import java.util.Scanner;
public class Second
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("请输入圆的半径:");
double R=in.nextDouble();
yuan g=new yuan(R);
System.out.println("圆的周长是:"+g.perimeter());
System.out.println("圆的面积是:"+g.area());
System.out.println("请输入正方形的边长:");
double B=in.nextDouble();
zhengfangxing z=new zhengfangxing(B);
System.out.println("正方形的周长是:"+z.perimeter());
System.out.println("正方形的面积是:"+z.area());
}
}
class yuan extends shape
{
final double PI=3.1415926;
double r,area,perimeter;
public yuan(double R)
{
r=R;
}
public double area()
{
area=PI*Math.pow(r,2.0);
return area;
}
public double perimeter()
{
perimeter=2*PI*r;
return perimeter;
}
}
class zhengfangxing extends shape
{
double l,area,perimeter;
public zhengfangxing(double L)
{
l=L;
}
public double area()
{
area=l*l;
return area;
}
public double perimeter()
{
perimeter=4*l;
return perimeter;
}
}
abstract class shape
{
public abstract double area();
public abstract double perimeter();
}
十分感谢。。。。可是第二题呢,还要运行结果的截图。。。