Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java compiler: "cannot find symbol method getResult()"

  • 12-10-2007 02:35AM
    #1
    Closed Accounts Posts: 12,382 ✭✭✭✭


    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.
    /*
     * 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.


Comments

  • Registered Users, Registered Users 2 Posts: 12,025 ✭✭✭✭Giblet


    You can only use methods available to Thread if you go
    Thread t1 = new Search();


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    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


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭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()


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Ah yes, yes, that worked perfectly.

    Thank you for taking the time to figure out what the problem is.


Advertisement