System.out.print(Arrays.toString()); is not working for array of OBJECTS

I keep searching on how to fix this, but I cannot find something that applies/works for my problem. I am trying to print out an array of Circles. All I know is that I have to overwrite the toString() method.

I keep getting the following output:

[Heather$Circle@2a139a55, Heather$Circle@15db9742, Heather$Circle@6d06d69c, Heather$Circle@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(){

    }
}

Upvotes: 0

Views: 1936

Answers (1)

Zulfe
Zulfe

Reputation: 881

This part of your code...

    }
public String toString(){

}

closes the Circle class before it includes your toString() method. Therefore, you should rewrite it like...

    public String toString(){

    }
}

and then just fill in whatever you want in the toString() method. Maybe something like...

return "Circle of radius " + radius; 

You may find that these issues are more easily detected if your actively organize your code for readability. I've cleaned the code you posted for reference...

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; 
    }
}


}

Upvotes: 4

Related Questions