Boards.ie uses cookies. By continuing to browse this site you are agreeing to our use of cookies. Click here to find out more x
Post Reply  
 
Thread Tools Search this Thread
12-10-2007, 02:35   #1
AARRRGH
Registered User
 
AARRRGH's Avatar
 
Join Date: Jul 2004
Posts: 12,119
Java compiler: "cannot find symbol method getResult()"

Hello

My Java compiler is giving me a weird error... of course I know the problem is me, but I can't figure it out!

The details of the code aren't really important, as the problem is related to the getResult() method only, but just in case: I have two threads searching through an array looking for the largest number. I have a method getResult() which returns the result of the search. One of the threads searches the first half of the array, the other searches the second half.

Code:
/*
 * Find the maximum element in a large array
 * Two threads
 *
 */
 
 import java.util.*;
 
 class Search extends Thread {
 	
 	private int largeArray[];
 	private int start;
 	private int end;
 	private int biggest;
 		 
 	Search(int largeArray[],int start,int end) {
 		this.largeArray = largeArray;
 		this.start = start;
 		this.end = end;
 	}
 	
 	public void run() {
 		// navigate through each element in the array starting at start
 		biggest = largeArray[start];	
 		for(int x = (start+1); x <= end; x++)
 			if (biggest<largeArray[x])
 				biggest = largeArray[x];
 		// biggest should now contain the biggest number
 	}
 	
 	public int getResult() {
 		return biggest;
 	}
 	
 }
 
 class Question2 {
 	public static void main(String[] args) {
 		
 		System.out.print("How many elements in the array? ");
 		Scanner in = new Scanner(System.in);
 		int x = in.nextInt();
 		
 		int largeArray[] = new int[x];
 		
 		for (int count = 0; count < largeArray.length; count++) {
 			largeArray[count] = (int)(Math.random()*x);
 		}
 		
 		int split = x/2;
 		
 		Thread t1 = new Search(largeArray,0,split-1);
 		Thread t2 = new Search(largeArray,split,x-1);
 		
 		t1.start();
 		t2.start();
 		
 		try {
 			t1.join();
 			t2.join();
 		} catch (InterruptedException e) {}
 		
 		if (t1.getResult()>t2.getResult())
 			System.out.println("The biggest number is " + t1.getResult());
 		else 
 			System.out.println("The biggest number is " + t2.getResult());
 		// catches if they are equal too
 	}
 }
So, it's pretty simple, but the compiler doesn't like the getResult() method.

Can any of you figure out why?

Thanks very much in advance.
AARRRGH is offline  
Advertisement
12-10-2007, 03:01   #2
Giblet
Registered User
 
Giblet's Avatar
 
Join Date: Oct 2001
Location: Dublin
Posts: 10,696
You can only use methods available to Thread if you go
Thread t1 = new Search();
Giblet is offline  
12-10-2007, 03:15   #3
AARRRGH
Registered User
 
AARRRGH's Avatar
 
Join Date: Jul 2004
Posts: 12,119
Hi Giblet

Thanks for the reply. I do have these lines in my code though -

Thread t1 = new Search(largeArray,0,split-1);
Thread t2 = new Search(largeArray,split,x-1);

...followed by...

if (t1.getResult()>t2.getResult())

...which in theory should use...

public int getResult() {
return biggest;
}

Am I not able to do this?

Thanks
AARRRGH is offline  
12-10-2007, 07:40   #4
hussey
Registered User
 
hussey's Avatar
 
Join Date: Oct 2002
Location: Sydney, AUS
Posts: 6,116
Send a message via MSN to hussey
No ...

You are using the Thread class
Thread doesn't have a getResult method ... search does

try Search t1 = new Search(largeArray,0,split-1);

by Using Thread t1 .. you can only use method available to Thread as it isn't aware of subclass methods

Search is still a 'Thread' though

You should only use
Thread t1 = new Search( .. if you don't care about the underlying methods
if you were only ever going to call run()
hussey is offline  
12-10-2007, 13:34   #5
AARRRGH
Registered User
 
AARRRGH's Avatar
 
Join Date: Jul 2004
Posts: 12,119
Ah yes, yes, that worked perfectly.

Thank you for taking the time to figure out what the problem is.
AARRRGH is offline  
Post Reply

Quick Reply
Message:
Remove Text Formatting
Bold
Italic
Underline

Insert Image
Wrap [QUOTE] tags around selected text
 
Decrease Size
Increase Size
Please sign up or log in to join the discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Share Tweet