6 Replies - 1061 Views - Last Post: 22 January 2013 - 04:20 PM Rate Topic: -----

#1 lyates93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-January 13

Double cannot be dereferenced

Posted 22 January 2013 - 10:12 AM

Having problems comparing to objects radius... says double cannot be dereferenced. I'm new to the equals method and greaterThan method so I need some help!


 public class Circle
{
	private double radius;
	
	public Circle(double r)
	{
	
		radius=r;
		
	}
	
	public double getArea()
	{
	
		return Math.PI * radius * radius;
		
	}
	
	public double getRadius()
	{
	
		return radius;
		
	}
	
	public String toString()
	{
	
		String str = "The radius is: " + getRadius() + "and the area is: " + getArea(); ;
		
		return str;
		
	}
	
	
	public boolean equals(Circle circle1)
	{
	
		boolean status;
		
		if(getRadius.equals(circle1.getRadius()) && this.getArea().equals(circle1.getArea()))
			status=true;
		else
			status=false;
			return status;
			
	}
	
	public boolean greaterThan(Circle circle1)
	{
	
		boolean status;
			
			if(getArea().greaterThan(circle1.getArea()))
				status=true;
				
			else
				status=false;
				
		return status;
		
	}
}




Here is my demo class
public class CircleDemo
{

	public static void main(String [] args)
	{
	
	Circle circle1 = new Circle(1.44);
	Circle circle2 = new Circle(3.22);
	
	System.out.println(circle1);
	
	if(circle1.equals(circle2))
		System.out.println("Both objects have the same radius and area.");
	else
		System.out.println("The objects have different radi and areas.");
		
		
	if(circle1.greaterThan(circle2))
		System.out.println("Circle 1 is greater than circle 2.");
	else
		System.out.println("Circle 1 is not greater than circle 2.");
		
	}
		
}



Is This A Good Question/Topic? 0
  • +

Replies To: Double cannot be dereferenced

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Double cannot be dereferenced

Posted 22 January 2013 - 10:18 AM

this.getArea().equals(circle1.getArea())



In equals(), you're calling getArea() which returns a primitive type (double), but then you're trying to call a method on it, which is illegal. Use the equality operator to compare primitive types. Also, why make calls to getRadius() in your instance methods when you can use radius directly?

This post has been edited by blackcompe: 22 January 2013 - 10:19 AM

Was This Post Helpful? 0
  • +
  • -

#3 lyates93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-January 13

Re: Double cannot be dereferenced

Posted 22 January 2013 - 10:49 AM

So what am I supposed to do?

use the == signs?

I changed it to this, and I still have the same problem

public boolean equals(Circle circle1)
	{
	
		boolean status;
		
		if(radius.equals(circle1.radius) && area.equals(circle1.area))
			status=true;
		else
			status=false;
			return status;
			
	}


Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Double cannot be dereferenced

Posted 22 January 2013 - 11:41 AM

As stated, you can't deference (i.e. calling a method on) a primitive type (i.e. int, char, boolean, double, etc.). You are still doing so, and the compiler is telling you that can't. Primitive types can be compared to each other using relational and equality operators (i.e. > < == !=). Why don't you try that and see if it works?

This post has been edited by blackcompe: 22 January 2013 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

#5 lyates93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-January 13

Re: Double cannot be dereferenced

Posted 22 January 2013 - 03:59 PM

Now I have this... And my outputs are not correct. It says Circle 1 is greater than Circle 2. Also, when I change the circle objects to have the same radius, it says they area and radi are different. What am I doing wrong here?



	public boolean equals(Circle circle2)
	{
	
		boolean status;
		
		if(radius==(circle2.radius) && area==(circle2.area))
			status=true;
		else
			status=false;
			return status;
			
	}




Demo code...


public class CircleDemo
{

	public static void main(String [] args)
	{
	
	Circle circle1 = new Circle(1.44);
	Circle circle2 = new Circle(1.44);
	
	System.out.println(circle1);
	
	if(circle1.equals(circle2))
		System.out.println("Both objects have the same radius and area.");
	else
		System.out.println("The objects have different radi and areas.");
		
		
	if(circle1.greaterThan(circle2))
		System.out.println("Circle 1 is greater than circle 2.");
	else
		System.out.println("Circle 1 is not greater than circle 2.");
		
	}
		
}



Was This Post Helpful? 0
  • +
  • -

#6 lyates93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-January 13

Re: Double cannot be dereferenced

Posted 22 January 2013 - 04:07 PM

I'm confused on what to call the object in the equals(....) method, and how to utilize that in the if statements?
Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Double cannot be dereferenced

Posted 22 January 2013 - 04:20 PM

Quote

Now I have this... And my outputs are not correct. It says Circle 1 is greater than Circle 2. Also, when I change the circle objects to have the same radius, it says they area and radi are different. What am I doing wrong here?


If a circle has a single attribute, wouldn't its equality be based solely on that attribute?

public boolean equals(Object other)
{
     if(!(other instanceof Circle))
          return false;
     return radius == ((Circle)other).radius;
}



I can't tell you about greaterThan since it's not posted.

I'm also moving this to the Java forums.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1