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...