Results 1 to 17 of 17
  1. #1
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default 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

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default 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:

    Java 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.
    Java 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.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    "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.

  4. #4
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default

    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:

    Java 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:

    Java 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:

    Java 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);

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default 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?

    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.

    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.

    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.

    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
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    Moniboniz is offline Member
    Join Date
    Feb 2009
    Location
    Venezuela
    Posts
    1
    Rep Power
    0

    Default Hello www.java-forums.org visitors...

    Hi people... :)

  7. #7
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Moniboniz: Only post if what you're saying is relevant. There is a different sub-forum for introductions, this isn't it.
    OP(Original Poster): I have just recently wrote a program for many factor calculations ("divisors"), here is my method that gets the factors. Note there is a few things called there that are internal to my program that I'm not showing, but look at mine and see what I used to accomplish this and then try doing it yourself.
    Java Code:
    private int[] getFactors(int number) {
            ArrayList<Integer> factorList = new ArrayList<Integer>();
            int numOfFactors = 0;
            Integer[] factors;
            for(int i = 1; i <= number; i++) {
                if(i % number == 0) {
                    factorList.add(new Integer(i));
                    numOfFactors++;
                }
            }
            factorList.trimToSize();
            if(numOfFactors == 2) isPrime = true;
            else isPrime = false;
            factors = new Integer[factorList.size()];
            return integerArrayToIntArray(factorList.toArray(factors));
    }
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    there needs to be some kind of zombie post indicator here...

  9. #9
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    What has he already figured out how to get the factors? Or did I not understand you?
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default yeap... Zombie alert...

    Danger, Will Robinson, Danger ...!!!

    huh... maybe a little too dramatic for a zombie alert?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    it's a relatively old topic. and when a topic hasn't been touched for half a year, i think it means that the discussion is over. not sure why (or how) newcomers manage to often dig up old posts to introduce themselves.

  12. #12
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    OOh didn't see date.. Its so annoying how people introduce themselves like this I've seen it quite a few times, not digging up old ones but just posting it in a random thread its like they don't have common sense...
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  13. #13
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    haha its even scarier when you think about them becomming future programmers.
    Why doesn't my door unlock? Why is my freezer spitting out ice every night at 12am?
    Does anyone know why the zune got locked up on newyears eve?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  14. #14
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Talking The Master at work.

    After discussing agreement with the occasional random hello posting non-existent telephone numbers for discounted converter boxes As Seen on TV, we can see the work of Masters emerging at those random posts:
    Quote Originally Posted by angryboy View Post
    haha its even scarier when you think about them becomming future programmers.
    I think they did already, note typical systems you see routinely. e.g. - nt will not reload a dll ..... and so on.
    Quote Originally Posted by angryboy View Post
    Why doesn't my door unlock? Why is my freezer spitting out ice every night at 12am?
    How come my TV set only snows on me as of yesterday?
    Quote Originally Posted by angryboy View Post
    Does anyone know why the zune got locked up on newyears eve?
    Whois zune?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  15. #15
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you're right, but i was only referring to those new members who post randomly, hijacking old threads.

    what is a zune?
    You're gearing up to create a killer playlist on your 30GB Zune for your annual New Year's bash. All of a sudden, your Zune locks up, reboots itself, and freezes. What the heck is going on? ... a bug in the internal clock driver related to the way the device handles a leap year.
    FROM: Microsoft Says Leap Year Bug Caused Zune Failures - PC World
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  16. #16
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    I think the Zune is MickeySoft's equivalent to iPOD....

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  17. #17
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Talking what about the crawlers at the bottom

    Quote Originally Posted by angryboy View Post
    you're right, but i was only referring to those new members who post randomly, hijacking old threads.
    well what about the crawlers at the bottom when one posts - sometiimes they are very old ( 6+ mo ) and I took a few of them recently as the poster never got a reply. I know I have waited patiently sometimes for months hoping for a reply, as much as a year, and go back and check the work. It is rare, but sometimes the poster comes back with copious gratuity and I just try to pick the good ones.

    I don't care much about whether op had a good frame, what it is is that some of the most profound questions in cs are touched by newbies sometimes - though it may need to be moderated I took one at jr and ( ... will pm you ... ) it can get combative, maybe an angry boy can answer what to do ....

    some poster came in at jr asking what to do as new team lead, ...... I almost racked a pinion on that one, got into a discussion with someone who had ran a business for years. It went on for pages, I told my Branch Manager at the bank about it and she really grasped the subtle issues. Actually, that is not unlike what we are doing here with the big diff that op is gone and we have a better skill level in the codase. ( offhand employment for coder base )

    I'm gonna be a definite league above where you are at but you are showing skills already that make for productive work, given: If you were New Team Lead - what would you do about the occasional intruder who posts non-existent telephone numbers for deeply discounted digital television converter boxes. Would you assume that person is from a derivative markets firm, just got home from work and wants to make distractions? I doubt it, I met one of those persons back when automated tele-dialers were new tech ~ many of them have an aversion to work that is systemic - they will do remarkable things, if it is to avoid work. Counter intuitive is the professional teminology, but actually it is deeper than that.

    When you have a New Team Lead, there are issues involved that can only be handled by adroit skills that find their base in 50,000+ hours in a field. You do not have that, efforts to work the issue of where the border line is may assist in resolution of where the authorities on this forum want to go. You have already laid against IDE's, a common idiom in the cs field. When I started coding, I blew into the bowels of STL - before I could code int someint = 0; In there, I saw something anyone who works around machinery would recognize as a potential runaway pointer. Later, I got into a discussion with a cryptographer from a major institution. The result is that I got cited in scientific literature.

    Let's see if you can get cited, is there anything you are excited about?

    Other than the hack you mentioned.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •