aspose file tools*
The moose likes Beginning Java and the fly likes Divisor or prime problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Divisor or prime problem" Watch "Divisor or prime problem" New topic
Author

Divisor or prime problem

Sammi Groover
Greenhorn

Joined: Mar 12, 2005
Posts: 19
okay, I admit it, I suck at Java and I am worse at math. I have to do a problem to list number 2 through 100 and show their divisors of if prime, with no input out a JOptionOane output. For the life of me, I cant figure it out. I have bought over 5 book and cd tutorials ovaer and above my textbooks. I have asked instuctor for help. but he never replies (an online class) to my requests for help, especially with an assignment. I have sat at this computer day and night since I go tthis last assignment and tried to do it myself but cant get anything to work right. (yes, this will be my one and only Java class, I realize I just am not made for it, but I cannot withdraw from class. I have to at least pass it, so I am trying very very hard to do so. Anyhow, here is my latest effort.. can anyone tell me what I have dont wrong ( besides point out I did not use JOptionPane, I know that). I get a 'class' error at line 40 and a ';' expected at line 56. I know there are more errors beside those but until I resolve them, I cant even see if the program does what I want it to do.
I hope this does not qualify as an intermediate problem; I am about as much as a beginner as you can get at all this, and reading the books, to me I might as well be reading a differnt language as I do not understand it at all

any help greatly appreciated.

thnaks!
Sammi



[ March 12, 2005: Message edited by: Sammi Groover ]
Heh, I forgot my code... here it is:

import java.awt.*;
import java.applet.*;
import java.swing.*;

public class Assignment2ProjectA extends Applet {

//Set up GUI and divide numbers 2 through 100
public void init()
{
// JTextArea to display results
JTextArea outputArea = new JTextArea();

// get applet's content pane (GUI component display area)
Container container = getContentPane();

// attach outputArea to container
container.add( outputArea );

int number;// numbers 2 through 100
int result;// store result of call to method divisor
String output = "";// String container results


outputArea.setText( output ); // place results in JTextArea

} // end method init

public static void main( String args [] )
{
// loop 100 times
for ( int number = 2; int number <=100; number++ )
{
boolean isPrime = true;
int divisor = 2;

while(divisor < number && isPrime )
{
if( number % divisor == 0)
isPrime = false;

divisor++;
}

result = divisor( number ); // method call

// append result to String output
output += number ":" + " is " +result+ "\n";

} // end for

outputArea.setText( output ); // place results in JTextArea

} // end static void

// divisor method declaration
public int divisor( int y )
{
return y / y; //return divisor of y

} // end method divisor

} // end Class


[ March 12, 2005: Message edited by: Sammi Groover ]

"Most folks are about as happy as they make up their minds to be" Abraham Lincoln
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Can you get it to work using just textual output to standard error (System.out.printlns)? I mean, can you at least verify that you're factoring the numbers correctly?


James Carman, President<br />Carman Consulting, Inc.
Sammi Groover
Greenhorn

Joined: Mar 12, 2005
Posts: 19
well, I started with a prime number code, and guessing at the part to have the numbers from 2 to 100 run, appear, and display as required
(being example:
2:prime
3:prime
4: 2
5:prime
6: 2 3
7:prime
8: 2 4
The prime ran in the System,out.println. And, if I take out the sections regarding the prime and divisors, the program actually does run, just displays an empty box. I am so lost... I should be in Taos and I am in San Francisco...
I just dont know what to do anymore and I have no one to help me or point me in right direction. I can't afford a tutor, I am a single, disable mother living on disablity as it is. ( why I am going to school, to learn a new way to support my kids since I cant do what I used to do anymore, but this is killing me. I am used to "A"'s and understanding concepts, this is driving me nuts!
I am not asking anyone to do my homework assignmnets, just help me understand how to make it work and what I am doing wrong.

thanks again
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
In your init() method, you do this...



What would you expect to be in the text area after those two lines execute? You're never putting anything into the "output" variable.
Sammi Groover
Greenhorn

Joined: Mar 12, 2005
Posts: 19
I know, I dont know what to put. My text book does not cover this type of problem, nor do any of the books I bought. Nor in the 5 powerpoint lectures ( my instuctors only interaction in this cllas) is it explained either. All I can do is look at the figures in the book, search the web for other examples and try to peice it together> I suppose to someone who knows algebra, it might be easy. But back when I went to grade - high school, it was not even required you take pre-algebra even to graduate, and I have never held a job where I needed math. So I do not even know how to figure out how to do the problem on paper, much less in Java code, but I am trying. Just doing it all wrong.... Heck, in our text, we have assignments, not required to do by instuctor, but then, there are no answers/solutions to the assignments either, so even ther have have no idea what I am doing wrong. I really enjoyed the class and the concept at the start, now it is like a prison...
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Didn't you say you had code already that could identify prime numbers and print their factors? Here's what I would suggest writing first...



Then, if you want to know if a number is prime, just count its factors.
Sammi Groover
Greenhorn

Joined: Mar 12, 2005
Posts: 19
Ok, I will work on that and give it a try.

On what I first posted, I changed a part to
// divisor method declaration
public int divisor( int y )
{
return (y/y); //return divisor of y

} // end method divisor

it compiled, but gave answer to a different project... lol

Oh well, I will start again with your suggestion and let you know how it goes from there, okay?


thanks!
Sammi Groover
Greenhorn

Joined: Mar 12, 2005
Posts: 19
Whoa.. collections are chapter 22 and we are only in chapter 8 (at least I think 8, he never gives any reading assingments, but syllabus only has us at math methods this week, which is even before arrays... dang. It is like, do this assignment, I am nt going to tell you how to until after you fail it lol LOL
Sammi Groover
Greenhorn

Joined: Mar 12, 2005
Posts: 19
okay, so I did this:

import java.awt.*;
import java.io.*;
import java.swing.*;

public class Assignment2ProjectA {

public static void main( String args [] )
{
// loop 100 times
for (int i=2;i<=100;i++)
{

for (int j=2;j<=i;j++)
{

boolean isPrime = true;
int divisor = 2;

while(divisor < number && isPrime )
{

if( number % divisor == 0)
isPrime = false;

divisor++;
}

}// Print all the divisors or "prime" out





}
// display results
JOptionPane.showMessageDialog(null, (number),
"Divisors or Prime"
JOPtionPane,INFORMATION_MESSAGE )



} // end static void


} // end Class Assignment2ProjectA

It compiled, I cheered. But when I tried to execute it, it gave a exception in "main"java,lang NoClassDefFoundError
I shut it down, lived a little, came back a little later and tried in in notepad and dos promt, same thing. So I reopened in JCreator and I got ')' expected at line 47 and ';' expected at line 48 errors

I am perplexed...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

there are a couple problems here that i see...

JOptionPane.showMessageDialog(null, (number),
"Divisors or Prime" <----------------- comma???
JOPtionPane,INFORMATION_MESSAGE ) <----------------- semicolon???

you have never declared the variable "number", but you start using it in your while loop...

and i think you want to import

javax.swing.*;

i'm not sure what INFORMATION_MESSAGE is... and neither is my compiler.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Please use the code tags when posting code as it keeps your identations in place.
There are a few things wrong with your code, Fred has pointed out some. I would suggest you take a step back and work in very short but specific steps that can be tested along the way.
Can you describe your problem in psuedo code ?
You really don't need to use collections for this task, just a couple of loops and some if/else statements. Remember do one thing at a time, it is easier to debug small amounts of code that are incorrect logically, than large amounts of code that doesnt compile at all.
Sammi Groover
Greenhorn

Joined: Mar 12, 2005
Posts: 19
Okay, thank you both

I will work on it more of course. I realize I am missing needed code info still to make program do what I want, but when I cant even get it tocomplie as it should per the examples given in my text book, it is tough to figure out. (as in none of the examples in my bok have a comma after the "comment" in the JOption section, nor a semi-colon at the end of it, so I appreciate that tip very much. I wil keep wprking and keep you updated on progress.
Thanks again!
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Step 1. Get a class template that compiles.


Step 2. Do something small which works towards the solution, then recompile, test and move on.


e.t.c
You will notice that I haven't imported any classes yet. This is because until I can get the output I want to appear on the console my logic isn't in place, so get the logic to function before adding a graphical output.
HTH
[ March 14, 2005: Message edited by: Nigel Browne ]
Sammi Groover
Greenhorn

Joined: Mar 12, 2005
Posts: 19
ok, I do understand what you are saying, and I do know what I want the program to do, I am just having a hard time translating it to language the computer understands is all.

so, I am going step by step, in process. looks like this so far...
public class Assignment2ProjectA {
public static void main( String args [] ) { //Loop from 2 to 100

for (int i = 2; i <= 100; i++) {// list all number from 2 through 100

for (int j =2; j <= i; j++){// divide all numbers from 2 though 100 with all numbers from 2 though 100
// figure divisors or if prime
boolean isPrime = true;
int j = 2;
if int (i % j) = 0
}

//prove the loop by visually printing to the console
System.out.print(i + " ");
}
}
} but just added 3 lines from boolean on since last compile, as I said working and trying
thanks for your helpful tip!
Sammi Groover
Greenhorn

Joined: Mar 12, 2005
Posts: 19
okay so up to
public class Assignment2ProjectA {
public static void main( String args [] ) { //Loop from 2 to 100

for (int i = 2; i <= 100; i++) {// list all number from 2 through 100

for (int j = 2; j <= i; j++){// divide all numbers from 2 though 100 with all numbers from 2 though 100

boolean isPrime = true;
int divisor = 2;
while (j < i && isPrime) {

}

}

//prove the loop by visually printing to the console
System.out.print(i + " " );
}
}
}
it compiles. of course, I have not changes print out so it doesn't print out more than 2 - 100 yet, but when I add the if statement and it looks like this:
public class Assignment2ProjectA {
public static void main( String args [] ) { //Loop from 2 to 100

for (int i = 2; i <= 100; i++) {// list all number from 2 through 100

for (int j = 2; j <= i; j++){// divide all numbers from 2 though 100 with all numbers from 2 though 100

boolean isPrime = true;
int divisor = 2;
while (j < i && isPrime) {

if( i % j ) =0
isPrime =false;
divisor++;
}

}

//prove the loop by visually printing to the console
System.out.print(i + " " );
}
}
}
it says illegal start of expression for line 23, so obvioulsy I am doing something wrong, may mind is just not clicking on what.. I have to stare at it long and hold my tongue right to figure it out.. (an old family expression, okay?, just thinking out loud here...

again, thanks for any help and discussion
Sammi Groover
Greenhorn

Joined: Mar 12, 2005
Posts: 19
I got it, it should have been if( i % j == 0), just needed to think out loud, now I have to work on printing out what I did so far, I know I know,I shoulda done that 3 steps back, but I forgot, sorry.

Okay, I just saw your last post, will go back and work a bit more, be back soon
[ March 14, 2005: Message edited by: Sammi Groover ]
Sammi Groover
Greenhorn

Joined: Mar 12, 2005
Posts: 19
Okay, just an update on how it is going.. am I headed down wrong road again?

So anyhow, of course I get 3 errors on the JOptionPane line; being ')' expected, illegal start of expression, and ';' ex[ected (again... what am I doing wrong? please

thank you kindly in advance
[ March 14, 2005: Message edited by: Sammi Groover ]
Sammi Groover
Greenhorn

Joined: Mar 12, 2005
Posts: 19
Oh, I did notice I had a semi-colon instead of a colon in the (null, (int i ? "Prime" : int k ). I fixed it and lost ';' expected error, but still have other two if spoke of last reply.
thanks again so much!
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Sammi Groover:
Dave plays "be the compiler":
  • The class is "JOptionPane" -- remove the first period
  • The ternary operator is a question mark and a colon -- not semicolon.
  • The first part of the ternary statement must be a boolean condition. "int i" is a local variable declaration -- what did you intend?
  • The two results of the ternary statement must be of the same type. "Prime" is a String and "int k" is another local variable declaration.

  • The only variable in scope at this line is the for loop index "i" from above. Can you describe in words what this line is supposed to do? Obviously, you want to display a message dialog, but describe what it should say under the what conditions.
    Sammi Groover
    Greenhorn

    Joined: Mar 12, 2005
    Posts: 19
    Thanks, I missed that period.. too many hours with face in computer, have tunnel vision.

    Anyhow, what I want the program to print out is to list each numner, from 2 through 100 and then state either the divisors of each number, or if that number is a prime number, with each number statement ment in a new line, such as:

    2:prime
    3:prime
    4: 2
    5:prime
    6: 2 3
    7:prime
    8: 2 4
    (etc.)

    problem is I have no idea to even fo that woth a calculator much less tella computer how to do it...

    I also know I need to do this:

    loop number1 goes for 2 to 100
    loop number2 goes for 2 to number1
    in this inner loop I check whether number1 % number2 is zero
    for any number in number2 if the remainder is zero then number1 is not
    prime
    then turn the flag on and print the factor move to the next cycle.
    However if the remainder is not zero I dont know until I have exhausted
    all the divisiors
    so then move to the next cycle without changing the flag.

    at the end if the flag is not turned on that means I have a prime.

    Still, I am having a heck of a time here as you all can tell. I just do not know what to do... I have redone this so many times, I am lost totally instead of understanding it better. and you can safe and assured, this old lady will never be one to take your jobs away. if I pass this class with a "C", I will be happy and promise to never ever ever ever take another programming class for the rest of my life...

    Thanks once more
    David Harkness
    Ranch Hand

    Joined: Aug 07, 2003
    Posts: 1646
    Okay, going from the program from your latest post, you're pretty close.

    First, your outer loop using i controls which number you're currently investigating for primeness. The inner loop using j is the one that checks i against all integers between 2 and i, excluding i itself.

    One problem is that you declare the isPrime local variable inside this inner loop, meaning it gets reset each time. Move it up one level (inside the outer loop). You also use a local k counter, but this also gets reset each time through the inner loop. Drop the use of k entirely and replace it with j.

    Inside the else block is where you want to collect the divisors of i. One very simple way to do this is to append the divisor onto a String which you can display at the end of each inner loop iteration using System.out.println() or JOptionPane. Here's an example:This prints
    Sammi Groover
    Greenhorn

    Joined: Mar 12, 2005
    Posts: 19
    okay, thank you truly. I will work on it more, obviously. I am confi=used though as earlier in post, I was told to use k. No biggy, I will go forward

    thanky ou again. I will keep all posted on my progress, though I am beginning to realize reading case law and penal codes are much easier than all this heehee
    Sammi Groover
    Greenhorn

    Joined: Mar 12, 2005
    Posts: 19
    Well, thanks to all of you for your help, I am sure I will be back soon as I have to do an 11 function calculator next ( oh boy!)

    I got it to complie and execute, how neat is that?

    Thank you all again!!!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Divisor or prime problem