Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    Fred1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Need Help. with two simple things

    OK I have my Circle.java that extends to GeomerticObject.java and my testCircle. the thing is i do not know how to print out the compareTO and the equals in my testCircle.java. If you can just show me one of both ill fix everything else. i attempted it as you can see but its not right :(.
    Attached Files Attached Files

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Would you mind posting the code directly instead of as attachments?

  3. #3
    Fred1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    i don't know how to do that. can you tell me. i am new to this

  4. #4
    Fred1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    //Listing 14.1
    public abstract class GeometricObject {
    private String color = "white";
    private boolean filled;
    private java.util.Date dateCreated;

    /** Construct a default geometric object */
    protected GeometricObject() {
    dateCreated = new java.util.Date();
    }

    /** Construct a geometric object with color and filled value */
    protected GeometricObject(String color, boolean filled) {
    dateCreated = new java.util.Date();
    this.color = color;
    this.filled = filled;
    }

    /** Return color */
    public String getColor() {
    return color;
    }

    /** Set a new color */
    public void setColor(String color) {
    this.color = color;
    }

    /** Return filled. Since filled is boolean,
    * the get method is named isFilled */
    public boolean isFilled() {
    return filled;
    }

    /** Set a new filled */
    public void setFilled(boolean filled) {
    this.filled = filled;
    }

    /** Get dateCreated */
    public java.util.Date getDateCreated() {
    return dateCreated;
    }

    /** Return a string representation of this object */
    public String toString() {
    return "created on " + dateCreated + "\ncolor: " + color +
    " and filled: " + filled;
    }

    /** Abstract method getArea */
    public abstract double getArea();

    /** Abstract method getPerimeter */
    public abstract double getPerimeter();
    }

  5. #5
    Fred1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    im sorry here it is


    /****************************************
    *
    * Student Name: LastName, FirstName MiddleName
    * Student Email:
    * Assignment number:
    * Date Due:
    * Date Submitted:
    * Program Name: TestCircle.java
    * Program Description: TestCircle.java
    *
    ****************************************/

    public class TestCircle {

    public static void main(String[] args) {
    Circle circle1 = new Circle(3.0);
    Circle circle2 = new Circle(10.0);
    Circle circle3 = new Circle(3.0);

    String circle1 = new String("3.0");
    String circle2 = "10.0";
    String circle3 = "3.0";

    circle1.getColor();
    circle2.getColor();
    circle3.getColor();

    circle1.isFilled();
    circle2.isFilled();
    circle3.isFilled();

    circle1.getDateCreated();
    circle2.getDateCreated();
    circle3.getDateCreated();


    System.out.println("Circle 1: radius = " + circle1.getRadius());
    System.out.println("Circle 2: radius = " + circle2.getRadius());
    System.out.println("Circle 3: radius = " + circle3.getRadius());

    System.out.println("Circle1 equals Circle 2: " + circle1.equals(circle2));
    System.out.println("Circle1 equals Circle 3: " + circle1.equals(circle3));
    System.out.println("Circle2 equals Circle 3: " + circle2.equals(circle3));

    System.out.println("Circle1 compares to Circle2: " + Max.max(circle1, circle2));
    System.out.println("Circle1 compares to Circle3: " + Max.max(circle1, circle3));
    System.out.println("Circle2 compares to Circle3: " + Max.max(circle2, circle3));




    }
    }

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    What did you want the printed output to look like?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I apologize, I forgot to include how to use code tags
    [code ]<-omit space
    YOUR CODE HERE
    [/code]

    Also, would you mind clarifying the problem for me?

  8. #8
    Fred1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    ok. What i was trying to do is have the equals() print True or False based off of the comparison of the three circles. Same thing with the compareTo but instead of the True/False i was trying to get that to print 1, -1, 0

  9. #9
    Fred1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Java Code:
    
    /**************************************** 
    * 
    * Student Name: LastName, FirstName MiddleName
    * Student Email: 
    * Assignment number: 
    * Date Due: 
    * Date Submitted: 
    * Program Name: Circle.java
    * Program Description: Circle.java
    * 
    ****************************************/ 
    
    
    public class Circle extends GeometricObject
    {
    			
    			
      	private double radius;
      	
      	public boolean equals(Circle Circle1)
      {
        return this.radius == Circle1.getRadius();
      }
      	
    	
    	
      	public int compareTo(Object o)
      		{
      		if (getRadius() > ((Circle)o).getRadius())
      			return 1;
      		else if (getRadius() < ((Circle)o).getRadius())
      			return -1;
      		else 
      			return 0;
      		}
    	
      	
    	
    	//Keep methods from Listing 14.2
      	public Circle() {
      	}
    
      	public Circle(double radius) {
        	this.radius = radius;
      	}
    
      	/** Return radius */
      	public double getRadius() {
        	return radius;
     	}
    
      	/** Set a new radius */
      	public void setRadius(double radius) {
        	this.radius = radius;
      	}
    
      	/** Return area */
      	public double getArea() {
    		System.out.println("getArea() in Circle class.");
        	return radius * radius * Math.PI;
      	}
    
      	/** Return diameter */
      	public double getDiameter() {
        	return 2 * radius;
      	}
    
      	/** Return perimeter */
      	public double getPerimeter() {
    	  	System.out.println("getPerimeter() in Circle class.");	  
        	return 2 * radius * Math.PI;
      	}
    
     	/* Print the circle info */
      	public void printCircle() {
        	System.out.println("The circle is created " + getDateCreated() +
          	" and the radius is " + radius);
      	}
    }

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Are you getting errors? I believe to have a compareTo method you need to implement comparable(or comparator, double check which one it is). The code looks good otherwise.

  11. #11
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    There is no 'Max' class. Math.max(..) returns the larger of the two arguments. For compare to you should call the compareTo method, e.g. circle1.compareTo(circle2). You don't need to implement Comparable just for this (although you might just as well).

  12. #12
    Fred1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    i tried to do that with compareTO ill try it again ....and i have to create another class?

  13. #13
    Fred1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    o ok nevermind i see. thats what i did for equals to but it do not work

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The compareTo method can be simplified to use the built in integers compareTo method.
    Java Code:
    Circle c = (Circle)o;
    return radius.compareTo(c.getRadius());
    notice I referred to radius directly. You can do this because you are referring to this, which is the class you are in. You only have to use getters on objects, it's safe to access the variables of the class like the call to radius.

    You don't have to make a new class, it may be helpful to implement comparator, although I suppose it's not necessary.

  15. #15
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Fred1 View Post
    i tried to do that with compareTO ill try it again
    The method is 'compareTo' not 'compareTO'.

    ....and i have to create another class?
    Er, no, you don't have to... Comparable is an interface that declares the compareTo method. Useful for storing objects in ordered collections, or sorting lists of objects, and so-on. Your Circle (or GeometricObject) class could implement it (you already have the compareTo method in place), but I wouldn't worry about it for now.

  16. #16
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by sunde887 View Post
    The compareTo method can be simplified to use the built in integers compareTo method.
    Good point - easy to forget.

  17. #17
    Fred1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    When i put this in my class circle it give me this error
    double cannot be dereferenced

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I try to be as lazy as possible with comparing stuff if I can. Fred would you mind showing memthe code that caused the error?

  19. #19
    Fred1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    ok well i used the circle1.compareTo(circle2). it works but its not giving me the -1, 1, 0 . what should i do? is it my code in my circle class or the System.out.print in the testcircle thats giving me the regular doubles.

  20. #20
    Fred1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    this is what i did sunny but dlorde said it was fine i just changed it back

    Java Code:
    
    
    public class Circle extends GeometricObject
    {
    			
    			
      	private double radius;
      	
      	public boolean equals(Circle Circle1)
      {
        return this.radius == Circle1.getRadius();
      }
      	
    	
    	
      	public int compareTo(Object o)
      		{
      		Circle c = (Circle)o;
    		return radius.compareTo(c.getRadius());
      		}
    	
      	
    	
    	//Keep methods from Listing 14.2
      	public Circle() {
      	}
    
      	public Circle(double radius) {
        	this.radius = radius;
      	}
    
      	/** Return radius */
      	public double getRadius() {
        	return radius;
     	}
    
      	/** Set a new radius */
      	public void setRadius(double radius) {
        	this.radius = radius;
      	}
    
      	/** Return area */
      	public double getArea() {
    		System.out.println("getArea() in Circle class.");
        	return radius * radius * Math.PI;
      	}
    
      	/** Return diameter */
      	public double getDiameter() {
        	return 2 * radius;
      	}
    
      	/** Return perimeter */
      	public double getPerimeter() {
    	  	System.out.println("getPerimeter() in Circle class.");	  
        	return 2 * radius * Math.PI;
      	}
    
     	/* Print the circle info */
      	public void printCircle() {
        	System.out.println("The circle is created " + getDateCreated() +
          	" and the radius is " + radius);
      	}
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Couple hopefully simple things.
    By drucey in forum New To Java
    Replies: 10
    Last Post: 11-18-2010, 08:44 PM
  2. Checking if 2 things are false
    By ile4 in forum New To Java
    Replies: 4
    Last Post: 11-16-2010, 10:40 AM
  3. Replies: 28
    Last Post: 09-15-2010, 10:21 AM
  4. XML, and other things.
    By Tortex in forum New To Java
    Replies: 5
    Last Post: 03-28-2010, 05:53 PM
  5. so whats going on? (things aren't showing up)
    By Adrien in forum AWT / Swing
    Replies: 9
    Last Post: 02-20-2010, 07:22 PM

Posting Permissions

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