Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33
  1. #21
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Fred1 View Post
    When i put this in my class circle it give me this error
    double cannot be dereferenced
    You need to use the Double class rather than the primitive double. With autoboxing, the conversion is just a simple cast these days:
    Java Code:
    Circle c = (Circle)o;
    return ((Double)radius).compareTo(c.getRadius());

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

    Default

    Quote Originally Posted by dlorde View Post
    You need to use the Double class rather than the primitive double. With autoboxing, the conversion is just a simple cast these days:
    Java Code:
    Circle c = (Circle)o;
    return ((Double)radius).compareTo(c.getRadius());
    Good catch, I completely missed that.

    As clarification for the op, the double primitive is a primitive, you can't call methods on it, however; the Double class creates an object which allows you to call methods on a primitive with a simple cast, as shown above.

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

    Default

    But on my testcircle to print out true and false for the equals method
    Java Code:
    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));
    it gives me this error
    circle1 is already defined in main(java.lang.String[])

    and the same for circle 2 and 3 . if i put .equals(circle1) that do not work either so i dont know what to do from here

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

    Default

    Sorry, I keep reading that as "my testicle" :)

    I'm going to bed...

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

    Default

    lol wow but its 8:30 here what time is it over there

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

    Default

    Quote Originally Posted by dlorde View Post
    Sorry, I keep reading that as "my testicle" :)

    I'm going to bed...
    Lol, goodnight.

    @op, would you mind posting the entire main method again?

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

    Default

    Java Code:
    //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();
    }
    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 ((double)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);
      	}
    }



    Java Code:
    /**************************************** 
    * 
    * 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: " + .equals(circle1));
    			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: " + circle1.compareTo(circle2));
    			System.out.println("Circle1 compares to Circle3: " + circle1.compareTo(circle3));
    			System.out.println("Circle2 compares to Circle3: " + circle2.compareTo(circle3));
    		
    		
    		
    			
    	}
    }
    i need to get my equals to work .

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

    Default

    System.out.println("Circle1 equals Circle 2: " + .equals(circle1));

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

    Default

    In your main method, you declare three circles with the names circle1, circle2 and circle3, then you declare 3 strings with the same names. The compiler throws a fit when it sees this. Change the strings to has different names. Also, in our compareTo method you need to cast so Double, not double.

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

    Default

    wow :) thank you guys for helping me. Dlorde sorry for keeping you up. sunde thanks for taking the time out to help. one last thing how do i make UMLs for these?

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

    Default

    Umls are basically just charts which show the instance variables and methods a class contains and it shows dependencies as well. I suggest you google around to see some examples.

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

    Default

    ok but is there like a website to go to

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

    Default

    Not that I know off the top of my head, dream in code may have some uml stuff, also stack overflow may, but these are just guesses. I haven't done any uml so I don't really know sites for it.

Page 2 of 2 FirstFirst 12

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
  •