Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

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

  • 12-10-2007 2: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 Posts: 11,977 ✭✭✭✭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 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