Java练习题求解

来源:百度知道 编辑:UC知道 时间:2024/05/31 19:21:15
2、根据课堂PPT上的图示的类的结构和给定代码框架TestShape,完成该下面的程序:
abstract class Shape {
protected int x, y, k;
protected double m;
abstract public double getPerimeter();
abstract public double getArea();
public void display(){
System.out.print("The perimeter="+getPerimeter()+"\t and the area="+getArea());
}
}
class Circle extends Shape{ // 完成类的定义
Circle(int r){
// 写语句
}
public double getPerimeter(){
// 写语句
}
public double getArea(){
// 写语句
}
public void display(){
// 写语句
}
}
class Square extends Shape{
Square(int a, int b){
// 写语句
}
public double getPerimeter(){
// 写语句
}
public double getArea(){
// 写语句
}
public void display(){
System.out.println("矩形边长分别为"+x+"和"+y+":");
super.display();
System.out.println();System.out.println();

abstract class Shape {
protected int x, y, k;
protected double m;

abstract public double getPerimeter();

abstract public double getArea();

public void display() {
System.out.println("The perimeter=" + getPerimeter() + "\t and the area="
+ getArea());
}
}

class Circle extends Shape { // 完成类的定义
Circle(int r) {
m=r;
}

public double getPerimeter() {
return 2*3.14*m;
}

public double getArea() {
return 3.14*m*m;
}

public void display() {
System.out.println("圆的半径是:"+m);
super.display();
}
}

class Square extends Shape {
Square(int a, int b) {
x=a;
y=b;
}

public double getPerimeter() {
return 2*(x+y);
}

public double getArea() {
return x*y;
}

public void display() {