题目显示不全,完整题目描述:
(1)定义闭合图形抽象类ClosedFigure定义属性:1.形状;2.定义构造方法,给形状赋值;3.定义两个抽象方法:计算面积和计算周长;4.定义一个显示方法:显示图像形状,周长,面积;(2)定义ClosedFigure的子类椭圆Ellipse定义属性:1.长短轴定义构造方法:初始化长短轴;2.实现从父类继承来的抽象方法;[提示:椭圆求周长公式Math.PI*(this.radius_a+this.radius_b)](3)定义ClosedFigure的子类矩形类Rectangle定义属性:1.长和宽定义构造方法:初始化长和宽;2.实现从父类继承来的抽象方法;
完整代码如下:
package naizi;
import java.util.Scanner;
//闭合图形抽象类
abstract class ClosedFigure{
public String shape;
public ClosedFigure(String shape){
this.shape=shape;
}
abstract double perimeter();
abstract double area();
public void print() //显示形状、属性、周长及面积
{
System.out.println("this is a "+this.shape+",perimeter:"+this.perimeter()+",area:"+this.area());
}
}
//定义椭圆类
class Ellipse extends ClosedFigure{
double Longaxis,Shortaxis;
public Ellipse(String shape) {
super(shape);
}
public Ellipse(double a,double b) {
this("Ellipse");
this.Longaxis=a;
this.Shortaxis=b;
}
double perimeter(){
return Math.PI*(this.Longaxis+this.Shortaxis);
}
double area(){
return Math.PI*this.Longaxis*this.Shortaxis;
}
}
//定义矩形类
class Rectangle extends ClosedFigure{
double Length,Width;
public Rectangle(String shape) {
super(shape);
}
public Rectangle(double a,double b) {
this("Rectangle");
this.Length=a;
this.Width=b;
}
double perimeter(){
return 2*(this.Length+this.Width);
}
double area(){
return this.Length*this.Width;
}
}
public class ClosedFigure_ex
{
public static void main(String args[]){
int a,b;
ClosedFigure d1;
ClosedFigure d2;
try{
Scanner sc = new Scanner(System.in);
String str1 = sc.next();
a=Integer.parseInt(str1);
str1 = sc.next();
b=Integer.parseInt(str1);
d1=new Ellipse(a,b);
d1.print();
String str2 = sc.next();
a=Integer.parseInt(str2);
str2 = sc.next();
b=Integer.parseInt(str2);
d2=new Rectangle(a,b);
d2.print();
} catch(Exception e){
System.out.println("error");
}
}
}
测试如下: