954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to compare the attributes of arrays?

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();
			
		}	
	}	
	
}
long89
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
long89
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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();
			
		}	
	}	
	
}
long89
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
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();
			
		}	
	}	
	
}
long89
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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"))
{
				
}
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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);
	}	
	
	
}
long89
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
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.

long89
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You