The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Threads

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
jigz n

Posts: 1
Nickname: jigz
Registered: Feb, 2006

Java Threads Posted: Feb 25, 2006 4:04 PM
Reply to this message Reply
Advertisement
Hi,
I am stuck on this bit with thread group.

Problem is that A General needs to find the factors of a big number.

He can do this himself by:


import java.lang.Math.*;

public class SeqGeneral {
int oddNumber, totalNumberToCheck;
boolean[] factorOrNot;

SeqGeneral(int _oddNumber) { // constructor
oddNumber = _oddNumber;
totalNumberToCheck = (int)Math.sqrt((double)oddNumber) - 1;
factorOrNot = new boolean[totalNumberToCheck];

for(int i = 0; i < totalNumberToCheck; i++) // initialization
factorOrNot = false;
}

boolean isFactor(int numberToCheck) {
int intPart = (int)(oddNumber / numberToCheck);
return (intPart * numberToCheck == oddNumber);
}

void report() { // report the non-trivial factors
System.out.println("All non-trivial factors of the given number "
+ oddNumber + " are listed as follows: ");

for(int i = 0; i < totalNumberToCheck; i++) {
if (factorOrNot == true) {
System.out.println( (i+2) +
" is a non-trivial factor, and its related factor is: "
+ (int)(oddNumber/(i+2)) );
}
}
}

public static void main(String[] args) {
int oddnumber = Integer.parseInt(args[0]); // read in the odd number

SeqGeneral sg = new SeqGeneral(oddnumber);

// checks the numbers one by one himself!!!
for(int i = 0; i < sg.totalNumberToCheck; i++)
sg.factorOrNot = sg.isFactor(i+2); // note: index starts from zero but
// the number to check starts from two
sg.report(); // reports the result to the princess
System.out.println("The hard-working general wins the princess!");
}
}


He has alot of soldiers that he can use. I can use the soldiers to do the job using threadgroup.

Any help would be appriciated.

Topic: jsp extracting data from files Previous Topic   Next Topic Topic: error message: unable to access jarfile server.jar

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use