Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Im stuck "Calculating Strenght"  RSS feed

 
jens jarl
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, this threads title might be a bit "off" but I really had no idea how to title this, but I digress..
Im working on some java for a class im doing and I am supposed to make a program that asks for and reads in a integrer and calls on a method(strenght) with this number as the argument.
This method is supposed to give this number the following treatment: If the number is dividable with 2, it will be halved, if not it will be multiplied by 3 and then this result will be increased by one.

This process will be repeated until the result is 1.
The method will then count how many of these "treatments" the input number was exposed to, this amount will be called "strenght" to the opposite number. The method returns the numbers strenght.

Example if the input number was 14:

Treatmeant Number:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Result:
7 22 11 34 17 52 26 13 40 10 5 16 8 4 2 1

At this point the program will this value(17) and return it from the method, causing the final output to look something like this:

Input the number that will be checked: 14
The number 14 has a strenght of: 17


This might be a bit confusing as im trying to translate this assignment to english in my own words, ill try to eloborate further if there is confusion. My main problem with this task is how im supposed to to the part that I bolded, i am pretty sure that I need to use some sort of loop possibly connected to "if" statements but I can't wrap my head around how to actually accomplish this, especially how im supposed to "test" if a number can be divided or not. This work is due soon and I can't seem to find a way to work this out, the increasing amount of stress it is giving me is not exactly helping me ether.
If someone could help me understand how I am supposed to solve this ill be deeply thankful.
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with if/else constructs.



I have the final solution, work on it and I will help you further.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The important thing to do is to break things down, and do them a step at a time. So, look at the statement you had bold:

(If) (the number is dividable with 2), (it will be halved), (if not) (it will be multiplied by 3 and then this result will be increased by one.)
that can be interpreted as
(if) (some condition is met ) (do something) (else) (do something else )

I kind of broke it up. so if you look at this, it is the EXACT structure of an if/else statement



When you get to writing the code, i would suggest concentrating only one part at a time. So, I might only write the if-part, and simply put a "System.out.println()" statement in the else block stating "it's not divisible by two, i'll fix this later".

That lets you concentrate on one part only. You can then test the HECK out of it to make sure the 'even' side works. Once you know that is rock solid, you can then start working on the odd side, and not worry about the even side anymore.

Hopefully, you'll see that you should have a method that does nothing but takes in a number and returns it converted. that is ALL it should do. You then have another method that wraps up the logic behind calling it. In other words, you want to separate the logic behind WHEN to call it from the logic of WHAT is done to the number.

You write code in discrete pieces, and you want to make sure each part works before you start coding the next.
 
jens jarl
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help so far, ive learned a few things but I still can't seem to figure out how to do this.
The "divide" term suggested, and multiply gives me the error "cannot be resolved to a type" and I don't know how to fix this.(Ive never used these before).
Ive been thinking about how to loop it and I belive that using a while loop would be my best bet, am I totally out of the barn here or am I right in thinking this?

This the code so far:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wesleigh did not mean for you to literally type "divide n by 2;". He was providing what is called "pseudo-code". It is supposed to give you the idea of what the code is doing - it isn't actual java (or any other programming language).

Ive been thinking about how to loop it and I belive that using a while loop would be my best bet,

This concerns me. When you write code, you shouldn't be thinking about the language and what it lets you do. You should be thinking in English (or whatever your native language is). If it were me, I would literally start like this:

get a number from the user
as long as the number isn't 1
if the number is even
divide it by 2
otherwise
multiply by three and add one.
keep track of how many times I have to repeat the above
When the value is finally 1, print how many iterations there were.

so now, I look at that. I start to see things that may become methods. I see I need some way to get a number from a user. I would code that - AND NOTHING ELSE - and make sure it works. I would get it, and make sure I can print it back out.

Once that is done, i would see that I need a method that computes a new value. so, I'd then write a method that takes an integer, computes the new value, and returns the newly computed value. I'd test the heck out of it. I'd test it with negative numbers. I'd test it with 0. I'd test it with 1,2,3,5,10,1000.... I would ONLY want it to compute the next value. So, an input of "2" would return "1". an input of 3 would return 10. an input of 42 would return 21.

Only when I am sure that is working would i consider doing anything else.

I never write more than 2-3 lines before I compile and re-test. I write a TON of code I throw away, that validates what my code is doing. I want to see each and every step, so I write hundreds of System.out.println() statements that I delete 2 minutes later, but are there at least once when I run my code.

You are trying to do too much at once. Writing code is a very incremental thing. you need to be sure the last line of code you write works before you write the next.
 
jens jarl
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it might be psuedo code, but my lack of experience caused me to be unsure, thanks for clearing that up.
Im trying to do it in the way you suggested, but for some reason I just can't seem to get this line of code to work, do you know whats wrong?
Ive never done this with a scanner before, just typing strenght(14); is easy and it works but when I try doing the same with the scanner I just get a variable error.

 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jens jarl wrote:I figured it might be psuedo code, but my lack of experience caused me to be unsure, thanks for clearing that up.
Im trying to do it in the way you suggested, but for some reason I just can't seem to get this line of code to work, do you know whats wrong?
Ive never done this with a scanner before, just typing strenght(14); is easy and it works but when I try doing the same with the scanner I just get a variable error.



You never declared the variable properly, what is strenght (btw isn't it strength?) isnt it an int? i.e



also there are a few general issues you should look at fixing, don't name your variables and methods the same, read up on the general conventions, I would have used something like public static void printStrength(){ }

so it is a noun verb pair that is descriptive with the second word starting with a capital.

Likewise with your class name, the words should all be capitilised, i.e OppGaveOne or ThisIsMyClassName etc
 
jens jarl
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, I did't know it was so important to keep captializing everything, ill try to do that from now on, thanks.
I am trying to return a value to the method so that it can be used in the methods statements, thats why I named them the same, hence why I talked about using strenght(14);
But im supposed allow the user to input the number, and this is what never works for some reason.

This code does not print the statement in the strenght method.


But the code beneath does: printing out the "The Strenght:" and the count variables value.
The only difference between the code is that the int strenght = input.nextInt(); is replaced with strenght(14);
Also if I write some extra code in the strenght method, it won't show up ether.
This is pretty much a mystery to me, I have no idea what is causing it.



Also I am sorry if I am asking too many dumb questions, as you can probably see im fairly new to this.

EDIT:

I managed to solve it by marrying the solutions:


But id still very much like to hear if there are other ways to solve this.
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is because in the first code you never actually call the method, you did write out the method but it is never called. Also it is good practise to call a static method by using the class name, ie Classname.staticMethodName(); for example when you call the Math methods it is Math.random(); etc.

so in your code you need to call the method and pass in the argument you have now created.



you see how confusing the repetition of naming is above?

edit - I see you solved it, well done
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so look at this code:


I cleaned it up just a little, but left it basically how you wrote it. When you run your program with "java Oppgaveone", you start in the main() method, and go through things one line at a time. So, we would start on line 4. args would be empty. We hit line 6, and print "Enter a integer:"
We hit line 7, and create a Scanner called "input".
We hit line 8, and read in the value the user types, saving it in the int variable 'strenght'.

We hit the close brace on line 9....and the program ends.

So yes, you need to call the method if you want it to run, as you figured out.
 
jens jarl
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help, but I kinda still have the main issue to resolve.
Ive been working on fixing it and ive been able to do a few things to get the code closer to completion, but I still do not know how im supposed to keep the code from running until the "count" variable hits 1. Ive been playing around with the "while" loop but I always seem to end up stuck in some infinitve loop or it ends up just not working. I can't see if im just doing it wrong or if I should use some different way of reaching my goal.. Then the final problem would be to count up the amount of times the loop have rolled into the b variable to find out the "strenght" of the number, and here I am really lost, got no idea how Im supposed to do this.

Any assistance I could get would be very helpful at this point.

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jens jarl wrote:Ive been playing around with the "while" loop but I always seem to end up stuck in some infinitve loop or it ends up just not working. I can't see if im just doing it wrong or if I should use some different way of reaching my goal.. Then the final problem would be to count up the amount of times the loop have rolled into the b variable to find out the "strenght" of the number, and here I am really lost, got no idea how Im supposed to do this.


As you already mentioned, yes, you need a loop -- but you don't have a loop in the example that you presented to us. Can you show us some of the "while" loop examples that you have been "playing around" with?

Henry
 
jens jarl
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example.

 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jens there are a few problems with your code, just for example match up your braces "{}" and see after the while();{{ // why is there 2 opening braces for example? and why a semi colon?

also % - that does not mean divide - that is modulos it means to return the remainder of, go read up on operators in java. so when i gave you the condition if (num%2 == 0) what this really means is return the remainder of my number of 2 and if that is equal to 0 then the number is even (i.e it is divisible by 2 with no remainder, get it?)

I have helped you a bit here but you really need to try understand what I have done



say you are stuck in the loop and it is not doing what you want, you can put in System.out.prinltn(""); statements at each point to print the value of a variable and then it will be easier for you to see where it is going wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!