Taylor Street

Greenhorn
+ Follow
since Jul 10, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Taylor Street

I think I answered my own last question. I was missing the point that once the int or factor was assigned x, that is how many zeros it will have prior to being multiplied by y or 10. Thanks so much for all the help. This forum is great!
14 years ago
Pete, thank you so much! It worked! With my additional System out, I am much closer to understanding. The first loop reads: When x was 5 the doStuff method returned a value of 500000. How can that be? "ivar" takes the value of "y" and the value of "y" is 10 and never changes.
14 years ago
Thank you for the reply David. It was very helpful! I have attempted to add some additional code to help me follow along with what is happening as the code runs for learning purposes; however, I can't seem to get it to compile correctly after I added the additional code and System out. What am I doing wrong? Maybe trying to learn the Java Util Logging (JUL) would be better?

14 years ago
This puzzle is tripping me up for some reason. For instance, result = result + obs[x].doStuff(x); Why the need for doStuff(x)? Also, it doesn't give "factor" a value and it seems it should have one? Here is the code and the output for the puzzle:

public class Puzzle4 {
public static void main(String[] args) {
Puzzle4b[] obs = new Puzzle4b[6];
int y = 1;
int x = 0;
int result = 0;
while (x < 6) {
obs[x] = new Puzzle4b();
obs[x].ivar = y;
y = y * 10;
x = x + 1;
}
x = 6;
while ( x > 0 ) {
x = x - 1;
result = result + obs[x].doStuff(x);
}
System.out.println("result " + result);
}
}
class Puzzle4b {
int ivar;
public int doStuff(int factor) {
if (ivar > 100) {
return ivar * factor;
}else{
return ivar * ( 5 - factor );
}
}
}

Output:
%java Puzzle4
result 543345
14 years ago