Results 1 to 4 of 4
  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Why is the logic not correct?

    Why is the logic not correct with the method public static boolean containsColor(String color, Circle [] ca)? It only returns me only false values.

    Java Code:
    public class Circle {
    
    	private double radius;
    	private String color;
    	
    	
    	public Circle()
    	{
    		radius = 1;
    		color = "red";
    	}
    	
    	public Circle(String color, double radius)
    	{
    		this.color = color;
    		this.radius = radius;
    	}
    	
    	public String getColor()
    	{
    		return this.color;
    	}
    	
    	public double getRadius()
    	{
    		return this.radius;
    	}
    	
    	public static boolean containsColor(String color, Circle [] ca)
    	{
    			
    		for (int i = 0; i < ca.length; i++)
    		{
    			if(ca[i].equals(color))
    				return true;
    		}
    		return false;
    		
    		
    		
    	}
    }
    
    
    public class UseCircle {
    
    	public static void main(String [] args)
    	{
    		Circle bc = new Circle("blue", 3.4);
    		Circle gc = new Circle("green", 2.22);
    		Circle rc = new Circle();
    		
    		System.out.println(rc.getColor());
    		System.out.println(gc.getRadius());
    		
    		Circle [] ca1 = new Circle[]{bc, gc, rc};
    		System.out.println(Circle.containsColor("red", ca1));
    		
    		Circle [] ca2 = new Circle[]{bc, gc, bc, gc};
    		System.out.println(Circle.containsColor("red", ca2));
    		System.out.println(Circle.containsColor("green", ca2));
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Why is the logic not correct?

    What is the if statement comparing?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Why is the logic not correct?

    That's where I'm having trouble with..

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Why is the logic not correct?

    Yes, Can you explain what objects the code is comparing?
    What is the data type of the two objects being compare?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Where's the logic?
    By diamonddragon in forum New To Java
    Replies: 11
    Last Post: 01-28-2012, 04:16 AM
  2. Logic Help
    By Barbados in forum New To Java
    Replies: 6
    Last Post: 01-22-2012, 11:24 PM
  3. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 11:06 AM
  4. need a logic for this
    By rajivjoshi in forum New To Java
    Replies: 4
    Last Post: 06-12-2010, 02:18 PM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 12:22 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
  •