Saturday, May 19, 2007

PA4 FAQ (Part I)

Q: Who will be the grader for this project?
A: Daniel. So, questions regarding the requirement and grading criteria should be directed to him. (and be nice to him :)

Q: Should I work on this project in Unix?
A: No, you shouldn't. It is a little tricky to get graphic programs running remotely on a unix machine. You will need an X Windows server installed on your computer, and you will have to do some simple configurations. (I probably can show that in my office hours if you are really interested.)

Q: Is MouseMotionListener needed?
A: No.

Q: I have no idea where to start. Can you help?
A: I would take the following steps...
1) Develop an applet that allows users to play the game. Don't worry about the buttons. Don't worry about the status messages at the bottom.
2) Add the status bar message by using the showStatus method.
3) Add the status message (Move #, % red, % green, whose move it is). There are (>=) two ways you can do this: by using JLabel or by calling drawText method from inside the paint method. I think drawText is simpler.
4) Put the four buttons at the bottom. Use setLayout(null) and setBounds to specify the locations of each button.
5) Implement each button's functionality. Start with the Reset button, then the Color button, and the Computer vs. Human button.
6) Test everything. Write the README.txt file. Test everything again.
7) Attempt the extra credit only if you are sure everything else is working correctly.
8) Turn it in and smile :)

Q: Step 1 seems like a big step. I don't know how to do it. Can you help?
A: Here you go ...
[Spoiler Warning!]

Monopole is your main applet. The logic of the main program is here. The paint method is here. Think about other classes as helper classes. Write them and use them at your discretion. The recommended classes are explained on the assignment sheet.

Can you get by with putting everything on Monopole.java and not writing other classes? Yes, you can probably make your program work, but it'll probably be messy and your style points may suffer (up to Daniel's judgement).

Here is how I would do it. Keep in mind that there is no need to follow me. This is just an example, and it may not even be the best one.

I will develop a simple applet that accepts the mouseClicked event. I'm not worried about the colors, the Square, and the Board at this point. I will first try to keep track of all the poles that the users place on the board.

I will write a simple class Move that contains the x, y coordinates of each move, and the type of the charge (positive or negative). Then I will declare a 1-dimensional array of Move (and call it moves) -- the size should be at least 42. I will need another integer to keep track of how many moves are currently stored.

Whenever the user clicks at some point, I'll create a new Move and append it to the moves array. Now, to visualize it, I override the paint method of the applet. I paint a circle for each of the move.

I have got a simple applet that interacts with the user. Now it is not hard to write the Board class which contains a 150x150 array of Squares. Each square should contain its coordinate on the board, as well as a value of type double to store the net charge acting on it. Each time the user clicks on the board, I can re-evaluate the whole Board and draw a little rectangle (using fillRect method) on the screen for each of the squares, with the color depending on the net charge.

After this point, it's not so complicated.

Q: Can I have more hints?
A: My next office hours are 3-5pm on Monday, and the extra office hours are:
Tuesday, May 22: 1 PM - 4 PM (Daniel)
Wednesday, May 23: 10 AM - 1 PM (Mock)

Dr Jacobson added that:

"It is ok to get a 0 on this project. Not everyone in the class is a CS
major. Not everyone is expected to be able to do every project. Just DON'T
COPY anyone's work, do your best, and remember, the class is curved."
:)

Thursday, May 10, 2007

PA3 Laundry List

Here is a laundry list of what I will be looking for when I grade PA3. The list is derived from the assignment sheet.

FUNCTIONALITY
- You have exactly 50 happy faces.
- They move at reasonable speed.
- They bounce when they hit the wall.
- They die when they are clicked.
- There is a text at the top and at the bottom.
- The text is in big font.
- The counter works.
- The "You won" screen appears when all faces are killed.

CODE
- You have good style.
- You put reasonable amount of comments.
- HappyFace.java handles the drawing of happyFace.gif
- HappyZapper.java is a JApplet, implements MouseListener, ActionListener. It uses Timer to move things. It contains an array of 50 HappyFace objects.

Monday, April 30, 2007

HW2 Grading Notes

------------------
HW2 Grading Notes
by Mock Suwannatat
------------------
4/30/2007

CRITERIA:

Part A: 20 points
- Maximum 5 points if code does not compile.
- 5 test data, 4 points each
* data 1: in example, easy
* data 2: in example, hard
* data 3: not in example, medium
* data 4: 0 way
* data 5: wrong input
- Up to 2 points may be deducted for style

The actual test data can be found at
http://www.cs.ucsb.edu/~mock/cs10/hw2-test-data.zip
The files rb.# are input, robotOut.# are sample output.

Part B: 20 points
- Maximum 5 points if code does not compile.
- 5 test data, 4 points each
* data 1: in example, very easy, mod 2, 10 rows
* data 2: mod 9, 50 rows
* data 3: mod 7, 50 rows
* data 4: mod 3, 50 rows
* data 5: wrong input: 0 9 9 9
- Up to 2 points may be deducted for style
- Up to 8 points are deducted if you do not use JOptionPane


The actual test data can be found at
http://www.cs.ucsb.edu/~mock/cs10/hw2-test-data.zip
For Robot, the files rb.# are input, robotOut.# are sample output.
For Pascal, the files pc.# are input, pascalOut.# are sample output.


COMMENTS YOU SEE OFTEN:

- Lines too long: this means that your lines go over 80 character long, which makes it hard to read on my terminal. Truncating long lines nicely is a work of art, and you should learn it.
- Funny indentation: this means that you have inconsistent indentations, or you may be using tabs instead of spaces. Keep in mind that tabs may be set differently on different environment. The best way to make sure your code look good everywhere is use spaces, and avoid using tabs. Many editors support this. (It can be set that when you press Tab, spaces are inserted.)
- Lacking comments: you should put some comments
1) at the top of the file to summarize what the program does
2) at the top of each method to explain what it does, what it returns, and what each parameter is.
3) along the program to explain tricky logic

ROBOT:

Note that the small step may be bigger than the entire distance, in which case the number of ways is zero. These values are not illegal and your program should take them.

Some of you did not try to match the messages and output exactly with the sample runs. This makes grading very difficult as I cannot use my script. The worst thing is when your order of input is different. This could lead to errors in grading which negatively affect you.

PASCAL:

The assignment clearly states that 2 and 9 are valid inputs. Many of you reject the value 2 and 9.

The assignment also states that you ask for the modulo first, then the number of rows. Some of you did it in a different order. This would make your program fail all the tests.

Some of your programs produce negative numbers when number of rows is large. This could fail 3 tests.

Some programs do not work when n = 50, missing the last number of the last row. You should have spotted this error. You should always test your program on boundary cases.

Thursday, April 26, 2007

HW2 FAQ (round 2)

------------------------------------------------------------------------------
This is an automatic message sent to all CS-10 students, Spring 2007
------------------------------------------------------------------------------

Greetings,

There are not many questions in this email, but I think it's worth a spam.

QUESTION: Do we need to use recursion in Part B (Pascal.java)?
ANSWER:
No, you don't need to use recursion for Pascal. You can use it, but it is not encouraged. The solution code uses some for-loops and arrays. Please refer to Dr Jacobson's email for his hint on Pascal. I also copied his hints and posted it online:
http://cs10s07.blogspot.com/2007/04/dr-jacobsons-hints-on-pascal.html
Note that your program needs to run very fast even for 50 rows of the triangle.

QUESTION: Are there some sample runs for Robot.java?
ANSWER:
Yes, Dr Jacobson sent it out some time back. I posted it online on my blog:
http://cs10s07.blogspot.com/2007/04/robot-sample-run.html
It would be great if you can try to match the output. If not, it will be hard for me to grade. But I'm not allowed to take points off on that, though. However, my eyes may get dizzy and there could be some errors in grading, which will cause you some hassles of having to come to my office hours and fight for your points back ;)
(I won't intentionally do that, of course.)
One reminder: don't forget to check for the negative values, and that small < med < large

QUESTION: Are there some sample runs for Pascal.java?
ANSWER:
Yes, they are here.
http://cs10s07.blogspot.com/2007/04/pascal-sample-run.html
Again, please try to match the output. Please just print out the triangle and no extra messages. Do not forget to check the input and keep asking until the user enters the right values.

Best,
- Mock

Dr Jacobson's hints on Pascal

Dr Jacobson wrote an email to the class with the following hints. If you have no idea how to approach Part B of hw2, this hint may come in handy.
I used two arrays:

int[] thisRow = new int[50];
int[] nextRow = new int[50];

I initialized the first row to be 1,0,0,...0
I printed out theRow.
I then used the formulas to figure out the nextRow.
I copied nextRow back to thisRow.
I printed out thisRow.
And so on...

Pascal sample run

Some sample runs of Pascal are given here:
Note that your program should only print out the triangle and no extra message.

If you enter modulo = 2 and row = 20, you'll get...

1
1 1
1 0 1
1 1 1 1
1 0 0 0 1
1 1 0 0 1 1
1 0 1 0 1 0 1
1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 1 1
1 0 1 0 0 0 0 0 1 0 1
1 1 1 1 0 0 0 0 1 1 1 1
1 0 0 0 1 0 0 0 1 0 0 0 1
1 1 0 0 1 1 0 0 1 1 0 0 1 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1

If you enter modulo = 3 and row = 20, you'll get...
1
1 1
1 2 1
1 0 0 1
1 1 0 1 1
1 2 1 1 2 1
1 0 0 2 0 0 1
1 1 0 2 2 0 1 1
1 2 1 2 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2 4 6 3 6 4 2 4 1 5 1
1 6 6 5 6 6 1 0 0 1 6 6 5 6 6 1
1 7 3 2 2 3 7 1 0 1 7 3 2 2 3 7 1
1 8 1 5 4 5 1 8 1 1 8 1 5 4 5 1 8 1
1 0 0 6 0 0 6 0 0 2 0 0 6 0 0 6 0 0 1
1 1 0 6 6 0 6 6 0 2 2 0 6 6 0 6 6 0 1 1

Robot sample run

F:\CS-10\PA2>java Robot
Enter small step: 2
Enter medium step: 3
Enter large step: 5
Enter distance: 20
Number of ways the robot can move a distance of 20
takings steps of length 2, 3 and 5 is: 448

F:\CS-10\PA2>java Robot
Enter small step: 3
Enter medium step: 2
Enter large step: 1
Illegal values: small < medium < large
Enter small step: 3
Enter medium step: 6
Enter large step: 9
Enter distance: 6
Number of ways the robot can move a distance of 6
takings steps of length 3, 6 and 9 is: 2

F:\CS-10\PA2>java Robot
Enter small step: 3
Enter medium step: 7
Enter large step: 17
Enter distance: 50
Number of ways the robot can move a distance of 50
takings steps of length 3, 7 and 17 is: 695

F:\CS-10\PA2>java Robot
Enter small step: 7
Enter medium step: 17
Enter large step: 34
Enter distance: 100
Number of ways the robot can move a distance of 100
takings steps of length 7, 17 and 34 is: 192

F:\CS-10\PA2>java Robot
Enter small step: 5
Enter medium step: 17
Enter large step: 53
Enter distance: 200
Number of ways the robot can move a distance of 200
takings steps of length 5, 17 and 53 is: 198045

F:\CS-10\PA2>java Robot
Enter small step: 5
Enter medium step: 13
Enter large step: 37
Enter distance: 200
Number of ways the robot can move a distance of 200
takings steps of length 5, 13 and 37 is: 5244091

F:\CS-10\PA2>java Robot
Enter small step: 5
Enter medium step: 13
Enter large step: 37
Enter distance: 25
Number of ways the robot can move a distance of 25
takings steps of length 5, 13 and 37 is: 1

F:\CS-10\PA2>java Robot
Enter small step: 5
Enter medium step: 13
Enter large step: 37
Enter distance: 12
Number of ways the robot can move a distance of 12
takings steps of length 5, 13 and 37 is: 0
F:\CS-10\PA2>