程序使用类点类圈圈java

时间:2015-10-03 16:01:34

标签: java

创建具有两个私有成员的Point类:int x和int y,表示点的坐标。 Class Point有两个构造函数:

  1. Point()将零分配给变量x和y,以及
  2. Point(int x,int y),分别将值x和y分配给变量this.x和this.y。
  3. 另外,编写一个成员方法public void move(int x,int y),它将值x和y赋给私有成员并覆盖方法public String toString(),它返回Point的字符串表示形式(x +“”+ y )。

    创建具有两个私有成员的类Circle:int radius和Point center。写下以下的counstructors:

    1. Circle() - 假设默认半径为1且中心(0,0)
    2. Circle(int radius) - 为this.radius指定整数半径(中心位于0,0)
    3. 圆(点中心) - 将点中心指定给this.center(半径1)
    4. Circle(int radius,Point center) - 为this.radius分配整数半径并将this point指向this.center
    5. 另外,编写以下成员方法:

      1. double area() - 计算圆圈区域
      2. double circumference() - 计算圆周长度
      3. String toString() - 返回圆的半径和中心的字符串表示。
      4. 编写一个驱动程序类Lab2.java,它实例化Point和Circle类型的几个对象(至少4个,使用4个不同的构造函数)并测试所有成员方法。

        class Point {
        
            private int x; 
            private int y;
        
            public Point(){
                x = y = 0; 
            }
        
            public Point(int x, int y){
                this.x = x; 
                this.y = y; 
            }
        
            public void move (int x, int y){
                this.x = x; 
                this.y = y; 
            }
        
            public String toString(){
                String s = "x: " + x + "y: " + y; 
                return s; 
            }
        }
        
        class Circle{
            private int radius; 
            private Point center; 
        
            public Circle(){
                int radius = 1; 
                Point center = new Point(0,0);
            }
        
            public Circle(int radius){ 
                int radius = this.radius(center(0,0));  
            }
        
            public Circle(Point center){ 
                Point center = this.center(radius (1));   
            } 
        
            public Circle(int radius, Point center){
                int radius = this.radius; 
                Point center = this.center; 
            }
        
            double area() {
                return Math.PI*radius*radius; 
            }
        
            double circumference(){
                return 2*Math.PI*radius; 
            }
        
            String toString( ){
                String s = "radius: " + radius + "center: " + center; 
                return s; 
            }
        }
        
        class Lab21{ 
            public static void main(String[] args){
                Circle c = new Circle (); 
                System.out.println(c); 
                c.move(4,2); 
                System.out.println(c); 
                c.resize(5,2); 
                System.out.println(c); 
        
                point center = new Point(3,3); 
                Circle s = new Circle (6, 1, center); 
                System.out.println(s); 
            }
        }
        

        这些是我目前得到的错误:

        Lab21.java:37: error: variable radius is already defined in constructor Circle(int)
            int radius = this.radius(center(0,0));  
                ^
        Lab21.java:37: error: cannot find symbol
            int radius = this.radius(center(0,0));  
                                     ^
          symbol:   method center(int,int)
          location: class Circle
        Lab21.java:41: error: variable center is already defined in constructor Circle(Point)
            Point center = this.center(radius (1));   
                  ^
        Lab21.java:41: error: cannot find symbol
            Point center = this.center(radius (1));   
                                       ^
          symbol:   method radius(int)
          location: class Circle
        Lab21.java:45: error: variable radius is already defined in constructor Circle(int,Point)
            int radius = this.radius; 
                ^
        Lab21.java:46: error: variable center is already defined in constructor Circle(int,Point)
            Point center = this.center; 
                  ^
        Lab21.java:57: error: toString() in Circle cannot override toString() in Object
        String toString(){
               ^
          attempting to assign weaker access privileges; was public
        Lab21.java:67: error: cannot find symbol
            r.move(4,2); 
            ^
          symbol:   variable r
          location: class Lab21
        Lab21.java:69: error: cannot find symbol
            r.resize(5,2); 
            ^
          symbol:   variable r
          location: class Lab21
        Lab21.java:72: error: cannot find symbol
            point center = new Point(3,3); 
            ^
          symbol:   class point
          location: class Lab21
        

1 个答案:

答案 0 :(得分:0)

错误主要在你的Circle课程中。我认为你需要一些澄清:

  1. 属性radiuscenter已在类的开头声明。您不应该在构造函数中再次声明它们。声明变量意味着指定它的类型,例如int radiusPoint center

  2. 如果你调用radius(1),java认为你正在寻找一个名为radius的方法,并将值1传递给它。但是你没有定义一个名为radius或center的方法。 center只是一个点,radius只是一个整数。

  3. 要为它们指定值,您需要执行类似

    的操作
    this.radius = 1;
    this.center = new Point(0, 0);
    

    这就是说,给这个圆的半径赋值为1,并在(0,0)处指定一个新的Point作为这个圆的中心。

    1. 在构造函数中,您希望为属性指定值。您可以通过调用this.radius来访问您的属性,这意味着Circle的当前实例的半径。您似乎要做的是为名为radius的新变量赋值。
    2. 因此默认构造函数应如下所示:

      public Circle(){
          this.radius = 1; 
          this.center = new Point(0, 0);
      }
      

      您告诉它将此圆的半径设置为值1,并将此圆的中心设置为(0,0)处的点。

      其他构造函数的定义类似。例如,指定半径和中心的构造函数应如下所示:

      public Circle(int radius, Point center){
          this.radius = radius; 
          this.center = center; 
      }
      

      这就是说,将这个圆的半径设置为我调用方法时指定的半径,并将此圆的中心设置为我指定的中心。

      这应该是这样的:

      class Circle {
          private int radius; 
          private Point center; 
      
          public Circle(){
              this.radius = 1; 
              this.center = new Point(0,0);
          }
      
          public Circle(int radius){ 
              this.radius = radius;
              this.center = new Point(0,0);
          }
      
          public Circle(Point center){ 
              this.radius = 1;
              this.center = center;   
          } 
      
          public Circle(int radius, Point center){
              this.radius = radius; 
              this.center = center; 
          }
      
          double area() {
              return Math.PI * radius * radius; 
          }
      
          double circumference(){
              return 2 * Math.PI * radius; 
          }
      
          public String toString( ){
              String s = "radius: " + radius + "center: " + center; 
              return s; 
          }
      }
      

      主要方法中存在一些小错误。

      <强>首先,

      point center = new Point(3,3);
      

      应该是

      Point center = new Point(3, 3);
      

      如果你这样写,java就会试图找到一个名为point的类,然后抱怨,因为它无法找到它。您需要告诉它查找Point,使用大写字母P。

      <强>其次,

      Circle s = new Circle (6, 1, center);
      

      应该是

      Circle s = new Circle (6, center);
      

      您没有带三个参数的构造函数。您只能为半径指定一个值,为中心指定一个值。

      最后,你不能打电话

      c.move(4,2);
      c.resize(5,2);
      

      第一种方法适用于Point类型的对象。你不能告诉Circle像点一样。第二种方法甚至没有定义。