Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-07-2008, 10:10 PM
Member
 
Join Date: Jul 2008
Posts: 8
starchildren3317 is on a distinguished road
Positive Divisor of int
Hello Community,

I am a new java student. I recently began taking a java course a few weeks ago just to let you all know about where my knowledge with java is.

I am trying to create a program that will prompt the user (I am using JOptionPane) to enter in a number and then I need to get all the positive divisors of that number, seperate them between the even numbers and the odd numbers and output the result to the user. I am learning how to do loops at the moment.

So for example if the user typed in 100
I need the program to figure out that the positive divisors of 100 are: 1, 2, 4, 5, 10, 20, 25, 50, and 100.

This is where I am stuck.

Also, I have a fairly good idea of how to seperate numbers into even and odd by telling the program to % and if the remainder is 0 than the number is even and if it is not zero than the number is odd. However, I can't figure out how I would seperate the positive divisors that were calculated by the program.

For example, if the user entered in the numbers 1, 2, 4, 5, 10, 20, 25, 50, and 100 than I could use: switch (number % 2) and then use cases to seperate which were odd and even but not sure how I could put it into the switch after the program figured out the positive divisors.

Any tips, help, suggestions, examples would be really really appreciated. I have been working on this program for days and this is where I am really stuck.

Thanks again,
Newbie Java Student
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-07-2008, 11:12 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 421
Nicholas Jordan is on a distinguished road
keep going
Quote:
Originally Posted by starchildren3317 View Post
I am a new java student. I recently began taking a java course a few weeks ago just to let you all know about where my knowledge with java is.
At a junior college?

Quote:
Originally Posted by starchildren3317 View Post
I am trying to create a program that will prompt the user (I am using JOptionPane) to enter in a number and then I need to get all the positive divisors of that number, seperate them between the even numbers and the odd numbers and output the result to the user. I am learning how to do loops at the moment.
Loops are a powerful construct. Getting divisiors is not unlike what you have already done to determine even/odd

Quote:
Originally Posted by starchildren3317 View Post
So for example if the user typed in 100 I need the program to figure out that the positive divisors of 100 are: 1, 2, 4, 5, 10, 20, 25, 50, and 100.
First divisor other than provided number cannot be greater than one-half of sample number. I suggest a brute-force approach to start off with:

Code:
While number % --candidate divisor != 0 continue.
The double minus mark is shorthand for number = number miinus one.

Quote:
Originally Posted by starchildren3317 View Post
This is where I am stuck.
Do you have any ideas no matter how crude? Also, check for division by zero before attempting modulo operator. Also, eliminate negative numbers. Also,....

This is how you get unstuck, program away conditions that cannot exist. The solution naturally falls out.


Quote:
Originally Posted by starchildren3317 View Post
Also, I have a fairly good idea of how to seperate numbers into even and odd by telling the program to % and if the remainder is 0 than the number is even and if it is not zero than the number is odd. However, I can't figure out how I would seperate the positive divisors that were calculated by the program.
Code:
if ( number < 0 )
Quote:
Originally Posted by starchildren3317 View Post
For example, if the user entered in the numbers 1, 2, 4, 5, 10, 20, 25, 50, and 100 than I could use: switch (number % 2) and then use cases to seperate which were odd and even but not sure how I could put it into the switch after the program figured out the positive divisors.
Switch approach for this is clumsy in hands of beginner. Use a loop, test for a condition.
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-08-2008, 12:44 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 939
Norm is on a distinguished road
"have been working on this program for days"
If you post your code with coments on where you are having problems, it would help us help you.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-08-2008, 05:13 AM
Member
 
Join Date: Jul 2008
Posts: 8
starchildren3317 is on a distinguished road
I sort of follow you but I am still having a hard time figuring out how to do it.

To answer your question first: It is not a junior college but I have no IT background. After teaching myself HTML I decided that I wanted to learn Java so I signed up for a Java course.

Here is where I am at:

Code:
import javax.swing.JOptionPane; import java.util.*; public class CH05EX16 { public static void main(String[] args) { int locker; //The locker number user inputs. int evens; //The number of closed lockers. int odds; //The number of open lockers. int number; //The positive divisor of locker. String inputMessage; String outputMessage; String inputStr; inputMessage = "Enter the number of lockers " + "in your school. \n" + "To exit the program enter 0."; inputStr = JOptionPane.showInputDialog(inputMessage); locker = Integer.parseInt(inputStr); while (locker != 0 ) { } } }
So what the program is doing is that the user is entering in the number of lockers that are at their school. There is a game that is played and the result is that all the even number lockers end up being closed and all the odd number lockers end up being open.

I must use JOptionPane and leave it open so that the user can continue to put in any number of locker without having to restart the program.

So I have to figure out how to take the locker number and get its positive divisor. Then I must take all the positive divisors of the number and seperate them between the ones that are even and the ones that are odd numbers.
For each positive divisor I figured that I would use:

Code:
(number % 2)
This way I could tell that any number that == 0 was an even number and any number that != 0 was an odd number.

I then need to output:

Code:
outputMessage = "The lockers that are closed are: " + evens + \n + "The lockers that are open are: " + odds; JOptionPane.showMessageDialog(null, outputMessage, "The locker game", JOptionPane.PLAIN_MESSAGE); inputMessage = "Enter a different number of lockers" + "To exit the program enter 0."; inputStr = JOptionPane.showInputDialog(inputMessage); locker = Integer.parseInt(inputStr); } //end while System.exit(0);
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-08-2008, 04:11 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 421
Nicholas Jordan is on a distinguished road
tedious work here, be patient
Okay, first thing is to write a separate class to do the data handling. This may not sound correct at first but that keeps us within standard practice. Just do NumericsClass nc = new NumericsClass(); in CH05EX16 and put get and set methods in, along with any method that helps you keep track of where you are. In other words, it goes where you have while( condition )

Do you have an editor?

Quote:
I must use JOptionPane and leave it open so that the user can continue to put in any number of locker without having to restart the program.
That is a JFrame or AWT program, that is what they do.

Quote:
So I have to figure out how to take the locker number and get its positive divisor. Then I must take all the positive divisors of the number and seperate them between the ones that are even and the ones that are odd numbers.
There is an absolute value method somewhere. Probabaly in java Math class. That, along with not starting off with a zero value should be your first few lines in ( Numerics class ) Using % to determine odd even is effective. What this program centers on is a widely studied problem of factoring numbers.

Quote:
I then need to output:
We can pass information in several ways. Do you know what a command line is? As well, we can have a data file, calcs can be initiated with a button in a JFrame program, a Dialog is a JFrame that does not stay on screen after the button is pushed. The way Dialog.getValue() works is the Dialog is reachable after the window on the screen is vanished.

Quote:
So I have to figure out how to take the locker number and get its positive divisor. Then I must take all the positive divisors of the number and seperate them between the ones that are even and the ones that are odd numbers.
That all goes in the Numerics Class
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 06:40 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org