reset password
Author Message
wcwir
Posts: 75
Posted 19:17 Feb 28, 2019 |

Just starting the thread - don't be shy posting questions. This is not an easy homework.

306645006
Posts: 4
Posted 10:28 Mar 03, 2019 |

For Part 2 of HW 2, the angle rotation question, I was having trouble using Boolean operators to verify if the input was "+" or "-". I accepted the input as a string and am trying to make an 'if' statement. It doesn't seem like the computer is recognizing what the signs are. 

wcwir
Posts: 75
Posted 14:57 Mar 03, 2019 |
306645006 wrote:

For Part 2 of HW 2, the angle rotation question, I was having trouble using Boolean operators to verify if the input was "+" or "-". I accepted the input as a string and am trying to make an 'if' statement. It doesn't seem like the computer is recognizing what the signs are. 

If you are having trouble compiling, then you may be trying to compare String to char:

If you read the input in using .next().charAt(0)then you have it as char, and you have to compare it to '+' and '-' and not "+" or "-".

If you accept the input as a String (using .next()), and compare it to a String "+" using the == equality operator, the results are not always what you'd expect. Comparing Strings in Java is tricky! You'll find about it this coming week.

 

dliang
Posts: 35
Posted 18:53 Mar 03, 2019 |

Does anyone finish Question 4? I really confuse about the Hint, and I don't know how to limit the character range.

Attachments:
wcwir
Posts: 75
Posted 19:54 Mar 03, 2019 |
dliang wrote:

Does anyone finish Question 4? I really confuse about the Hint, and I don't know how to limit the character range.

You are seeing ? because the numbers you are generating correspond to characters Windows Command Prompt doesn't recognize (I think by default it uses UTF-8).

There are a few ways to limit the range of numbers you get from your random number generator -- the one in HINT is just one of them. I think in class I showed you another, using the .nextInt() method of the Random class:

Say you want the number you generate to be in range [3, 9] (inclusive). You make yourself an object of class Random, and call it randy:

Random randy = new Random();

then you can use it to generate a random integer, and if you know that you want the largest value to be 9, the argument that you pass to the .nextInt() method has to be 10. (Yes, this is kind of infuriating! But we don't make these rules...):

int myNum = randy.nextInt(10);

But then you'll discover that you get values of myNum as low as 0, and that's not what you want! So how do we fix this? Well, you can add 3 to myNum to make sure it is never lower than 3:

int myNum = 3 + randy.nextInt(10);

Oh but now you discover that myNum can be as big as 12! So what is the largest value you can add to 3 before you go over 9? It is six. So you want your randy to give you something between 0 to 6. So now you change what you ask of randy:

int myNum = 3 + randy.nextInt(7);

Why 7? Well because of this infuriating way nextInt() is defined by someone a long time ago. So now you will get what you want. But do you want to go through this every time you write code that needs a random number in some range? No you don't. So you can remember the formula: for random integral numbers in range [lowerbound, upperbound] (inclusive), you start with lowerbound, and add to it the result of random number generator that gives you a number from 0 to (upperbound - lowerbound). Of course if you want the largest value to be equal to (upperbound - lowerbound) you have to add 1 because of those evil long ago developers who made it like that! This checks out for our [3, 9] example:

int myNum = 3 + randy.nextInt(9 - 3 + 1);

OK, so now how to generate a random number in range. But how to pick the range? There are two ways. You can look up what decimal numbers the characters you want correspond to. What are the decimal values for 'a' and 'z'? What are the decimal values for 'A' and 'Z'? Or -- and here is where Java is making it easier for you -- you can just use 'a' and 'z' and 'A' and 'Z' char literals as if they were already numbers! No need to look up their decimal values, as Java will do that for you!

wcwir
Posts: 75
Posted 20:04 Mar 03, 2019 |

A couple of things from r/ProgrammerHumor:

Thanks to Keenan for showing me this illustration of what debugging feels like. You are not alone with this feeling! Programming is often a humbling experience: you think you got it, you write it and run it and ... oh no!

And related to the above: please test your code before you submit it:

Last edited by wcwir at 20:05 Mar 03, 2019.