JAVA编写一个Application程序。包含类Point、Circle和Test

编写一个Application程序。包含类Point、Circle和Test,具体要求如下:
⑴ 类Point (表示一个点的类),主要包括:
① 成员变量int x 和 int y;② 构造方法 Point ( ) 用来初始化变量x和y。
(2) 类Circle(圆类),主要包括:
① 成员变量Point p(即代表圆心的点)和float r(即圆的半径);② 构造方法Circle ( ) 用来初始化成员变量;③ 方法 getArea ( ) 与 getRound ( ) 分别用来返回圆的面积和周长。
(3) 类Test(主类),其中有一个main方法,在main方法中,执行以下操作:
① 创建一个Point类的对象p1,p1的横坐标和纵坐标分别为:10、20。创建一个圆心为p1,半径为15的Circle类的对象c1
② 调用 c1.getArea() 方法来计算对象c1的面积,并输出结果;
④ 调用 c1.getRound () 方法来计算对象c1的周长,并输出结果。

class Point {
int x;
int y;
Point(int xx,int yy)
{ 
   Scanner sc = new Scanner(System.in);
xx=sc.nextInt;
yy=sc.nextInt;
x=xx;y=yy;

}

          class Circle extends Point()
{   
  float r;
  Point p;
  Circle(Point pp,float r)
{
  Scanner sc = new Scanner(System.in);
rr=sc.nextInt;
    r=rr;

};
  public getArea (){return 3.14*r*r};
  public getRound (){return 2*3.14*r};
 }

public class Test{
public static void main(String[] args)
{
   Point p1= new Point (10,20);
   Circle c1=new Circle ( p1,15);
System.out.println(c1.getArea());
System.out.println(c1.getRound ());


}


  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个可能的解决方案: Point: public class Point { private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } public String toString() { return "(" + x + ", " + y + ")"; } } Circle: public class Circle { private Point center; private double radius; public Circle(Point center, double radius) { this.center = center; this.radius = radius; } public Point getCenter() { return center; } public double getRadius() { return radius; } public void setCenter(Point center) { this.center = center; } public void setRadius(double radius) { this.radius = radius; } public double getArea() { return Math.PI * radius * radius; } public String toString() { return "Circle with center " + center + " and radius " + radius; } } Cylinder: public class Cylinder extends Circle { private double height; public Cylinder(Point center, double radius, double height) { super(center, radius); this.height = height; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getVolume() { return getArea() * height; } public String toString() { return "Cylinder with center " + getCenter() + ", radius " + getRadius() + " and height " + height; } } 测试: public class Test { public static void main(String[] args) { Point p = new Point(1, 2); Circle c = new Circle(p, 3); Cylinder cyl = new Cylinder(p, 3, 4); System.out.println(p); System.out.println(c); System.out.println(c.getArea()); System.out.println(cyl); System.out.println(cyl.getVolume()); } } 运行测试将输出以下内容: (1.0, 2.0) Circle with center (1.0, 2.0) and radius 3.0 28.274333882308138 Cylinder with center (1.0, 2.0), radius 3.0 and height 4.0 339.29200658769764

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值