3 Replies - 2159 Views - Last Post: 04 June 2007 - 12:54 PM Rate Topic: -----

#1 Emper0r  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 07-December 06

Private method which returns paticular objects from an array

Posted 04 June 2007 - 12:30 PM

Hey guys, basically what I've made is a driver class designed for users to add 2 differe types of objects to an array of type Worker. The two types of objects are Producers and Managers. After the user is done adding all the desired Producer and Manager objects to the array, the main method needs to call a private static method in the same class.

This private method is supposed to check which elements in the array are Producer objects, then return that Producer's information if their Production Target(a variable declared in the Producer class) is greater than or equal to 50.

I know I have to use the String class's compareTo method to check if the element is equal to "Producer" while walking the array, but I'm not doing the syntax correctly. Can someone please point out what I formatted wrong in the method below, thanks a bunch:

// Returns info of all Producers with production targets larger or equal to 50
	private static int bigPT(Worker wrs[])
	{
	   for(int i = 0; i < wrs.length; i++)
		{
   		if(wrs[i].toString(wrs[i].compareTo("Producer")))
			{
				return wrs[i].productionTarget >= 50;
			}
		}
	}




Here's the error messages I recieve:

CompanyDriver.java:71: cannot find symbol
symbol : method compareTo(java.lang.String)
location: class Worker
if(wrs[i].toString(wrs[i].compareTo("Producer")))
^
CompanyDriver.java:73: cannot find symbol
symbol : variable productionTarget
location: class Worker
return wrs[i].productionTarget >= 50;
^
CompanyDriver.java:73: incompatible types
found : boolean
required: int
return wrs[i].productionTarget >= 50;
^
3 errors

Is This A Good Question/Topic? 0
  • +

Replies To: Private method which returns paticular objects from an array

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: Private method which returns paticular objects from an array

Posted 04 June 2007 - 12:38 PM

you cannot use compareTo since the objects do not have a compareTo method. You have 2 choices.
Add a compareTo method to both Worker types, which return a String of the class name.
OR
use the
((wrs[#].getClass()).getName()).equals("Name of class");

all objects can return a Class type, which has the ability to return the name of the class as a String :)
Was This Post Helpful? 0
  • +
  • -

#3 Emper0r  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 07-December 06

Re: Private method which returns paticular objects from an array

Posted 04 June 2007 - 12:47 PM

View PostWilliam_Wilson, on 4 Jun, 2007 - 12:38 PM, said:

you cannot use compareTo since the objects do not have a compareTo method. You have 2 choices.
Add a compareTo method to both Worker types, which return a String of the class name.
OR
use the
((wrs[#].getClass()).getName()).equals("Name of class");

all objects can return a Class type, which has the ability to return the name of the class as a String :)


Thanks for the response, I modified the code but not with what you suggested yet. Do you think this would work:

private static int bigPT(Worker wrs[])
	{
	   for(int i = 0; i < wrs.length; i++)
		{
   		if(wrs[i].toString().equals("Producer"))
			{
				return wrs[i + 2] >= 50;
			}
		}
	}



I'm getting a new error that maybe you could help on also, here it is:

CompanyDriver.java:73: operator >= cannot be applied to Worker,int
return wrs[i + 2] >= 50;
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: Private method which returns paticular objects from an array

Posted 04 June 2007 - 12:54 PM

If your classes have toString() methods which return the name of the class it should.
You cannot apply >= to an int and your Object.

I assume you want to compare: i+2 >= 50
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1