• Post Reply Bookmark Topic Watch Topic
  • New Topic

am i on the right track??  RSS feed

 
John Arbuckle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to setup an array of 1000 integers, fill it with random integers, then check how many r prime. i am asked to launch 10 threads and assign 100 integers to each.

[
import java.util.Random;
import java.io.*;

public class CountPrime
{
int[] Myarray = new int[10];
int Counter = 0;
public void main (String args[]) throws InterruptedException
{
// Populate the array with positive random numbers
Random rng = new Random();
for( int i = 0; i < 10; i++)
{

Myarray[ i ] = (int) (rng.nextGaussian()*90+10);
if(Myarray[ i ] < 0 )
Myarray[i] *= -1;

System.out.println( Myarray[ i ] );
}

// create and startthreads
ThreadOne= new oneHundred();
One.start();
ThreadTwo = new twoHundred();
Two.start();
ThreadThree = new threeHundred();
Three.start();
ThreadFour = new fourHundred();
Four.start();
ThreadFive = new fiveHundred();
Five.start();
ThreadSix = new sixHundred();
Six.start();
ThreadSeven = new sevenHundred();
Seven.start();
ThreadEight = new eightHundred();
Eight.start();
ThreadNine = new nineHundred();
Nine.start();
ThreadTen = new thousand();
Ten.start();

One.join();
Two.join();
Three.join();
Four.join();
Five.join();
Six.join();
Seven.join();
Eight.join();
Nine.join();
( (thousand) Ten ).end();

System.out.println( "There are : " + Counter + " Primes in the array! " );
}


// Class that checks for prime numbers with the 1st 100 integers
class oneHundred extends Thread
{
public void run()
{
try {
boolean flag = true;
for(int i = 0; i < 100; i++)
{
int tmp = Myarray[i];
int tmp2 = tmp / 2;
for(int j = 2; j < tmp2; j++)
{
int modulus = Myarray[i] % i;
if( modulus == 0 )
flag = false;
}
if (flag)
Counter++;
}
}
catch ( IOException ioe )
{
System.out.println( ioe.getMessage() );
}
}
}

// Class that checks for prime numbers with the integers 101 - 200
class twoHundred extends Thread
{
public void run()
{
try {
boolean flag = true;
for(int i = 100; i < 200; i++)
{
int tmp = Myarray[i];
int tmp2 = tmp / 2;
for(int j = 2; j < tmp2+1; j++)
{
int modulus = Myarray[i] % i;
if( modulus == 0 )
flag = false;
}
if (flag)
Counter++;
}
}
catch ( IOException ioe )
{
System.out.println( ioe.getMessage() );
}
}
}


// Class that checks for prime numbers with the integers 201-300
class threeHundred extends Thread
{
public void run()
{
try {
boolean flag = true;
for(int i = 200; i < 300; i++)
{
int tmp = Myarray[i];
int tmp2 = tmp / 2;
for(int j = 2; j < tmp2+1; j++)
{
int modulus = Myarray[i] % i;
if( modulus == 0 )
flag = false;
}
if (flag)
Counter++;
}
}
catch ( IOException ioe )
{
System.out.println( ioe.getMessage() );
}
}
}


// Class that checks for prime numbers with the integers 301-400
class fourHundred extends Thread
{
public void run()
{
try {
boolean flag = true;
for(int i = 300; i < 400; i++)
{
int tmp = Myarray[i];
int tmp2 = tmp / 2;
for(int j = 2; j < tmp2+1; j++)
{
int modulus = Myarray[i] % i;
if( modulus == 0 )
flag = false;
}
if (flag)
Counter++;
}
}
catch ( IOException ioe )
{
System.out.println( ioe.getMessage() );
}
}
}


// Class that checks for prime numbers with the integers 401-500
class fiveHundred extends Thread
{
public void run()
{
try {
boolean flag = true;
for(int i = 400; i < 500; i++)
{
int tmp = Myarray[i];
int tmp2 = tmp / 2;
for(int j = 2; j < tmp2+1; j++)
{
int modulus = Myarray[i] % i;
if( modulus == 0 )
flag = false;
}
if (flag)
Counter++;
}
}
catch ( IOException ioe )
{
System.out.println( ioe.getMessage() );
}
}
}


// Class that checks for prime numbers with the integers 501-600
class sixHundred extends Thread
{
public void run()
{
try {
boolean flag = true;
for(int i = 500; i < 600; i++)
{
int tmp = Myarray[i];
int tmp2 = tmp / 2;
for(int j = 2; j < tmp2+1; j++)
{
int modulus = Myarray[i] % i;
if( modulus == 0 )
flag = false;
}
if (flag)
Counter++;
}
}
catch ( IOException ioe )
{
System.out.println( ioe.getMessage() );
}
}
}


// Class that checks for prime numbers with the integers 601-700
class sevenHundred extends Thread
{
public void run()
{
try {
boolean flag = true;
for(int i = 600; i < 700; i++)
{
int tmp = Myarray[i];
int tmp2 = tmp / 2;
for(int j = 2; j < tmp2+1; j++)
{
int modulus = Myarray[i] % i;
if( modulus == 0 )
flag = false;
}
if (flag)
Counter++;
}
}
catch ( IOException ioe )
{
System.out.println( ioe.getMessage() );
}
}
}


// Class that checks for prime numbers with the integers 701-800
class eightHundred extends Thread
{
public void run()
{
try {
boolean flag = true;
for(int i = 700; i < 800; i++)
{
int tmp = Myarray[i];
int tmp2 = tmp / 2;
for(int j = 2; j < tmp2+1; j++)
{
int modulus = Myarray[i] % i;
if( modulus == 0 )
flag = false;
}
if (flag)
Counter++;
}
}
catch ( IOException ioe )
{
System.out.println( ioe.getMessage() );
}
}
}


// Class that checks for prime numbers with the integers 801-900
class nineHundred extends Thread
{
public void run()
{
try {
boolean flag = true;
for(int i = 800; i < 900; i++)
{
int tmp = Myarray[i];
int tmp2 = tmp / 2;
for(int j = 2; j < tmp2+1; j++)
{
int modulus = Myarray[i] % i;
if( modulus == 0 )
flag = false;
}
if (flag)
Counter++;
}
}
catch ( IOException ioe )
{
System.out.println( ioe.getMessage() );
}
}
}


// Class that checks for prime numbers with the integers 901 - 1000
class thousand extends Thread
{
public void run()
{
try {
boolean flag = true;
for(int i = 900; i < 1000; i++)
{
int tmp = Myarray[i];
int tmp2 = tmp / 2;
for(int j = 2; j < tmp2+1; j++)
{
int modulus = Myarray[i] % i;
if( modulus == 0 )
flag = false;
}
if (flag)
Counter++;
}
}
catch ( IOException ioe )
{
System.out.println( ioe.getMessage() );
}
}
}


}


]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't a GUI question. Moving to Java In General (Intermediate)
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your question???
 
John Arbuckle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the following error when compiling.any idea why?


C:\my java\textpad\CountPrime.java:54: cannot resolve symbol
symbol : method end ()
location: class CountPrime.thousand
( (thousand) Ten ).end();
^
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a tip for you. Instead of doing this:

you should use a loop to do all that stuff, then shove the threads into an array if you want to keep track of them. It's much cleaner that way.

Edit: I also recommend using recursion for this, ultimately returning the number of primes.
[ June 23, 2004: Message edited by: Darin Niard ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!