Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

factor program

 
Ashley Griffin
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My instructions for my program is:
Create an application that will repeatedly read in positive numbers from the user, until the user enters a 0. For each number, the program will list out the proper factors of the number.
I can't figure out how to work all the kinks out of my program. For example when I enter a number like 9, it return 9, 1.
Here is my program:
 
Henry Wong
author
Marshal
Pie
Posts: 20834
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashley Griffin wrote:Hi,
My instructions for my program is:
Create an application that will repeatedly read in positive numbers from the user, until the user enters a 0. For each number, the program will list out the proper factors of the number.
I can't figure out how to work all the kinks out of my program. For example when I enter a number like 9, it return 9, 1.


Hint: is there a difference when you enter 9 the first time? And when you enter 9 after you enter 1 to continue?

Henry
 
Ashley Griffin
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I not quite sure that I understand your hint.
 
Henry Wong
author
Marshal
Pie
Posts: 20834
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashley Griffin wrote:I not quite sure that I understand your hint.


How about this? When you first run your program, your divisor starts off as 2, and get incremented. When you continue -- meaning the same program is processing another number, what is the value of divisor starting at? Or what should the value of divisor start at?

Henry
 
Ashley Griffin
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The divisor should increase by one during each run starting from 2 to 3 and so on.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are trying to do too much at once here. The most important thing one needs to learn to be a programmer is how to separate out the various pieces of the project into their component parts. For example, I would (at minimum) break this down into at least three parts:

1) Prompt and get input from a user exactly once.
2) loop indefinitely until some flag value is set (in your case, it will be a "0")
3) assuming you have an integer, find and print the factors.

notice that each of these can be written without a concern for what or how the other two work. #3 doesn't care if you get the data form a user, read it from a file, query a database, or pick a number at random - all it cares is that you pass it an integer, and it will find/print the factors. Personally, I would probably even split it into 3a) find the factors and return them as a list, and 3b) given a list of integers, print them out.

So what I would do is start over. use what you've written as a crib, but begin fresh. The I'd start by having a main method that only calls another method - something like "private static int getUserInput()". This method doesn't need any parameters passed to it, but will return an input. Then have your main print the value it gets back.

On your first draft of getUserInput(), have it do nothing but return value of "5". Once you know you can call it, get back a value, then you can refine it to prompt and get input from the user, and return that. Eventually, you'll have it working perfectly.

ONLY WHEN IT DOES WORK PERFECTLY should you consider adding the code for #2. It would be as simple a writing a loop around the calling of the method, and checking the returned value.

ONLY WHEN THE LOOPING is working perfectly, add in a call that gets/prints the factors. Again, my first draft of getFactors() would probably do nothing more than print "i'm finding the factors of X" - where you print out the value you are trying to factor.


The point here is that each piece needs to be developed separately. It is an iterative and refine process - never try to write you code in one fell swoop.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic