Friday, January 27, 2012

Homework 3: Magic

For this homework you will write a program that guesses what number the user is thinking of. Below is a sample transcript:
Think of a number between 1 and 255.
I will guess the number you are thinking of.
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 
57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 
107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 
147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 
187 189 191 193 195 197 199 201 203 205 207 209 211 213 215 217 219 221 223 225 
227 229 231 233 235 237 239 241 243 245 247 249 251 253 255 
Is it in the list above? (yes or no): yes

2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31 34 35 38 39 42 43 46 47 50 51 54 55 
58 59 62 63 66 67 70 71 74 75 78 79 82 83 86 87 90 91 94 95 98 99 102 103 106 
107 110 111 114 115 118 119 122 123 126 127 130 131 134 135 138 139 142 143 146 
147 150 151 154 155 158 159 162 163 166 167 170 171 174 175 178 179 182 183 186 
187 190 191 194 195 198 199 202 203 206 207 210 211 214 215 218 219 222 223 226 
227 230 231 234 235 238 239 242 243 246 247 250 251 254 255 
Is it in the list above? (yes or no): no

4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 36 37 38 39 44 45 46 47 52 53 54 55 
60 61 62 63 68 69 70 71 76 77 78 79 84 85 86 87 92 93 94 95 100 101 102 103 108 
109 110 111 116 117 118 119 124 125 126 127 132 133 134 135 140 141 142 143 148 
149 150 151 156 157 158 159 164 165 166 167 172 173 174 175 180 181 182 183 188 
189 190 191 196 197 198 199 204 205 206 207 212 213 214 215 220 221 222 223 228 
229 230 231 236 237 238 239 244 245 246 247 252 253 254 255 
Is it in the list above? (yes or no): yes

8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 40 41 42 43 44 45 46 47 56 57 58 
59 60 61 62 63 72 73 74 75 76 77 78 79 88 89 90 91 92 93 94 95 104 105 106 107 
108 109 110 111 120 121 122 123 124 125 126 127 136 137 138 139 140 141 142 143 
152 153 154 155 156 157 158 159 168 169 170 171 172 173 174 175 184 185 186 187 
188 189 190 191 200 201 202 203 204 205 206 207 216 217 218 219 220 221 222 223 
232 233 234 235 236 237 238 239 248 249 250 251 252 253 254 255 
Is it in the list above? (yes or no): yes

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 48 49 50 51 52 53 54 55 56 57 
58 59 60 61 62 63 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 112 113 114 
115 116 117 118 119 120 121 122 123 124 125 126 127 144 145 146 147 148 149 150 
151 152 153 154 155 156 157 158 159 176 177 178 179 180 181 182 183 184 185 186 
187 188 189 190 191 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 
223 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 
Is it in the list above? (yes or no): no

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 
58 59 60 61 62 63 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 160 161 162 163 
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 
184 185 186 187 188 189 190 191 224 225 226 227 228 229 230 231 232 233 234 235 
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 
Is it in the list above? (yes or no): no

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 192 193 194 195 
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 
Is it in the list above? (yes or no): yes

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 
248 249 250 251 252 253 254 255 
Is it in the list above? (yes or no): no

You are thinking of 77

The program prints out 8 sets of numbers. Notice the pattern:
  1. starting at 1 print the number, skip the next one, print the next one, skip the next one, etc.
  2. starting at 2 print the next 2 numbers, skip the next 2, print the next 2, skip the next 2, etc.
  3. starting at 4 print the next 4 numbers, skip the next 4, print the next 4, skip the next 4, etc.
  4. starting at 8 print the next 8 numbers, skip the next 8, print the next 8, skip the next 8, etc.
  5. etc

You will implement this using nested for loops.

You can then calculate the number the person is thinking of by simply adding up the first number in each one of the lists the user said "yes" to. In the example above this would be 1+4+8+64 = 77.

The homework is due on Monday, February 6 @noon at the dropbox.cse.sc.edu as usual. Yes, this is the same time as your Test 1.

Thursday, January 26, 2012

How does a Computer Work?

The following two videos describe how a computer works and the Java Virtual Machine (JVM). They complement Chapter 1 of the textbook. There will not be any questions directly about the contents of Chapter 1 in the test, but it is helpful for this class, and in Life, if you understand the basics of how a computer works.


If you are interested in building your own PC, I recommend watching the newegg.com video tutorials. As you'll see, building a PC is just a matter of buying your parts (CPU, RAM, motherboard, case, etc.) and then plugging them together. Its lots of fun. I made mine super-silent.



Eclipse Debugging and other Tips and Tricks

Now that you are familiar with the basics of editing and running a Java program in eclipse, you might want to learn how to use a few more advanced features of eclipse. Note that you will not be tested on these, they are purely optional. However, I do note that these tips make my life much more pleasant and save me a lot of typing.

First, the video below shows you how to use the eclipse debugger. The debugger is especially useful when you are trying to figure out why a loop in your Java code is not doing what you thing it should be doing.

Eclipse debugger



The video below is a collection of keyboard shortcuts and other eclipse tips and tricks that I think are very useful, especially for beginners. If you were wondering what keyboard shortcuts I use in lecture, the video below explains.


Wednesday, January 25, 2012

Lab 6: Hot or Cold

For this lab you will implement a simple game where your program 'thinks' of a number between 0 and 1000 and the user has to guess it. The user will keep trying to guess until he finally gets it right, at which point your program will end. Below is a sample run of the program:
I am thinking of a number between 0 and 1000. Try to guess it.
Your guess:500
You are Cold
Your guess:600
You are Cold
Your guess:700
You are Hot
Your guess:800
Warm
Your guess:650
Warm
Your guess:720
Warm
Your guess:700
You are Hot
Your guess:710
You are Hot
Your guess:720
Warm
Your guess:690
Warm
Your guess:715
Warm
Your guess:705
You got it! Congratulations!
Game Over

As you can see, your program will tell the user how far his guess is from the correct answer using hot-or-cold words, the closer he is the hotter he is. Specifically, if the user is
  • a distance of less than 5 from the correct answer then he is Burning
  • less than 10 then he is Hot
  • less than 100 and he is Warm
  • less than 300 he is Cold
  • otherwise he is Freezing

Also, remember that the program must end after the user guesses correctly.

Hint: To generate a random number use Math.random(), then multiply that by 1000.

Hint: To get the absolute value of a number (the positive of it), use Math.abs().

As always, you will turn in your finished lab at the dropbox.cse.sc.edu.

Tuesday, January 24, 2012

Test Practice and Bonus Points: Codingbat

The codingbat is a fun way to practice your coding skills.

Pretty much all the questions they have in the their Java sections Warmup-1, String-1, String-2, Logic-1, and Logic-2 are candidates for a Test 1 question. You can view your progress by clicking on "done" at the top of the screen. You can also share this page with the world (these are my results) to demo your l33t cod3rz skillz.

If you want, you can create an account there and then click on "prefs" (at the top) and in there set "Share to" to jmvidal@gmail.com. That way I will be able to see what problems you have done. I will then give you 1 extra test point (for Test 1) for each one of the 5 sections above that you finish before Monday Feb. 6 @noon.

Monday, January 23, 2012

Loops: While and For

On Wednesday's lecture we will be covering most of Chapter 4 which deals with loops: while loops and for loops.


While loops



For loops



Lab 5: Roommate Decision Tree


For this lab you will implement the (slightly modified) roommate decision tree you see here (from CollegeHumor). Notice that I crossed out one of the paths. Click on the image to see the full resolution version.

Your program will ask the user these questions in the specified order and will then tell the user what kind of roommate he is. Below are some example interactions.

I will tell you what kind of roommate you are.
Do you have a job? (YES or NO)
yes
Are you in a relationship? (YES or NO)
yes
Does you bf/gf have a roommate? (YES or NO)
yes
You are The Lover

I will tell you what kind of roommate you are.
Do you have a job? (YES or NO)
no
Do your parents love you? (YES or NO)
no
You are The Freeloader

I will tell you what kind of roommate you are.
Do you have a job? (YES or NO)
yes
Are you in a relationship? (YES or NO)
no
Oh geez, did you get dumped again? (YES or NO)
yes
You are The Derelict

As always, you will turn in your lab in the dropbox.cse.sc.edu, Lab 5.

Homework 1 Solution

Here is my solution to HW1. Please look at it and compare it with your solution.

We will not be taking off points for "style" issues in this class as our goal is just to learn to write programs that work. However, for those of you that want to get better at programming, notice that if you had the line .replace("e","3") more than once in your program then you were violating the DRY principle (Don't Repeat Yourself). Namely, every piece of knowledge should appear only once in your program.

In this case, the 'knowledge' that e turns into a 3 must appear only once in your program. There are two main reasons for this:
  1. If the knowledge changes in the future you will have to change the code in multiple places. For example, if we wanted e to turn into a 3 or # then we would have to make this change in multiple places. This change might have to be made by a different person from the one who initially wrote the code, so it might not be obvious, and he might just change it in one place, thus introducing a nasty bug.
  2. Because it is harder to change the program, we are now less likely to do so, even if it would be profitable. We humans are lazy, illogically so sometimes. If you can make your program easy to change then you will change it lot more, new features will be added, more bugs will be fixed, more costumers will be pleased, mo' money in your pocket. If your program does not change, it dies.