Wednesday, January 31, 2007

Notes from Week #3

Themes:
  1. Beware: local vs. global
  2. Overiding: which method is called?
  3. Overloading: which method is called?
  4. Static variables
  5. Recursion (we didn't get to this topic this time)

Example 1:

How big will you see the sun?

public class BigSun extends wheels.users.Frame {

private wheels.users.Ellipse e;
private int bigSize;

public BigSun() {
int bigSize = 200;

e = new wheels.users.Ellipse();
}

public void makeBig() {
e.setSize(bigSize, bigSize);
}

public static void main(String[] args) {
BigSun sun = new BigSun();
sun.makeBig();
}

}

Example 2:
What is wrong here? How to make it right? What does the output look like? What if we change changeSize in BlueSun to changeSize2 ?

import wheels.users.*;

public class Sun extends Frame {

private Ellipse e;

public Sun() {
e = new Ellipse();
}

public void changeSize() {
e.setSize(3,3);
}

}
----
import java.awt.Color;

public class BlueSun extends Sun {

public BlueSun() {
super();
e.setColor(Color.BLUE);
}

public void changeSize() {
e.setSize(200, 200);
}

public static void main(String[] args) {
BlueSun sun = new BlueSun();
sun.changeSize();
}

}

Example 3:
What is the output?

public class TestOverload {

private static int f(int x) {
return x*x;
}

private static int f(int[] a) {
return f(a[0]) + f(a[1]) + f(a[2]);
}

public static void main(String[] args) {
int x = 7;
int a[] = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;

System.out.println("The 1st result is " + f(x));
System.out.println("The 2nd result is " + f(a));
}

}

Example 4:
import java.awt.Color;

import wheels.users.*;

public class UglyAlien extends Ellipse {

private static Ellipse eye;

public UglyAlien() {
this.setSize(100, 100);
eye = new Ellipse();
eye.setFillColor(Color.WHITE);
eye.setSize(20, 20);
}

public void moveTo(int x, int y) {
this.setLocation(x, y);
eye.setLocation(x+40, y+40);
}

public void wink() {
eye.setSize(20, 5);
}
}

import wheels.users.*;

public class UglyAlienApp extends Frame{

public UglyAlienApp() {
UglyAlien a1, a2;

a1 = new UglyAlien();
a2 = new UglyAlien();

a1.moveTo(0, 0);
a2.moveTo(100, 0);

a2.wink();
}

public static void main(String[] args) {
new UglyAlienApp();
}

}


Note: the code on this post is badly commented. It's because of the blogger's thing... I'm too lazy to fix it. Do not follow this style. You should always indent your code nicely.

Thursday, January 25, 2007

Common Questions for Lab 2

Question:

Hi, I have a problem for cs10 Lab 2. Not a big problem, but I just
realized that whenever I run my program, it draws a red circle in the
middle of the frame...
Its no big deal, because everything else runs fine, but is there a way
to get rid of it?

Michael

Answer:

Hi Michael,

Yes, there is. It will be easy once I tell you how. But I should also
tell you why it works.

When you make the Alien class extends Ellipse, that means the Alien
itself is an Ellipse .. with some extra ellipses and maybe some
rectangles/lines. Inside the Alien constructor, you probably have a
statement like this...

face = new Ellipse();
face.setSize(...);
face.setLocation(...);

which creates a new Ellipse for variable face, resizes it, and puts it
at some location.

If you didn't call setSize() and setLocation(), what would happen?
Yes, the face will just be a red dot at the middle of the screen...

and that is exactly what is happening to your Alien's ellipse. So, to
fix this problem, you can make use of a special variable called "this"
(without the quotes) .. this refers to the class itself.

Since "this" is already an Ellipse, you do not need to create a new
Ellipse for the face. So, instead of saying

face = new Ellipse();

you can just say
face = this;

and then anything you do with the face, will be performed to the Alien
itself (which is initially a red dot).

Hope this helps!
- Mock

Wednesday, January 24, 2007

Notes from Week #2

Today I talked about Lab 2, giving clarifications and hints (as posted on the previous entry). I also presented a problem similar to Lab 2.

The problem is to write 2 classes SnowMan.java and SnowManApp.java so that SnowManApp will show 2 snowmen on the screen, one without a hat and the other with a hat.

We did the problem together on the board. Those who attended the discussion may have got a handful of hints for Lab 2 (due this Friday). Those who did not attend can come to my office hours tomorrow (Thurs: 12-3pm @ Phelp 1413) if they have questions.

Notes from Week #1:
Last week, we talked about different editors & IDEs you can use to develop your java program. I do not remember the full list, but popular ones (in my opinion) are listed here:

  • Text Editors
    • NotePad, DOS' edit
      Simple, fast, already available on your machine
    • TextPad
      Available on the labs' machines. If you want to use it on your machine, you will have to buy.
    • Crimson Editor
      Very nice, freely available at http://www.crimsoneditor.com/
      You can also use this tool to print out your code very nicely.
  • Integrated Development Environment (IDEs)
    • Eclipse
      Very popular, freely available at http://www.eclipse.org/
      You can use it for other programming languages, too.
    • NetBeans
      My choice. Specially designed for Java. Freely available at http://www.netbeans.org/
      It has a very nice GUI editing tool.
    • BlueJ
      Freely available at http://www.bluej.org/index.html
    • DrJava
      A lightweight IDE.
      Freely available at http://www.drjava.org/
      A light (Thanks to Kelly for her recommendation.)

Lab 2 clarification

Refered to: http://www.whizbangscholar.com/EB/010%20Lab%202%20W07%20B.pdf

There is a slight typo in the original lab write-up. On the line that says
public class
Alien extends wheels.users.Frame { ... }
It should have been
public class AlienApp extends wheels.users.Frame { ... }

In other words, the Alien class should NOT extend wheels.users.Frame. It does NOT need to extend anything. In fact, when write the Alien class, the header should be simply:
public class Alien { ... }

The AlienApp class, however, needs to "extends wheels.users.Ellipse".

You do not need to know what "extends" mean at this point. It has not been covered yet (but will be soon). If you are curious, read Chapter 3 on Inheritance.

QUESTION: Where should I put my main method?
Answer: The AlienApp class MUST have a main method. The Alien class does not need a main method.

QUESTION: How will my program be graded?
Answer:
We will do the following...
  • First, we will compile your program by...
    javac *.java
    If this creates an error, lots of points will be deducted. So, you must check to make sure there is no error at this point. If there is a "warning", minor points may be deducted.

  • Next, we will run AlienApp by
    java AlienApp
    If it doesn't run, or if it says the class is missing, or the main method is missing, your functionality points will be greatly affected (possibly zero!). So, make sure that AlienApp has a main method.

  • We will look at the result, we must see something like the following:

    That is, it must have 5 aliens on the same window. Note that alien #5 looks like alien #3 because he just winked and unwinked so rapidly our eye could not see.

    Your aliens don't have to look exactly like these. You may arrange them in a different ways -- just make sure we can see them all. Also, you can include your creativity by including a nose, a hat, ears, eyeglasses, body, ... whatever. Try to practice with different ways to draw things in wheels.

    If your program runs fine in this step, you will get a very good score.

  • We will try clicking on the alien face to see if you have implemented the extra credit.

  • Next, we will look at your code. You should submit only 2 classes:
    Alien.java and AlienApp.java
    If you have more files and they create errors, this may affect your score.

    We will look for the 4 methods specified in the lab write-up: moveTo, turnGreen, wink, and unwink.

    We will also look to see if you really made alien #5 wink and then unwink.

    Be sure to organize your code nicely. At least, you must indent your code, write your name and date at the top, include a short description of the program. Provide some comments.
QUESTION: I attempted the extra credit, but couldn't get mousePressed to work. Why?

You should put mousePressed method in Alien class. Also, modify the header of your Alien class to the following:

public class Alien extends wheels.users.Ellipse { ... }

This will make the Alien itself an ellipse which can be clicked by the user. However, it will create an extra red dot at the center of the screen. This is fine. When we click at the red dot, it will turn the alien to green & winked state.

Can you get rid of the red dot? (For the answer, visit my office hours -- Thurs 12-3pm @ Phelp 1413)

Even more fun: try to implement
public void mouseReleased(java.awt.event.MouseEvent e) { ... }

so that the alien only turn green and wink when the user holds down the mouse button. When the mouse button is released, the alien should turn white and unwinked again.

Contributors