Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 10, 2007, 12:51 PM   #1
jopeter
macrumors newbie
 
Join Date: Nov 2007
palindrome numbers with threads in java

hi
i have wrote this progaram in java.
it finf the palidrome numbers and print it.
its by threads

it has some errors and i can t run it.
please help me.

package palindromen;
import java.lang.Integer;
import java.lang.String;

public class Main {

public Main() {
}

public static void main(String[] args)
{//nop is number of processors.we inter it via keyboard
//for example we have number 0 - 20000.we want to know nubber of palindrome numbers between0-20000
// anyt=20000/nop; 20000/nop is the number of number that every thread should start
int nop,anyt;
Keyboard key=new Keyboard();
System.out.println("Enter number of processors : ");

nop=key.readInt();
anyt=20000/nop;

T Threads[]=new T[nop];
Palindromeset p=new Palindromeset();
for(int i=1;i<=nop;i++) {
//here we say to every thread from what number to what number it should define the palindrome numbers
//for exmaple if we have 10 processors ,Theard[0] should define palindrom numbers 0 to 2000
Threads[i]=new T("Threads"+Integer.toString(i));
Threads[i].setr((i*anyt),anyt*(i+1),p);

}

for(int i=1;i<=nop;i++)
Threads[i].start();

for(int i=1;i<=nop;i++)
try{
Threads[i].join();
}
catch(Exception e)
{ }



p.print();//caling print() from class Palindromeset





}

}
---------------------------------------------------------

package palindromen;


public class T extends Thread{


public int lowr,highr,n;

volatile Palindromeset c;


public void setr(int lr,int hr,Palindromeset pt)
{
lowr=lr;//lowrang
highr=hr;//highrang
c=pt;}


public T(String s) {

this.setName(s);
}

public void run()

{for(int i=lowr;i<highr;i++){


n = i; //used at last time check

int reverse=0,remainder;

while(i > 0){

remainder = i % 10;

reverse = reverse * 10 + remainder;

i = i / 10; }

if(reverse == n)
/*in the number is palindrome we add count bc we want to khow the number of palindrom numbers
and we put the palindrome number in array bubber bc at last we want to print the
palindrom numbers*/
c.addNum(i); //error: at palindromen.T.run(T.java:44)

}
}
}
--------------------------------------------------------------

package palindromen;


public class Palindromeset{
private int buffer[];
private int count;

/** Creates a new instance of Palindromeset */
public Palindromeset() {
count=0;
buffer=new int[20000];
}


public synchronized void addNum(int x)
{buffer[count]=x;
/*error : Exception in thread "Threads0" java.lang.ArrayIndexOutOfBoundsException: 15
at palindromen.Palindromeset.addnum(Palindromeset.java:17)*/

count++;
}

public void print(){
for(int i=0;i<count;i++)
{ System.out.println(buffer[i]);//print the palindrome numbers that exist in buffer[count]
System.out.println(" ");
}
System.out.println();
System.out.println(count); //print number of palindrome numbers

}



}

Last edited by jopeter; Nov 10, 2007 at 04:31 PM.
jopeter is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Using accessors in Java constructors (marc) Mac Programming 4 Dec 3, 2010 09:37 AM
Numbers with Wireless Keyboard gedna002 iPad Tips, Help and Troubleshooting 0 Nov 17, 2010 11:13 AM
Generating data with "regex" in Java SilentPanda Mac Programming 10 Apr 20, 2010 01:04 PM
Replace Symbols with Numbers in Java? JOD8FY Mac Programming 2 Mar 21, 2007 05:47 PM
Exception in thread "main" java.lang.NoClassDefFoundError pnz999 Mac Help/Tips 5 Feb 1, 2003 03:47 PM


All times are GMT -5. The time now is 03:47 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC