Review
Warning: I wrote this notes in one night (and this morning), it may contains errors, typos, bad grammers, read at your own risk!
The notes may not cover everything! You have to read the book and my lecture notes.
JFrame
getContentPane()
- how to set layout
- constructors
setSize, setVisible, setLocation....
Layout
- 3 layouts:
BorderLayout, FlowLayout, GridLayout
- the effect of different layout (how they look?)
- understand all the constructors
- What do
new GridLayout(0, 5), new GridLayout(5,0) mean?
add(component), remove(index), add(component, index)
JPanel
- use as an intermediate container for creating complicated Swing applications
JPanel(), JPanel(layout)
JLabel, JButton, JTextField
- constructors
getText, setText
setBackground
- know how to set text position, icons, etc etc
- getActionCommand, setActionCommand
ActionListener
-
public class XXX implements ActionListener {
.............
............
public void actionPerformed(ActionEvent e) {
// your codes
}
}
- Anonymous class:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// your codes
}
});
- familiar with them and know how to change form one form to another
ActionEvent
e.getSource()
- returns
Component
- may need to downcast it, example:
JButton button = (JButton)e.getSource();
- can be used to distinguish sources of the event, example
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1) {
// do something
} else if( e.getSource() == button2) {
// do something
} ......
.....
}
e.getActionCommand()
Applet
Exception
- There are 2 types of exception:
RuntimeException, Checked exception
RuntimeExceptions are classes that extends RuntimeException.
- You have to "take care" of checked exception. There will be compiler error if you don't do that.
2 ways of taking care of exceptions
someMethod(......) throws SomeException {
// your code
}
or
try {
// some codes
} catch (ExceptionX ex) {
// some codes
} catch (ExceptionY ex) {
// some codes
}
.....
- It is optional to "take care" of the
RuntimeException.
NullPointerException, ArraryIndexOutOfBoundsException, ArithmeticException, NumberFormatException are
all RuntimeExceptions. Understand when will these exceptions occur.
FileNotFoundException, IOException are checked exceptions. You should know when these exceptions occurs.
- You should understand the flow of the program with exception: example
try {
line1
line2
line3
......
line10
} catch (ExceptionX ex) {
lineX
......
} catch (ExceptionY ex) {
lineY
.......
} finally {
......
}
line101
line102
If the first exception occurs at line3, then the program will find out which exception matches the exceptin thrown by line 3. The
codes in the catch block will be executed. After that, the codes in finally block will be executed. And then line 101.......
Please modify the following program to understand the concept
public class ExceptionTest {
public static void main(String[] args) {
try {
System.out.println("line 1");
String[] s = new String[10];
System.out.println("line 2");
s[0] = "Hi";
System.out.println("line 3");
System.out.println(s[11]);
System.out.println("line 4");
System.out.println(s[1].toUpperCase());
System.out.println("line 5");
System.out.println(10/0);
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("line X");
} catch (NullPointerException ex) {
System.out.println("line Y");
} catch (ArithmeticException ex) {
System.out.println("line Z");
} finally {
System.out.println("line finally");
}
System.out.println("Start all over");
}
}
Object
equals (vs ==) and how to write equals method.
toString (System.out.println(obj))
instanceof: obj instanceof aClassName returns true if obj is of type aClassName.
Returns false otherwise.
- every class extends Object
Type Wrapper class
IO
Input
InputStream
BufferedReader:
Output
FileOutputStream
FileOutputStream out = new FileOutputStream(filename);
out.write(int ch)
PrintStream
PrintStream out = new PrintStream(new FileOutputStream(filename));
out.print(...), out.println(....)
System.out is a PrintStream
More :
- Most of the methods throw
IOException or FileNotFoundException
- don't forget to close the stream by
close()
- understand other
read, write methods.
File
- understand all the constructors and methods
- create a File object doesn't mean the file is created!
- read the API. Try my sample exam.
StringTokenizer
- use to break a
String into "tokens"
- Constructor:
new StringTokenizer(String).
String nextToken(), int countTokens(), boolean hasMoreTokens()
Example 1
String s = "this is a string"
StringTokenizer st = new StringTokenizer(s);
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
The output is
this
is
a
string
Example 2
String s = "12 35";
StringTokenizer st = new StringTokenizer(s);
int sum = Integer.parseInt(st.nextToken()) + Integer.parseInt(st.nextToken());
Then sum is 12 + 35 = 47.
Vector/LinkedList
- understand all the constructors and API
add(Object obj), get(int index), set(int index, Object obj, size(), remove(int index), remove(Object obj)
boolean contains(Object obj),int indexOf(Object obj) : these methods use equals method
to test equality
- Type casting:
(String)vector.get(5) get method returns Object.
- primitive type:
vector.add(new Integer(101)); you can' t add primitive type to a vector. instead, you have to use type wrapper class.
- get primitive type:
((Integer)vector.get(2)).intValue() returns int
Math
- understand how to use
Math class. Example Math.sqrt(5) is used to calculate the square root of 5.