1. 编写一个完整的Java Application 程序.包含抽象类Shape,MyCircle类 10
⑴抽象类Shape:
double getArea():求一个形状的面积
double getPerimeter ():求一个形状的周长
⑵类 MyCircle:
由Shape类派生的具体类,除实现Shape类中的方法外,另有以下属性和方法:
① 属性
radius: double类型,表示圆的半径
② 方法
MyCircle(double r):构造函数
toString()方法:输出圆的描述信息,如“radius=1.0, perimeter=6.28, area=3.14”
求完整的代码!!! 展开
import java.util.*;
public abstract class Shape{
public abstract double getArea();
public abstract double getPerimeter();
}
public class MyCircle extends Shape{
private double radius;//圆的半径
public static final double PI=3.14;
public MyCircle(){
}
public MyCircle(double r){
this.radius=r;
}
public void setRadius(double radius){
this.radius=radius;
}
public double getRadius(){
return radius;
}
public double getArea(){
return PI*this.radius*this.radius;
}
public double getPerimeter(){
return 2*PI*this.radius;
}
public void toString(){
System.out.println("radius="+this.radius+",perimeter="+this.getPerimeter()+",area="+this.getArea());
}
}
public class Test{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
MyCircle circle=new MyCircle();
System.out.println("请输入圆的半径:");
double r=input.nextDouble();
circle.setRadius(r);
circle.toString();
}
}
还有你的分呢
求大神指导一下代码!!!
import java.text.DecimalFormat;
abstract class Shape {
public abstract double getArea();
public abstract double getPerimeter();
}
class MyCircle extends Shape {
private double radius;
public MyCircle() {
}
public MyCircle(double r) {
this.radius = r;
}
@Override
public double getArea() {
return Math.PI * this.radius * this.radius;
}
@Override
public double getPerimeter() {
return Math.PI * 2 * this.radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public String toString() {
DecimalFormat df = new DecimalFormat("#.00");
return "radius=" + getRadius() + ", perimeter="
+ df.format(getPerimeter()) + ", area=" + df.format(getArea());
}
}
public class Test {
public static void main(String[] args) {
MyCircle circle = new MyCircle(1.0d);
System.out.println(circle);
}
}