Wednesday, June 6, 2007

Final exam review



Final exam review:

-----
"Immutable"
How to make a class immutable?
What is a sample class?

-----
Override vs. Overload?

Consider the following program. Which overrides which? Which overloads which?

public class A {
public void f() {
System.out.println("1");
}

public void g(int x) {
System.out.println("2");
}
}

public class B extends A {
private void f() {
System.out.println("3");
super.f();
}

public void f(int x) {
System.out.println("4");
}

public void g() {
System.out.println("5");
}

// can we have this method?
private void g(int x) {
System.out.println("6");
}
}

Consider the following calls. Which method is called at each line? Which line is an error?

A a = new A();
B b = new B();
a.f(5);
b.f();
b.g(2);
((A)b).f();
A.f();

Answers:
a.f(5); => not compiled
b.f(); => 3, 1
b.g(2); => 2
((A)b).f(); => 1
A.f(); => not compiled

-----
What is "this"?

public class C {
private void f() {
if (this.equals(null)) {
System.out.println("Duh?");
}
}

public static void main(String args) {
f();
OR this.f();
OR (new C).f();
}

}
-----
interface vs abstract class
What are the differences?
When to use which?
-----

Monday, June 4, 2007

PA5 FAQ1

PA5 FAQ1
By Mock

-----
QUESTION:
On a CSIL machine, how do I copy files from one directory to another?

ANSWER:
[Option 1]
Let's say you are in the destination directory. You want to copy all the files from ~teliot/PA5/ to the current directory. You can do this:
cp -r ~teliot/PA5/* .
The option -r stands for "recursively". It will copy all files and subdirectories from the source directory to the current directory. (Remember that the dot (".") stands for "current directory", and ".." stands for "the parent of current directory".)
[Option 2]
Let's say you are already in the source directory. You want to create a directory in your home directory called myPA5 and copy everything from the current directory to ~/myPA5. (Remember that ~ stands for "home". So, ~ is your home. ~teliot is home of teliot.) You can do this:
mkdir ~/myPA5
cp -r * ~/myPA5

-----
QUESTION:
What program can I use to transfer files between my home computer and CSIL?

ANSWER:
You can use the freely available WinSCP or FileZilla clients for PCs. For Macs, you can use Cyberduck (also free). Open the program and connect to csil.cs.ucsb.edu, using your own username and password. We do not officially support or endorse these programs. But I'm happy to help during my office hours if you are curious.

-----
QUESTION:
Can I write my program in Windows and upload it to CSIL? What could be a problem?

ANSWER:
No you should not. Dr Jacobson wants you to practice programming in Unix. You must make sure your code is readable in Unix (by cat, more, or less). The code must compile and the program must work in Unix.

But if you work in Windows, will we know it? The honest answer is "it depends."

One thing to keep in mind. Windows uses a combination of CR (Carriage Return, ASCII #13) and LF (Line Feed, ASCII #10) to represent new line. Unix uses only LF. So, if you open a Notepad-written file in Unix, you will see funny characters at the end of line. If you open a pico-written file in Windows, you will see all the lines crammed together.

How to fix this? Again, we do not support it. But these are two useful unix commands:
dos2unix [filename]
unix2dos [filename]
You can look at the man page for usage.

-----

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 1 2 1 2 1
1 0 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 0 1 1
1 2 1 0 0 0 0 0 0 1 2 1
1 0 0 1 0 0 0 0 0 1 0 0 1
1 1 0 1 1 0 0 0 0 1 1 0 1 1
1 2 1 1 2 1 0 0 0 1 2 1 1 2 1
1 0 0 2 0 0 1 0 0 1 0 0 2 0 0 1
1 1 0 2 2 0 1 1 0 1 1 0 2 2 0 1 1
1 2 1 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1
1 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 0 2 2 0 0 0 0 0 0 0 1 1

If you enter modulo = 5 and row = 20, you'll get...
1
1 1
1 2 1
1 3 3 1
1 4 1 4 1
1 0 0 0 0 1
1 1 0 0 0 1 1
1 2 1 0 0 1 2 1
1 3 3 1 0 1 3 3 1
1 4 1 4 1 1 4 1 4 1
1 0 0 0 0 2 0 0 0 0 1
1 1 0 0 0 2 2 0 0 0 1 1
1 2 1 0 0 2 4 2 0 0 1 2 1
1 3 3 1 0 2 1 1 2 0 1 3 3 1
1 4 1 4 1 2 3 2 3 2 1 4 1 4 1
1 0 0 0 0 3 0 0 0 0 3 0 0 0 0 1
1 1 0 0 0 3 3 0 0 0 3 3 0 0 0 1 1
1 2 1 0 0 3 1 3 0 0 3 1 3 0 0 1 2 1
1 3 3 1 0 3 4 4 3 0 3 4 4 3 0 1 3 3 1
1 4 1 4 1 3 2 3 2 3 3 2 3 2 3 1 4 1 4 1

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

Wednesday, April 25, 2007

HW1 & HW2 FAQ (round 1)

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

Greetings,

Sorry for yet another spam. But don't worry, this is not the last one. There will be more :D

You don't have to read it as it is mostly a clarification of what has already been said on the assignment sheet and the lectures. But if you feel like reading, here you go:

Today, I've got the following questions that I think are worthwhile.

::: FAQ :::

QUESTION: How do I know my grades for hw1?
ANSWER:
Uh? Don't you know it, yet? You're supposed to get it by email. If not, ask Dr Jacobson right away. Somehow, the email might have been lost in transit.

QUESTION: My triangle goes negative when n is big. Why?
ANSWER:
You are encouraged to re-read the "Warning and hints" section of the homework. Also, think about what Dr Jacobson said yesterday in class (when he opened Excel and put in some formula). If that doesn't help, then consider the following facts:
- The biggest value an integer can hold is 2^31-1. That's around 2 billion.
- If you add something together and the result is more than that value, it will "wrap around" and become negative.
- For any a, b, and k: (a + b) % k == ((a % k) + (b % k)) % k. Don't ask me why. It's a number theory thing. But it's useful to know for this assignment. To put it in human language: mod of the sum is the same as mod of the (sum of the mods). Oops, that sounds more confusing. Sorry. But the important thing is that the thing on the left side may exceed 2 billion if a and b are big. The thing on the right cannot get bigger than 2k, and you know that k is small (at most 9).

QUESTION: Do I have to indent my code nicely for this assignment?
ANSWER:
Yes. It is clearly stated in the "Notes" section of the handout. Please review my Java Style Guide.
http://cs10s07.blogspot.com/2007/04/java-coding-style-guide.html
For examples: limit line length to 80 chars. Don't use tabs, but use spaces. Be consistent on indentation levels. Stay away from long // comments. Prefer /* this style */ for multi-line comments. Use names that make sense. Make things private if possible.
Hint: I will be extra picky on the code that does not run correctly or does not compile. If you have a good style, it's easier for me to understand your logic and give you more partial credits.

QUESTION: Are comments required?
ANSWER:
Same as the previous question. Please write some comments, but don't write an essay. You won't be penalized from writing too much, though, but it's not good for your hands ;)

QUESTION: I hate Notepad.
ANSWER:
Is that a question? :P
Anyway, ...
Here are my personal recommendations. I do not endorse these products but I've used them and I'm happy with them. I'll be very happy to help during office hours if you need help with installation. But I may not reply emails about them (since it usually takes a long explanation).
- For Windows: I use Crimson Editor.
- For Macs: I use Smultron.
- For Linux: I use emacs.
They will help you indent better. They will do syntax highlighting. They are all free!


Best,
- Mock

Week #4 Discussion Notes

We will talk about:
The ASCII character set, Unicode

The slides I will use are here:
http://www.cs.ucsb.edu/~mock/cs10/ascii-unicode-lecture.zip

We will also do a problem similar to second part of homework 2. We will do this together in class, and I will not post the code. Only those who attend the discussion can see it.
;)

You are not allowed to share it with other people. As the problem is very related to your homework, sharing the code can be considered cheating. If you need hints and cannot make it to the discussion session, visit me or Daniel during our office hours.

Tuesday, April 24, 2007

questions about JOptionPane

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

Greetings,

A student asked me today about how to use JOptionPane. Another student
asked about how to convert a String to an int.

Here is an example program. You can cut and paste it on Sample.java to
try it out.

Best,
- Mock




/* We need to import the swing package in order to use JOptionPane easily
* Otherwise, we need to say javax.swing.JOptionPane every time we refer
* to it.
*/
import javax.swing.*;

/* Importing java.util package gives you access to Scanner */
import java.util.*;

public class Sample {

public static void main(String[] args) {
/* An example of how to use the JOptionPane input dialog */
String name = JOptionPane.showInputDialog("What's your name?");
System.out.println("Hello " + name);

/* An example of how to convert a String to an int
* I give you 2 ways (there are more). Both are equally good
* in my opinion.
*/
String s = "15";

/* Way #1: Using Integer.parseInt */
int x = Integer.parseInt(s);
System.out.println("x + 10 = " + (x+10));

/* Way #2: Using Integer.parseInt */
Scanner sScanner = new Scanner(s);
int y = sScanner.nextInt();
System.out.println("y + 10 = " + (y+10));

}

}

Base-of-Pascal-Pyramid Problem

Some students asked me for hints about the second part of PA2. I think a good
way to give hints is to do a similar, but not so exact, example.

The following problem will be a good practice for your homework. We will do
this together in my discussion tomorrow (9-10am, Phelp 1448) after I finish
talking about ASCII & Unicode.

Consider the following problem. I call it "Base-of-Pascal-Pyramid Problem".

You start with a 5x5 array like this:
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0

At each step, for each cell, you sum up the value in that cell with its
4 neighbors (N, E, W, S) to get the new value.

So, the next few steps will be:

After step 1:
0 0 0 0 0
0 0 1 0 0
0 1 2 1 0
0 0 1 0 0
0 0 0 0 0

After step 2:
0 0 1 0 0
0 2 3 2 0
1 3 6 3 0
0 2 3 2 0
0 0 1 0 0

After step 3:
0 3 4 3 0
3 8 14 8 3
4 14 18 14 4
3 8 14 8 3
0 3 4 3 0

Given n > 0 as input, we want to know what the array will look like after
step n.

We start with a pseudocode:
1. Declare a 5x5 array, initialize it to look like the starting configuration.
Call this array currentArray.
2. Have a backup array called nextArray of the same size.
3. Use nested for loops with variables i, j to go through each element.
For each (i, j), figure out what nextArray[i][j] should be.
4. Copy the values in nextArray to currentArray.
5. For n times, repeat steps 3-4.

Then we can write the Java code off that pseudocode.

About your homework grades

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

Dear CS10 Students,

I've received emails from many of you about your homework grades. I have replied all of them. However, I would like to say the following to the whole class:

There have been cases where, as careful as I am, I made obvious mistakes in grading. For example, one lucky student got the comment "Perfect :)" from me on Triangle, and received 0/15 for credits!

When these obvious errors happen, please email me as soon as you can. Please include your full name and the email address you used when you turned in your code. Please also include a copy of my comment. I will send you back an apology, and will ask Dr Jacobson to update your grade accordingly.

If there isn't an obvious error, but rather a question or a dispute about my grading criteria, I encourage you to visit me during office hours or make an appointment (in case you can't make it to my office hours). So you can negotiate your score more easily.

You are welcome to email me, but if your question is about:
- Why didn't I get more partial credits?
- Sorry I submitted the wrong file. Can you re-grade this?
- My program compiles on my machine. Why didn't it compile on yours?
- I tested my programs so many times. Why didn't it pass your tests?
- etc. (anything about my subjective evaluation of your work.)
I may reply you with the following email:
http://cs10s07.blogspot.com/2007/04/common-email-from-me.html


In short, I may ask you to visit me during office hours. You may or may not get points back as a result of your visit. But you will get a better understanding of your scores.

:)
Sincerely,
- Mock

a common email from me

Students are encouraged to discuss grading issues during my office hours. If you email me about grading, I may give you the following reply:

Dear Student,

You received this email because you asked me a question about the grading, relating to my subjective evaluation of your work.

- - -
First of all, I totally understand your concern. Spending so much time on an assignment and not getting enough credits for your effort is really sad. I've got that feeling several times myself when I was an undergrad, and that was a painful part of my learning process.

- - -
Second of all, let me clarify the grading process.

When a program passes the tests, the grades automatically come up. If it fails all the tests, it becomes the responsibility of the grader (me), to look at the code and evaluate it subjectively.

I would like to repeat the word "subjectively." It means that "it's up to me." Which, to many students, may sound like I can do it arbitrarily.

The matter of fact is: I do have some subjective guidelines. For example, the maximum I can possibly give for a program that fails all tests is around 4/15 points (that equals the maximum credits for non-compiling programs). The only programs to receive such credits need to show significant work toward project completion. Just a minor 1-line modification would have made your program perfect. In other words: it needs to clearly show that "you almost got it!"

My initial subjective evaluation of your program (both the output and the code) resulted in the score you received. As a human being, my evaluation may not be perfect. And of course, you are welcome to appeal.

- - -
Third of all, let me clarify the appeal/dispute process.

If you believe there was an error in grading, or that your program was evaluated unfairly, you are welcome to ask me to re-grade. However, I need to ask you to come to my office hours or make appointment. We need to meet in person so that you have a chance to explain to me:
- what minor changes are needed in order to make your program perfect.
- what's the approach you used, and why it did not work.
- or you can show me that your program actually works. And I can show you that it actually does not work.

This kind of back-and-forth discussion and negotiation cannot be done effectively by email. That's why meeting in person is the best way to do it.

:)
Sincerely,
- Mock

Sunday, April 22, 2007

Notes on Homework 1 Grading

-------------------------------------------------
Notes on grading this project
-------------------------------------------------

CS 10 - Java
Spring 2007
Homework 1

Graded by Mock Suwannatat
mock@cs.ucsb.edu

CRITERIA:
(Only a guide.)

--- Part 1: Reverse Product ---
10 points total
There are 4 tests, 3 points each.
Maximum 4 points if do not compile.

--- Part 2: Fractions ---
15 points total
There are 4 tests, 4 points each.
Maximum 4 points if do not compile.

--- Part 3: Triangle ---
15 points total
There are 4 tests, 4 points each.
Fore each test,
-2 for the first wrong output.
-1 point for each subsequent wrong output.
Maximum 4 points if do not compile.


LESSONS LEARNED:
This is where many people got it wrong:

- Your output and prompt messages should match the sample exactly.
+ Do not print anything extra.
+ Stick with the sample output: for example: last fraction term should be 1/1 not 1
+ I did not take points off this time, except in very rare cases.
- The ^ operator is not "power". It is XOR. So, when you write x2 = x^2; you may want to write Math.pow(x, 2); instead.
- Area formula of 0.5*side1*side2 would be correct only when it's a right triangle.
- Your program should terminate after producing the output. If the assignment does not say explicitly that you should keep asking for input, then don't.
- If you are asked to fill in the methods, then leave them blank if you don't know how to write them. Do not erase the method headers. Do not modify the headers.

TIPS & TRICKS:
The following tips may be useful for later projects. I gave some of them out to a few people who visited my office hours and asked. I did not give them to everyone because I did not want to spoil the fun ;)

Tip #1: How to convert number to String?
Say, you have an int x.
- First way: String s = (new Integer(x)).toString();
- Second way: String s = "" + x;

Tip #2: How to reverse a String?
- First way: write a for loop yourself.
- Second way: Say, you want to reverse s1 and store the result in s2, you write:
StringBuffer sBuffer = new StringBuffer(s1);
sBuffer.reverse();
s2 = sBuffer.toString();
Note that the String class is immutable, so there is no reverse() method for String. But the StringBuffer class is mutable, so you can make use if it. Next time, when you work on String, you may want to look at how you can use the StringBuffer class. (But you don't need it.)

FINAL WORDS:
Many people did a really good job. Have fun on hw2 (and others). If you feel like needing help, please visit the office hours. I've seen many cases where you "almost got it" and I could really help by giving some simple guides or hints -- but you need to ask :)

Wednesday, April 18, 2007

Java Coding Style Guide

CS 10 Discussion Notes
18 April 2007
By Mock Suwannatat

Java Coding Style Guide

Source:
- http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
- Conventional wisdom, past experiences

Indentation:
- Never use tabs. Use spaces instead.
- Use 2 or 4 spaces for each level of indentation (be consistent)
- No lines should be longer than 80 characters. If a line is long, break it up nicely.
- Always put { … } after an if-statement (recommended)
- Be consistent! Code with the same level must have the same level of indent.

Names:
- ClassName, variableName, methodName, CONSTANT_NAME
- Names should make sense. Don’t be shy for long names.

Variables:
- If an instance variable can be private, make it private. (almost aways)
- Declare instance variables at the top of your file.
- If it can be local, make it local.
- One declaration per line.

Methods:
- If a method can be private, make it private.
- Short methods are preferred.
- Should fit on a single screen. (no hard rule on this)

Comments:
- “Comments should be used to give overviews of code and provide additional information that is not readily available in the code itself.”
- When commenting, stick with the 80-char rule.
- When a comment block is longer than 1 line, do not use //. Instead, use /* … */
- Trailing comments must be short, and far away from the code. Either /* … */ or // can be used for trailing comments.
- /** javadoc comments */ are for readers who may not see the code. Each public method/field should have a javadoc comment.
- // can be used for commenting out a block of code.

Tips on IDE & Tools:
- Free IDEs: Eclipse, NetBeans, BlueJ
- Free text editors: Crimson Editor (Windows), Smultron (Mac only)
- An IDE can help you indent better. It can also do other things.
  • Auto-completion on variable names
  • Matching ( parentheses ) and { brackets }
  • Commenting/uncommenting code.
  • Syntax coloring.
  • Syntax checking!
- Visit Mock during office hours for help with installation.
- You still need to be able to use command line to compile and run your program.

Etc:
- One class per file (except for nested classes)
- …
- …
- …