Results 1 to 2 of 2
  1. #1
    bradoz is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default comparing cloned objects

    Hello,
    i'm not sure where or what i have done wrong here.

    What i am trying to do here is compared two circles and return the details of the circles and compare them to see if they are equal.

    my driver class
    Java Code:
    import java.util.*;
    
    public class TestCircle {
          public static void main(String[] args) {
    
    
               Circle circ = new Circle(5.0, 2, 3, 4);
    
               Circle circ1 = (Circle)circ.clone();
    
               if (circ1 ==(circ)){
        		System.out.println("The two circles are the same.");
        	} else {
        		System.out.println("The two circles are not the same");
        	}
            System.out.println();
        	System.out.println("Circle 1=" + circ);
        	System.out.println("Circle 2 " + circ1);
    
    
            System.out.println("The area of the circle of radius " + circ.radius + " is " + circ.getArea());
            //System.out.println("The Colour of my circle is " + myCircle.red + myCircle.green + myCircle.blue );
    
            Circle yourCircle = new Circle();
            System.out.println("The area of the circle of radius " + yourCircle.radius + " is " + yourCircle.getArea());
          }
    }

    My Circle class

    Java Code:
    public class Circle implements Cloneable {
    
    	private double radius; //This primitive type member can be automatically cloned
    	private int red; //This primitive type member can be automatically cloned
    	private int green; //This primitive type member can be automatically cloned
    	private int blue; //This primitive type member can be automatically cloned
    
    	private int[] color = {0, 0, 0}; //for RBG
    
        public Circle(double rad, int r, int g, int b) {
               radius = rad;
               red = r;
               green = g;
               blue = b;
        }
        public Circle(double newRadius) {
               radius = newRadius;
        }
        public double getArea() {
            return radius * radius * Math.PI;
        }
    
        public Object clone(){
        	try{
        		Circle cloned = (Circle)super.clone();
        		//Makes a copy of all primitive type members;
    
             return cloned;
        	}
        	catch(CloneNotSupportedException e){
             System.out.println(e);
             return null;
        	}
    
        public boolean equals(Object obj)
    {
         	if (obj instanceof Circle)
         	{
         		Circle newCircle = (Circle) obj;
         		return (newCircle.radius == radius) && (newCircle.red ==red) && (newCircle.green ==green) && (newCircle.blue ==blue ) );
           	//Way of comparison
          }
          else
             	return false;
    }
    
    public String toString( )
    {
       return "[Circle with Radius" + radius+ ", Red=" + red+ ", green=" + green+ ", blue=" + blue+ "]";
       	//return circle characteristics;
    }
    
    
    
    }
    
    }

    the errors that i am getting is:

    Circle.java:35: illegal start of expression
    public boolean equals(Object obj)
    ^
    Circle.java:35: ';' expected
    public boolean equals(Object obj)
    ^
    Circle.java:35: ';' expected
    public boolean equals(Object obj)
    ^
    Circle.java:40: ';' expected
    return (newCircle.radius == radius) && (newCircle.red ==red) && (newCircle.green ==green) && (newCircle.blue ==blue ) );
    ^
    Circle.java:47: illegal start of expression
    public String toString( )
    ^
    Circle.java:47: ';' expected
    public String toString( )
    ^


    I dont understand why i'm getting these error messages and where i have gone wrong.

    Any help is much appreciated

    thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: comparing cloned objects

    Count your curly brackets; they don't match ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 02-13-2012, 12:48 PM
  2. Comparing two objects
    By colerelm in forum Advanced Java
    Replies: 3
    Last Post: 12-06-2011, 03:07 AM
  3. Comparing objects: lists and maps
    By LucasH in forum Advanced Java
    Replies: 7
    Last Post: 05-24-2011, 07:41 PM
  4. Issues with comparing two generic objects
    By FOUAD-SPIDER in forum Advanced Java
    Replies: 0
    Last Post: 05-06-2011, 05:39 AM
  5. Homework help involving comparing Color objects to Strings
    By SergeantJoKer in forum New To Java
    Replies: 12
    Last Post: 09-25-2010, 07:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •