• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with factors  RSS feed

 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a program that allows the user to input an integer. Use the Scanner class and keyboard/console input. Use a loop to print the factors of that number separated by the word "and". For example, the factors of the number 24 should print as the following:

1 and 2 and 3 and 4 and 6 and 8 and 12 and 24

You may assume that the input value is greater than 0.
Your program should print nothing if the empty string ("") is passed.


Okay, this is the hardest problem I've come across (after learning Java for 4 and a half weeks now) 

So far I have this:




Any ideas on how to make this work?   am I even on the right track?  thanks so much
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob,
you are reading the input before printing the message(Enter the number) so how can a user know that he is required to enter a number.
you have not shown your logic to solve it.try to solve it on a piece of paper,just like you did it in your real life.your next step should be to frame the solution in terms of java language(or any,write some pseudo code).
show some efforts from your side.you can hit a reply button any time if you feel difficulties at any stage.
Anyway.don't put your entire code in main,for reasons click here→Main is a Pain.

Praveen
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are these homework assignments or are you self learning?

Are you reading any chapters from a book to go along with lectures, if in fact this is homework?
you should put a little more effort in trying to break down the problems on paper first and then try to make that happen in code

But I guess every programmer learn differently. I love to try coding first even if is something really hard or something I haven't seen before. When I am absolutely stuck and after I have tried various ways of solving it do I post here for help!
No substitute for hard work. Remember, the only shortcuts are at the butcher shop🍖

 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Andres wrote:. . . I love to try coding first even if is something really hard or something I haven't seen before. When I am absolutely stuck and after I have tried various ways of solving it do I post here for help! . . .
That is an effective way of adding a zero to the time required to create a program
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Daniel Andres wrote:.
That is an effective way of adding a zero to the time required to create a program


Lol sorry I didn't get it. Effective way of adding a zero as in spending more time in order to program instead of asking for help ?

Or adding a zero as in enough practice on your own eventually leads to better understanding and less time in finishing a program?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, a good substitute for hard work is smart work. I think Campbell is talking about increasing the time you spend working on the problem by tenfold.
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yes of course it is. But getting your hands dirty is always better than asking for help first before attempting to solve a problem.

This reminds me of a student in one of my classes that found a similar program online and simply copied and pasted instead of doing the assignment. He said: work smart, not hard.

The problem with this is that you are not practicing nor are developing your own algorithms. For beginners I believe it is better to work really hard so that you learn how to manipulate the code so that when you get stuck and ask for help, you can see where you made a mistake and have an "ahhhhh that's how it's done" moment instead of asking for a solution first.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Andres wrote:I love to try coding first even if is something really hard or something I haven't seen before.

I'm curious...if you had to write a novel, would you start by writing random words, or would you try and think through what you're going to write first?

Programming is not about typing. It's about thinking.  You should think through things A LOT before you type a single character.  I would estimate i spend 70-90% of my time thinking about what i'm going to type, 5% typing it, and then the rest thinking about why what I typed might be wrong.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, let's reel this in before it gets out of hand. Let's look at the positive sides of the statements that have been made:

1. Working hard and getting your hands dirty -- there's nothing wrong with experimentation and trying different things. You just have to balance that with some of the other things that have been mentioned. You have to be systematic and organized in the way you conduct experiments.
2. Programming is about thinking - absolutely agree. Combining thoughtful consideration of the problem with experimentation is the basis for Test-Driven Development, a very effective technique for developing software in a systematic and organized way.
3. Increasing the time by tenfold - also agree, if by "just getting your hands dirty" means that we're not planning and organizing our attack of the problem.

Does that get everyone on the same page now?
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Daniel Andres wrote:I love to try coding first even if is something really hard or something I haven't seen before.

I'm curious...if you had to write a novel, would you start by writing random words, or would you try and think through what you're going to write first?

Programming is not about typing. It's about thinking.  You should think through things A LOT before you type a single character.  I would estimate i spend 70-90% of my time thinking about what i'm going to type, 5% typing it, and then the rest thinking about why what I typed might be wrong.


That was precisely my intended message. My choice of words was not correct. I never sit down and code without thinking throughly first of what I need to accomplish. Then I put it to paper if is something complex or relatively long so that I could work out the hard parts and solve them on paper and make small methods and specify how the are to be connected to each other before I sit down and program. Sometimes I may spend 30 or 40 minutes trying to debug but that is different. That is something that suddenly went wrong with the code. I try to debug and if I can't I'll ask for help.
When I said: I love to try coding first I really meant what I explained above.
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was by no means trying to create a conflict here! I simply adviced the OP to try a little harder and think through the problem (on mind and paper) and then try to convert it to code.

Sorry if my message did not get across the way i meant it.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To that end, here's what I'd suggest to the OP:

1. Don't try to solve everything all at once. If you do, you'll likely end up confused and your code will likely be a big mess that reflects that confusion.

2. Break the problem down into smaller pieces. Create separate methods that focus on solving each smaller piece. Getting user to provide the number you want to factor is just one part of the solution, therefore, that code you wrote is best placed in a separate method rather than directly in main().

3. Try to experiment with the other things you're required to do. Do your experiments in isolation. That is, write a smaller program where you have some numbers and you try to print each number separated by " and ". Once you know how to do that, you can use what you learned to write a method that does that in the bigger program, except now, you pass in the numbers as a parameter instead of hard-coding them like you would have in your smaller test program.

4. Use the same approach as #3 for the other things you need to do.

5. Everything comes together in your main() method by calling all those separate methods you've written to solve each part of the problem. You'd pass the return value from one method along to the next method as appropriate until you get your final answer.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Andres wrote:That was precisely my intended message. My choice of words was not correct.

Writing specs is hard, isn't it?  ;-)

What's 100% clear in the authors mind is always open to interpretation by the reader. 

I will be the first to admit I tend to take a pretty hardline, literal interpretation of things i read - it's a hazard of the job.  My apologies.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I will be the first to admit I tend to take a pretty hardline, literal interpretation of things i read - it's a hazard of the job.

Indeed, it often comes from experiencing the consequences of Murphy's Law more times than you'd like.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Andres wrote:. . . a student in one of my classes that found a similar program online and simply copied and pasted . . . He said: work smart, not hard.  . . .
Round our way, that is a sure‑fire recipe for an overall mark of 0. Not at the end of the mark. As the whole mark.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Andres wrote:Are these homework assignments or are you self learning?

Are you reading any chapters from a book to go along with lectures, if in fact this is homework?
You should put a little more effort in trying to break down the problems on paper first and then try to make that happen in code

But I guess every programmer learn differently. I love to try coding first even if is something really hard or something I haven't seen before. When I am absolutely stuck and after I have tried various ways of solving it do I post here for help!
No substitute for hard work. Remember, the only shortcuts are at the butcher shop🍖



Yes it's for a class.  I've looked in my textbook, and had my lecture,  but we didn't go over something as complex as this :/ and being new to java, and a visual learner this class has been very difficult for me!  but Java is so interesting to me that I'm powering through!   and I've requested a tutor for the class haha   but I need this last program done in the next few days!  I'm going to be working on it all night tonight.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way to tackle the complexity of a problem is to break it down into smaller, simpler problems. Again, don't make things difficult for yourself by trying to solve everything all at once in one place. Step though the solution and solve one thing at a time. Getting the user input is one step. What's the next small step after that?
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:Jacob,
you are reading the input before printing the message(Enter the number) so how can a user know that he is required to enter a number.
you have not shown your logic to solve it.try to solve it on a piece of paper,just like you did it in your real life.your next step should be to frame the solution in terms of java language(or any,write some pseudo code).
show some efforts from your side.you can hit a reply button any time if you feel difficulties at any stage.
Anyway.don't put your entire code in main,for reasons click here→Main is a Pain.

Praveen


Okay,  so I've been working on it.  and I have this so far:





It works!   BUT  when it prints I get this extra "and" at the end.  Is there a way to tell Java that after the last number to not print "and" again?    not knowing java makes figuring out these parts very difficult. haha


Please enter any number to find its factors: 87
1 and 3 and 29 and 87 and

that's the output.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:Jacob,
you are reading the input before printing the message(Enter the number) so how can a user know that he is required to enter a number.
you have not shown your logic to solve it.try to solve it on a piece of paper,just like you did it in your real life.your next step should be to frame the solution in terms of java language(or any,write some pseudo code).
show some efforts from your side.you can hit a reply button any time if you feel difficulties at any stage.
Anyway.don't put your entire code in main,for reasons click here→Main is a Pain.

Praveen






I DID IT.  I was just messing around and it worked!!!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done

Now what you shou‍ld really have made is a MyInteger class with a getFactors method, which might return an array with all the factors in. If that is too complicated give it a printFactors method which does what you have done there. You shou‍ld move as much code as possible into classes and away from the main method.

Don't give your classes the same name as classes in the java.lang package (you haven't done that here) and don't give your variables the same names as classes (you have done that here). Variable names shou‍ld always start with lowerCaseLetters.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to avoid variables declarations on single line as this:
better change it to:

Never ever write again:

Jacob Sousie wrote:it worked
Hang on, did it compile to you?

Jacob Sousie wrote:
Revise that part, I'm not sure it is something correct. Can't you end up with? 1 and 2 and 3 and 4 and and 6 and and and and and 12

Understanding your intense, even the form you write "if (Number > i  )" many would find confusing, that would be better "if (i < number)".
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Sousie wrote:BUT  when it prints I get this extra "and" at the end.  Is there a way to tell Java that after the last number to not print "and" again?    not knowing java makes figuring out these parts very difficult. haha


Well, again, you don't need to be a Java expert for this question either. It's just a matter of deciding in your mind what has to be done in what order. So...

When you print one of the factors, do you know whether it's the last one you're going to print? (Hint: Yes, you do. Or at least you can find out, can't you?) If so, then print the factor but don't print the trailing "and" in that case.

If you don't know whether it's the last one you're going to print, then there's an alternate solution: Print "and" before every factor except the first one.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, right. You figured that out, I just didn't read your later post completely. Your all-caps and exclamation marks show that you agree with most of us, problem-solving is FUN!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Sousie wrote:. . . I get this extra "and" at the end.  Is there a way to tell Java that after the last number to not print "and" again? . . .
There is a kind of object which does that. Look for a StringJoiner.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!