class Circle
{
	private double  radius;
	private  String colour;
	
	Circle()
	{
	}
	
	Circle(double r, String c)
	{
		this.radius=r;
		this.colour=c;
	}
	
	
	public void display()
	{
		System.out.println("Radius of circle is "+this.radius);
		System.out.println("Colour of circle is "+this.colour);
	}
	
	public String getColour()
	{
		return (this.colour);
	}
	
	
}

how calculate and display the number of these Circle objects that have the colour attribute value equals to “red”,with use the equals() or compareTo() methods to compare strings.

import javax.swing.JOptionPane;
class TestCircle
{
	public static void main(String []args)
	{	
		double rad;
		String clr;
		Circle test[]=new Circle[5];
		for(int i=0;i<5;i++)
		{
			rad=Double.parseDouble(JOptionPane.showInputDialog(null,"Enter radius of circle "+(i+1)));
			clr=(JOptionPane.showInputDialog(null,"Enter colour of circle "+(i+1)));
			test[i] = new Circle(rad, clr);
				
		}
		for (int i=0;i<5;i++)
		{
			System.out.println("properties of  circle "+(i+1));
			test[i].display();
			
		}	
	}	
	
}

After you have filled your array of Circles you can loop through that array and use your getColour() method to get the colour of each Circle. Test if that is red, and increment a counter if it is.

..

basically, the same way you always compare Strings, by the equals method.
have you tried to do what JamesCherrill suggested?

if you have any trouble implementing it, show us what you tried.

basically, the same way you always compare Strings, by the equals method.
have you tried to do what JamesCherrill suggested?

if you have any trouble implementing it, show us what you tried.

I have tried, but I do not know how to begin after this. : (((

import javax.swing.JOptionPane;
class TestCircle
{
	public static void main(String []args)
	{	
		double rad;
		String clr;
		Circle test[]=new Circle[5];
		for(int i=0;i<5;i++)
		{
			rad=Double.parseDouble(JOptionPane.showInputDialog(null,"Enter radius of circle "+(i+1)));
			clr=(JOptionPane.showInputDialog(null,"Enter colour of circle "+(i+1)));
			test[i] = new Circle(rad, clr);
				
		}
		
		for(int i=0;i<5;i++)
		{
			test[i].getColour();
		}
		for (int i=0;i<5;i++)
		{
			System.out.println("properties of  circle "+(i+1));
			test[i].display();
			
		}	
	}	
	
}

test.getCouleur() returns the String you have to compare with equals

On line 19 you have the colour for each circle. Now see if that is "red". You already have some hints on how to compare Strings.

On line 19 you have the colour for each circle. Now see if that is "red". You already have some hints on how to compare Strings.

after this

import javax.swing.JOptionPane;
class TestCircle
{
	public static void main(String []args)
	{	
		double rad;
		String clr;
		Circle test[]=new Circle[5];
		for(int i=0;i<5;i++)
		{
			rad=Double.parseDouble(JOptionPane.showInputDialog(null,"Enter radius of circle "+(i+1)));
			clr=(JOptionPane.showInputDialog(null,"Enter colour of circle "+(i+1)));
			test[i] = new Circle(rad, clr);
				
		}
		
		for(int i=0;i<5;i++)
		{
			test[i].getColour();
			if (test[i].getColour().equals("red"))
			{
				
			}
		}
		
		for (int i=0;i<5;i++)
		{
			System.out.println("properties of  circle "+(i+1));
			test[i].display();
			
		}	
	}	
	
}

yes
but only if you enter red as parameter also in lower cases, and:

test[i].getColour(); // you don't use this line, so it can be left out.
if (test[i].getColour().equals("red"))
{
				
}

yes
but only if you enter red as parameter also in lower cases, and:

test[i].getColour(); // you don't use this line, so it can be left out.
if (test[i].getColour().equals("red"))
{
				
}

tq guys.already solve :D

import javax.swing.JOptionPane;
class TestCircle
{
	public static void main(String []args)
	{	
		double rad;
		String clr;
		Circle test[]=new Circle[5];
		for(int i=0;i<5;i++)
		{
			rad=Double.parseDouble(JOptionPane.showInputDialog(null,"Enter radius of circle "+(i+1)));
			clr=(JOptionPane.showInputDialog(null,"Enter colour of circle "+(i+1)));
			test[i] = new Circle(rad, clr);
				
		}
		
		int counter=0;
		for(int i=0;i<5;i++)
		{
			if (test[i].getColour().equals("red"))
			{
				counter++;
			}
		}
		
		
		
		for (int i=0;i<5;i++)
		{
			System.out.println("properties of  circle "+(i+1));
			test[i].display();
			
		}	
		
		System.out.println("Total objects have colour red is "+counter);
	}	
	
	
}

That's great. Well done.
Just one small suggestion: if you use the very useful method equalsIgnoreCase rather than just equals, then it won't matter whether you have red Red or RED.

That's great. Well done.
Just one small suggestion: if you use the very useful method equalsIgnoreCase rather than just equals, then it won't matter whether you have red Red or RED.

yes..tq ,this task just want me to compare if colour is "red"only. .:D
I will use that method if have any task to used that method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.