## Tuesday, January 31, 2012

### Loops Example: Wheel of Fortune

In the following video I show a more complicated example of using a loop. I write a program that simulates flipping the letters as done by Vanna White in Wheel of Fortune (that is, as done in the game of Hangman).

loops example: wheel of fortune

### Nested For Loops Example

Nested for-loops are very very useful.

## Monday, January 30, 2012

### Homework 2 Solution

You can view my solution to HW2. Notice how loops make programs shorter and more general: this solution will work on secrets of any length, just change `secretLength`.

### Lab 7: Flower

For this lab you will draw a pretty flower. Start by copying the code below into a new class called `Flower`.

```/**
* Flower.java
* @author Jose M Vidal
* Created on Jan 24, 2012
* The skeleton code for building a drawing application
*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Flower extends JPanel {

public void paintComponent(Graphics g){
final int centerX = 300; //the centerX and Y coordinates of our flower.
final int centerY = 300;
final int ovalWidth = 100; //the width of the oval. A petal is an oval.
Graphics2D g2 = (Graphics2D) g;

//TODO: Your code goes IN HERE (not in main), ALL OF IT. Replace the code below.

//draw one oval of length 300pixels from center to edge
g2.setColor(Color.black);
g2.fillOval(centerX, centerY - ovalWidth / 2, 300, ovalWidth);
//rotate the coordinate system by 15 degrees, around the centerX/Y.

//draw a second oval
g2.fillOval(centerX, centerY - ovalWidth / 2, 300, ovalWidth);

//draw a third oval
g2.fillOval(centerX, centerY - ovalWidth / 2, 300, ovalWidth);

}

// You do NOT need to change anything in main(). Leave it as is.
public static void main(String[] args) {
JFrame frame = new JFrame("Window"); //frame is the window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Flower panel = new Flower(); //panel is the graphics area where we can draw

frame.add(panel); //put the panel inside the window
frame.setSize(640,640); //set the window size to 640x640 pixels
frame.setVisible(true);
}
}
```

When you run that program it will pop up a window which will look like the one pictured here on the right in Figure 1. As you can see, this program draws 3 black petals. Each petal is actually just a filled-in oval, which we draw using the `fillOval()` method. We then rotate the coordinate system by 15 degrees around the center using `rotate()`. Thus, when we draw the second oval it appears rotated down, as a petal in a flower might.

For this lab you will modify that code so that your new program instead draws a flower like the one shown in Figure 2. You will do this by replacing the code inside the `paintComponent` method (you do not need to change anything in `main`). Replace the code in there that draws the 3 ovals, with a loop that draws 275 ovals. Follow these steps:
1. Change the rotation from 15 degrees to one ϕ of a circle, which is 137.5 degrees. See the video below for why this is the number Nature uses.
2. Add a loop that draws 275 successive ovals, each one 137.5 degrees from the previous one.
3. The first oval should be of a length 300 (as the ones in the code above are), the next one 299, the next one 288, and so on.
4. Make the color of the ovals alternate: `Color.blue` then `Color.cyan` then back to `Color.blue` and so on. For the first 250 petals.
5. The last 25 petals are all `Color.yellow`.
6. Finally, to get the black outline on the petals, simply draw a black oval that is an extra 2 pixels in width and length, then draw the colored oval as usual.

Hint: You do not need to read the documentation to find more `Graphics` operations. All you need is: `setColor`, `fillOval` and `rotate`, as shown in the sample code. Do read the documentation for those 3 methods.

This lab was inspired by Vi's video below, but note that she draws her flowers starting with the smallest petal, while your code starts with the biggest petal and proceeds to smaller and smaller petals. We do this because it is much easier to draw over something than it is to draw behind something.

If you are like me, you can't help but play around by changing some of the numbers to see what comes up. Below are some of the flowers I made by just changing the code around a bit.

If you make some pretty ones that you want to share, just email them to me or post them on the web somewhere and let me know. I will add them to our flower gallery right here! BTW, here is how to take-a-screenshot.org

## Sunday, January 29, 2012

Below I show how to write a loop that adds up some numbers the user gives us. This is a very frequently used pattern.

## Saturday, January 28, 2012

### Loops Example: Making Change

In an earlier lab you wrote a program that makes change, using math (the remainder operator). In the video below I show how to write a program that calculates the amount of change needed using loops.

making change with loops

### Break, Continue, Scope, and Enum

On Wednesday's lecture we talked about the `break` and `continue` statements. Watch the video below if you want a refresher.

break and continue

We also talked about scope. The video below explains what 'scope' means.

variables' scope

On Monday's lecture we will talk about one more Java language feature, the `enum`. It is explained in the video below.

enum

## 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 to print out the 8 sets of numbers (that is, you cannot just cut-n-paste these numbers into your program).

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.
You are Cold
You are Cold
You are Hot
Warm
Warm
Warm
You are Hot
You are Hot
Warm
Warm
Warm
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

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.

### Udacity: Yet Another Online Place to Learn to Code

In an earlier post I gave a list of a bunch of websites where you can learn to program for free. There is a new one called Udacity where you can take CS 101 and learn to program and build a search engine in just 7 weeks.

Check out the video and see Sergey Brin (after a hard night of partying it looks like) talk about how everyone should learn to code.

### Homework 2: MasterMindz

For this homework you will implement a variation of the Mastermind board game, where we are using digits instead of colors and changing the rules a little bit. In this game your program will generate a random 4-digit number that the user then has to guess (use `Math.random()` to generate a random number). After the user makes a guess your program will tell him:

1. how many of the digits in his guess are correct, that is, they have the same value and in the same position as in the secret,
2. how many digits in his guess are not found in the secret.

Below is a sample run. Notice that the program is telling the user the secret. This is useful for debugging. The program you turn in should also tell the user the secret so as to make it easier for us to grade it.
```Guess the 4-digit number I am thinking of.
(the secret is 9712)
1245 has 0 digits in the correct position.
1245 has 2 digits that are not found anywhere in the secret number.
9999 has 1 digits in the correct position.
9999 has 0 digits that are not found anywhere in the secret number.
5555 has 0 digits in the correct position.
5555 has 4 digits that are not found anywhere in the secret number.
9779 has 2 digits in the correct position.
9779 has 0 digits that are not found anywhere in the secret number.
9721 has 2 digits in the correct position.
9721 has 0 digits that are not found anywhere in the secret number.
1234 has 0 digits in the correct position.
1234 has 2 digits that are not found anywhere in the secret number.
9712 has 4 digits in the correct position.
9712 has 0 digits that are not found anywhere in the secret number.
Congratulations! You guessed correctly.
```

This homework is due Monday, Jann 30 @noon in the dropbox.cse.sc.edu

## Friday, January 20, 2012

Our texbook describes all the methods we will be using in this class, but so does the official java documentation. You can view the official Java documenation on any browser by clicking on the Java Documentation link on the top-right of this page. On that page you want to click on "java.lang" on the top-left window.

More conveniently, all the documentation you need is also on eclipse. The video below shows you how to get at the Java documentation from eclipse, as well as how to make your own javadoc documentation for your own programs.

### Booleans and Switch: plus Eclipse tips

On Monday's lecture we will cover chapter 3: the if-else statement, booleans, and the switch statement.

Booleans

The Switch Statement

The video below talks about the different Java comments and shows you how to set up eclipse so that it automatically adds your name at the top of every new file.

## Wednesday, January 18, 2012

### If else

If-else Statements

### Lab 4: Distances

For this lab you will write a program that asks the user for a distance in various units and then translates that distance into meters. Here are some examples of the program output:
```Enter distance:1m
You entered 1.0 meters.

Enter distance:33.12m
You entered 33.12 meters.

Enter distance:1.456cm
You entered 0.01456 meters.

Enter distance:1234.88mm
You entered 1.2348800000000002 meters.

Enter distance:5.1234km
You entered 5123.400000000001 meters.
```

The user can only type a number (a double) followed by a "m", "km", "cm", or "mm".

Hint: use `if-else` to check the last two characters in the user's String. If it is not "km" or "cm" or "mm" then you can assume the it must be "m", meters.

Hint: to turn a string into a double use `Double.parseDouble()`.

Hint: remember that 1m equals 100cm, and 1m equals 1000mm, and 1km equals 1000m.

The homework assignments will be graded out of 10 points. The grade assignment will follow these guidelines:

• If it works for all inputs, and has the student(s)' names on a comment, has at least a couple short comments explaining what it does, and is properly indented then 10 points.
• If it works for all inputs but is missing one of the other requirements then 9 points.
• If it does not work for some inputs or in some cases but works in others, then 8 points.
• If it does not compile then it will get a maximum of 7 points, maybe less depending on how much working code is there.

## Tuesday, January 17, 2012

To access your CEC network drive (H:) from outside the college you have to first join the VPN, as the instructions explain.

## Sunday, January 15, 2012

### Lab 3: Windows

In this lab you will take the code you wrote in the previous lab, or my solution if you could not do it, and change it so that it uses a popup window instead of the console. Listing 2.12 of the textbook shows you how to do this. Also, the code below will pop up a window asking for the user's name followed by another window with the "Your name is.." message.

```/**
* @author Jose M Vidal
* JOptionPane minimal example
*/

import javax.swing.JOptionPane;

public class MainJoptionpane {

public static void main(String[] args) {
String name = JOptionPane.showInputDialog("Enter your name:");
JOptionPane.showMessageDialog(null, "Your name is " + name);
System.exit(0);
}
}
```

After you get it working with the code from Lab 2, you will extend the program so that it accepts a double as input, which represents the number of dollars and cents, and then prints the amount of paper currency and coins the person will get. For example, if the person asks for 1.1 he will get 1 dollar and 1 dime. If he asks for deci-cents or less (for example, .001) you can just truncate those off.

The two screenshots shown on the right show you how your program should look like when run.

Hint: To get a line break in a String (so the next character show in the next line down) use the `\n` special character. For example, in the screenshot the first line comes from the string `"You will get:\n"`.

As always, when you are done turn in your .java file in dropbox.cse.sc.edu.

### Instructional Videos

The video below also shows how to build an application with JoptionPane

Optional video, just for learning: If you want to email the applications that you build in this class to other people so they can just double-click on them and run them (on windows, macs, and linux) then check out the video below which shows you how to export the apps you build with eclipse.

## Saturday, January 14, 2012

### Windows Programmer

Yet another place where software developers will find employment: programming windows.

### HomeWork 1: Tweet to Leet

For the first homework you will implement a simple twitter message to Leet translator. You will need to use the String class (Chapter 2) and if-else statements (Chapter 3) which we will cover the week of Jan. 16. Your program will ask the user for a tweet, change all the characters in the tweet to lower case, then it will print out that tweet but translated to Leet using the substitutions below.

Replace thiswith this
and&
a4
b8
e3
g6
l1
o0
s5
t7
z2

The message can also contain up to one hash tag. A hash tag starts with a # then some non-space characters, then ends with a space. So #hello is a hashtag but # by itself is just a #. Your program will find and print out this hash tag, if there. Also, the hashtag should not be changed into Leet. You can assume that there is at most 1 hash mark in the message the user types in.

Finally, you will print out the length of the tweet and if it is more than 140 characters you will print out an error message.

Below are some examples.

```I will translate your tweet to Leet.
Enter your tweet:Hi there! Just a simple tweet to get started.
No Hashtag found.
Translation: hi 7h3r3! ju57 4 5imp13 7w337 70 637 574r73d.
Message has 46 characters.

I will translate your tweet to Leet.
Enter your tweet:This one has a #hashtag Notice how it remains the same in the final message.
Hashtag: #hashtag
Translation: 7hi5 0n3 h45 4 #hashtag n07ic3 h0w i7 r3m4in5 7h3 54m3 in 7h3 fin41 m355463.
Message has 77 characters.

I will translate your tweet to Leet.
Enter your tweet:The empty # is not a hashtag and should be left alone.
No Hashtag found.
Translation: 7h3 3mp7y # i5 n07 4 h45h746 & 5h0u1d 83 13f7 410n3.
Message has 53 characters.

I will translate your tweet to Leet.
Enter your tweet:If you type a really long tweet then the program will still translate it but will give you an error because there is a 140 character limit #145rocks
Hashtag: #145rocks
Translation: if y0u 7yp3 4 r3411y 10n6 7w337 7h3n 7h3 pr06r4m wi11 57i11 7r4n51473 i7 8u7 wi11 6iv3 y0u 4n 3rr0r 83c4u53 7h3r3 i5 4 140 ch4r4c73r 1imi7 #145rocks
Message has 149 characters.
ERROR: Too big to tweet.

```

Remember that work on pairs if at all possible.

This homework is due Monday January 23 @noon in the dropbox.cse.sc.edu.

## Thursday, January 12, 2012

### Strings, Scanners, and Eclipse Comments

On Wednesday's lecture we will talk about String and the keyboard input. By Wednesday you should have read Chapters 1, 2, and 3 of the textbook and watched the following screencasts.

Strings

Scanner

### Lab 2: ATM

For this lab you will implement a program that would run inside an ATM machine and determines what bills to give the person. This machine, however, is from the future and is able to dispense \$1, \$5, \$10, \$20, and \$50 bills. Your program will ask the user how much money he wants to withdraw and then print out the minimum set of bills required to produce this amount.

For example:

```Enter the withdrawal amount:77
You will get:
1 fifty dollar bills
1 twenty dollar bills
0 ten dollar bills
1 five dollar bills
2 one dollar bills
```

For this and all future programs, the top of your .java file should have the name and email of both of you (pair programming), and the name of the file, as such:

```/**
* Main.Java
* @author Jose M Vidal <jmvidal@gmail.com>
* @author Jane Bigbrain <bigbrain@email.sc.edu>
* Created On Jan. 12, 2012
*
*/
```

As always, you will turn in your lab at dropbox.cse.sc.edu, under Lab 2 this time.

## Wednesday, January 11, 2012

### Pair Programming

We will be using pair programming for the labs and the homeworks. The specific requirements for this class are
1. Pair up with whomever you want. Try to pair up with a few different other students during the semester. Try to pair up with someone who has about the same level of experience as you.
2. When you turn in your lab or homework remember to put both of your names at the top of the file.
3. It is not required that you pair up. You can work by yourself if you want to, but note that studies have shown that students who pair up do better in introduction to programming classes.
4. One of you will be the driver who types in the code, while the other is the navigator who looks on, spotting errors and thinking higher-level thoughts. You should switch roles every 30 minutes or so.
5. It is hard for two people to see one laptop screen. So, if you are working on your laptops for the homeworks consider using the screen sharing (macs) or remote desktop (windows and linux) that is built into your Operating System. You can turn these on and allow one laptop to see what the other one is doing.

### Binary Hand Dance

A fun followup to Monday's lecture.

Q: What is the biggest number you can represent using just two hands?

## Tuesday, January 10, 2012

### Lab 1: Hello World

Use your favorite browser to get to the lab instructions. It is a good idea to set a bookmark at http://www.csce145.com as everything for this class will be posted there. If you use a feed reader, subscribe now to both the posts and the comments.

1. The laboratory computer systems run the Microsoft Windows operating system. Because it is connected to a server in the College (running Windows Server), you have a login, password, and a "profile" that can follow you around to whatever computer you use within the engineering domain (ENGR). Computer labs are located in Swearingen and 300 South Main (across the street), but our labs will meet in Swearingen 1D15.

3. Note the desktop (i.e., the appearance of the screen). Everyone’s desktop is identical now, but it’s your desktop. Take an inventory of the things on the desktop. You can change it as you wish at your convenience.

4. What you need for CSCE 145 is primarily a web browser, Java, Eclipse. These are all already installed in the lab machines.

5. Double click on the My Computer icon to see what you have. In particular, there are several drives: such as C:, H:, P:, and Z:. We will concentrate on drive H:. Note, if you are going to work from home (you most likely are) then I recommend you use dropbox to backup and synchronize your files across your computers.

6. Open the H: drive by double-clicking on it. Create a new folder on H: called csce145; open the csce145 directory by double-clicking on it; now create a lab01 directory.

7. You are next going to place an icon on the desktop for eclipse. Locate the eclipse folder by clicking on the Start menu, then Programs, CSE Apps, and eclipse. Click on the eclipse icon using the right mouse button. From the menu that appears, choose "send to" and then "Desktop (shortcut)". This will place a shortcut to the eclipse development environment on your desktop for ease of use.

8. Right click the eclipse shortcut that you just made and select properties. Go to Start in and put in the home directory that you made on your H: drive earlier (e.g.: `H:\csce145`)

9. Open eclipse by double-clicking on its icon on the desktop. If the "Welcome" window appears, click on the icon for the "Workbench." Eclipse will request that you "Select a workspace." Choose the default, which should be `H:\csce145\workspace`. If it is not there, type it in and click OK.

10. Use File/New/Project and then choose Java Project and click next. In the window that pops up, choose a project name of lab01 and click next.

11. Finally click finish at the bottom.

12. For the lab this week, you will write a Java program to do simple interaction with a user (you, in this case).

13. Right click on lab01 in the left Package Explorer frame and choose "new class". In the window that pops up, choose a meaningful name for your class (e.g., FirstProgram) and type it in the Name box. In the "which method stubs would you like to create?" section, check the box for `public static void main(String[] args)`. Click the Finish button.

14. A new class file will be created with the basic code you will need for your assignment. Include the following statement at the beginning of the class so that you can use the Scanner class: `import java.util.Scanner;`

15. Then, just below the line with `public static void main(String[] args)` type:

```System.out.println("Hello out there.");
//two forward slashes means the rest of the line is a comment!!!!

//make a String variable with your    first name -- change this to your first name
String firstName = "Jose";
//make a String variable with your last name -- change this to your last name
String lastName = "Vidal";

//make a char variable that indicates the grade you want to make in this class
char grade = 'A'; //note single quotes ' around characters

//Finally output all of that information to the screen
System.out.println("My first name is " + firstName + " and my last name is " + lastName +
" and if I study hard, I will make a(n) " + grade + " in CSCE145!");

//prompt the user to input two numbers to add
System.out.println("I will now add two integer values, please enter the two integer values separated by a space: ");

//now we need to create and connect a Scanner object to the keyboard
Scanner keyboard = new Scanner(System.in);

//now we can use the keyboard object to retrieve values from the user
int value1 = keyboard.nextInt();
int value2 = keyboard.nextInt();
int result = value1 + value2;

System.out.println("The result of adding " + value1 + " and " + value2 + " is: " + result);

System.out.println("Goodbye");
```

If you make a typing mistake you can correct it with the backspace or delete key. If you have positioned the cursor somewhere else, then you can use the four arrow keys to get back to the correct line.

16. Use the File/Save to save and compile the program. If there is an error while compiling your code it will show up in the tasks window of eclipse workbench with a red cross in the first column, description and line number where it occurs.

17. Navigate to Run/Run As/Java Application menu to run your program. Run your program a few times so that you can see it work. Try to understand what each line of code is actually doing. Congratulations! You've just written and run your first Java program (at least in CSCE 145).

18. Finally, show your work to the TA and submit your lab to dropbox.cse.sc.edu.

You should also install eclipse on your home machine.

## Monday, January 9, 2012

### Java Variables and Expressions

In the following screencasts I talk about Java syntax, variables, types and typecasting, and expressions. This material is covered in Chapter 2 of the textbook.

Java Syntax and Java Expressions

Variables

Types

### Online Resources for Learning Programming

I created a bunch of screencasts (link also on the right sidebar under "Videos") which I will be posting here as we cover the relevant material. However, if you don't like my screencasts you have a plethora of other choices. Below I list the ones I've found that I like the most.
1. Our textbook has a website which you can log in using the code written inside the textbook. Once logged in you can watch the videos they provide.
2. The New Boston has a long list of Java screencasts similar to mine, but with a Boston accent.
3. The Khan Academy, of course, has a bunch of computer science videos. He uses Python.
4. Hacker CS has videos on Java and data structures. Most of what they have right now is too advanced for 145 (but perfect for CSCE 146).
5. Harvard's CS 50 is the fourth largest class at Harvard. It covers C, PHP, and JavaScript plus SQL, CSS, and HTML. You can view the lectures online. They are awesome.
6. MIT's 6.00 is their intro to programming class. They have their lectures online. They use Python.
8. Codeacademy has a really cool webapp for teaching JavaScript. They are also hosting codeyear.

## Friday, January 6, 2012

### Getting Started with Eclipse and Java

You should read Chapter 1 and 2 of the textbook during the first week of classes. Chapter 1 is a quick overview of how a computer works. Chapter 2 introduces the basic concepts of computation: variable, types, and expressions.

The first screencast goes over eclipse and how to run your first "Hello world" Java application. You should watch it before the first lab, which is on Tuesday. Also, remember to install eclipse on your own computer.

Java "Hello World" using Eclipse