2016-04-08 8 views
0

Ich suche weiter, wie dies zu beheben ist, aber ich kann nicht finden, was für mein Problem gilt/funktioniert. Ich versuche ein Array von Circles auszudrucken. Alles was ich weiß ist, dass ich die toString() Methode überschreiben muss.System.out.print (Arrays.toString()); funktioniert nicht für Array von OBJEKTEN

Ich halte die folgende Ausgabe bekommen:

[Heather $ Kreis @ 2a139a55, Heather $ Kreis @ 15db9742, Heather $ Kreis @ 6d06d69c, Heather $ Kreis @ 7852e922]

import java.util.*; 
public class heather { 
    public static void main(String[] args) { 
     heather c = new heather(); 

     Circle c1 = c.new Circle(4,6,4); 
     Circle c2 = c.new Circle(4,5,4); 
     Circle c3 = c.new Circle(5,4,4); 
     Circle c4 = c.new Circle(5,4,3); 

     Circle[] a = {c1, c2, c3, c4}; 

     Arrays.sort(a); 
     System.out.print(Arrays.toString(a)); 
    } 

    public class Point { 
     private int x; 
     private int y; 

     public Point(int x, int y){ 
      this.x = x; this.y = y; 
     } 

     public int getX(){ 
      return this.x; 
     } 

     public int getY(){ 
      return this.y; 
     } 
    } 

    public class Circle extends Point implements Comparable<Circle> { 
     private double radius; 
     private Point point; 

     public Circle(int x, int y, double radius) { 
      super(x, y); this.radius = radius; 
     } 

     public double getRadius() { 
      return this.radius; 
     } 

     public Point getPoint() { 
      return this.point; 
     } 

     public int area() { 
      return (int) (Math.PI*radius*radius); 
     } 

     public int compareTo(Circle other){ 
      if(this.area()>other.area()) { 
       return 1; 
      } 

      if(this.area()<other.area()) { 
       return -1; 
      } else if(this.getX()>other.getX()) { 
       return 1; 
      } 

      if (this.getX()<other.getX()){ 
       return -1; 
      } else if(this.getY()<other.getY()) { 
       return -1; 
      } else { 
       return 1; 
      } 
     } 
    } 
    public String toString(){ 

    } 
} 
+2

Sie müssen – 3kings

+0

im 'toString' Methode füllen geschrieben gereinigt Ich weiß, es ausgefüllt werden muss. Aber ich weiß nicht, was nach innen zu setzen es. Ich habe versucht, getter Methoden und Sachen, aber ich bekomme immer rote Unterstreichungen. –

+0

Umm so setzen vielleicht .... 'return" Radius: "+ Radius +" || X: "super.getX() +" || Y: "+ super.getY();' Es kommt wirklich darauf an, was Sie möchte, dass es ausgedruckt wird, wenn Sie es anordnen. – 3kings

Antwort

4

Dieser Teil des Codes ...

} 
public String toString(){ 

} 

schließt die Circle Klasse, bevor es Ihre toString() Methode enthält. Daher sollten Sie es neu schreiben, wie ...

public String toString(){ 

    } 
} 

und dann füllen Sie einfach, was Sie in der toString() Methode wollen. Vielleicht so etwas wie ...

Sie können feststellen, dass diese Probleme leichter erkannt werden, wenn Sie Ihren Code aktiv zur Lesbarkeit organisieren. Ich habe den Code, den Sie als Referenz ...

package Shift; 

import java.util.*; 

public class Shift { 

    public static void main(String[] args) { 

     Shift c = new Shift(); 

     Circle c1 = c.new Circle(4,6,4); 
     Circle c2 = c.new Circle(4,5,4); 
     Circle c3 = c.new Circle(5,4,4); 
     Circle c4 = c.new Circle(5,4,3); 


     Circle[] a = {c1, c2, c3, c4}; 

     Arrays.sort(a); 
     System.out.print(a[0]); 
    } 



public class Point{ 

    private int x; 
    private int y; 

    public Point(int x, int y){ 
     this.x = x; this.y = y; 
    } 

    public int getX(){ 
     return this.x; 
    } 

    public int getY(){ 
     return this.y; 
    } 
} 



public class Circle extends Point implements Comparable<Circle>{ 

    private double radius; 
    private Point point; 

    public Circle(int x, int y, double radius) { 
      super(x, y); 
      this.radius = radius; 
    } 

    public double getRadius(){ 
     return this.radius; 
    } 

    public Point getPoint(){ 
     return this.point; 
    } 

    public int area(){ 
     return (int) (Math.PI*radius*radius); 
    } 

    public int compareTo(Circle other){ 
     if(this.area()>other.area()) 
      return 1; 


     if(this.area()<other.area()) 
      return -1; 
     else if(this.getX()>other.getX()) 
       return 1; 


     if (this.getX()<other.getX()) 
       return -1; 
     else if(this.getY()<other.getY()) 
      return -1; 
     else 
      return 1; 
    } 

    @Override 
    public String toString(){ 
     return "Circle of radius " + radius; 
    } 
} 


} 
+0

Ich habe es repariert, bevor ich deine Antwort gesehen habe, aber danke für die Hilfe. –

+0

@heatherwrieklestein Super! Besser, dass du es zuerst gesehen hast! Siehe oben den bereinigten Code. – Zulfe