Download Assignment 2 Naming policy Stack Trace

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Uppsala University
Assignment 2
ƒ
ƒ
ƒ
ƒ
ƒ
Actions
Internationalization (I18N)
Progress
Icons
Resize
@ UU/IT
2008-01-30 | #1
Uppsala University
Naming policy
ƒ getLastName()
ƒ setMaxOfLines(int i)
ƒ String longName = ”foo”;
ƒ int loopCounter = 7;
@ UU/IT
2008-01-30 | #2
Uppsala University
Stack Trace
java.lang.NullPointerException at se.uu.it110.icqui.IcqClient.setStatus(IcqClient.java:102)
at se.uu.it110.icqui.IcqUI$15.actionPerformed(IcqUI.java:343)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:654)
at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:678)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:730)
at
javax.swing.AbstractListModel.fireContentsChanged(AbstractListMode.ja
va:82)
at
javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBox
Model
.java:90)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:377)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:414)
at ...
@ UU/IT
2008-01-30 | #3
1
Uppsala University
Quality result
ƒ Few lines of code is easier to read, maintain,
understand etc.
ƒ Less code means you see more of the
program per page
@ UU/IT
2008-01-30 | #4
Uppsala University
Code style
// too much code
private JPanel getFilePanel()
{
JPanel p = new JPanel();
JScrollPane s = new JScrollPane(fileTable);
p.add(s);
return p;
}
// compact is better
private JPanel getFilePanel() {
JPanel p = new JPanel();
p.add(new JScrollPane(fileTable));
return p;
}
@ UU/IT
2008-01-30 | #5
Uppsala University
Compact
// method, 1 line
public String getColumnName(int col) { return headers[col]; }
// construktor
FileModel(User user) { this.user = user; }
// simple test
if (test == true) return;
// vs.
if (test) return;
@ UU/IT
2008-01-30 | #6
2
Uppsala University
javax.swing.Action
ƒ Interface, contains
• Accelerator, Mnemonic, Name, Icon, Description
(short+long), Enabled?, command
ƒ javax.swing.AbstractAction
• default implementations for the Action interface
ƒ new JButton(new ExitAction());
ƒ new JButton(new ExitAction(initValue));
@ UU/IT
2008-01-30 | #7
Uppsala University
Action – example
Action playAction = new
AbstractAction(“Play",
new ImageIcon(“play.gif")) {
public void actionPerformed(AE e) {
move(“left”);
}
};
JButton b = new JButton(playAction);
JMenuItem menuItem =
commandMenu.add(playAction);
playAction.setEnabled(false);
@ UU/IT
2008-01-30 | #8
Uppsala University
Action
ƒ The previous example is not good enough;
we also need:
• mnemonic, description, …
• I18n
• …
@ UU/IT
2008-01-30 | #9
3
Uppsala University
I18N
ƒ See build.xml, target “run”
File: player/ui/lang.properties:
ui.ok
= Ok
ui.cancel = Cancel
File: player/ui/lang_sv.properties:
ui.ok
= Okej
ui.cancel = Avbryt
ResourceBundle rb =
ResourceBundle.getBundle(“player.ui.lang”);
String okString = rb.getString(“ui.ok”);
JButton okButton = new JButton(okString);
@ UU/IT
2008-01-30 | #10
Uppsala University
java.util.ResourceBundle
ƒ Be specific and clear! Like this;
ui.menu.exit.name
ui.menu.exit.mne
= Exit
= x
ƒ Not like this:
exit=Exit
@ UU/IT
2008-01-30 | #11
Uppsala University
Icons
URL u = Player1.class.getResource("exit.gif");
JButton exitButton =
new JButton(new ImageIcon(u));
ƒ At runtime, exit.gif must be located next to
Player1.class
ƒ Icons also need to be in CVS
• Location, next to source code
ƒ Binary files need extra flag
ƒ cvs add –kb binary.gif
@ UU/IT
2008-01-30 | #12
4
Uppsala University
Collections
ƒ Eckel: chapter Collections of
Objects
• Read about containers, skip arrays
ƒ List – stores objects in sequence
ƒ Set – stores unique objects in a space
ƒ Map – manages key/value pairs
@ UU/IT
2008-01-30 | #13
Uppsala University
Iterators
ƒ Abstraction, you don’t care about the storage
ƒ Iterators are “lightweight” and do not
consume a lot of resources
Iterator it = collection.iterator();
while (it.hasNext()) {
Object o = it.next();
}
@ UU/IT
2008-01-30 | #14
Uppsala University
Iterator, exemple
public List getNames(Collection col) {
Vector names = new Vector();
Iterator it = col.iterator();
while (it.hasNext()) {
String name = ((Person)it.next()).getName();
names.add(name);
}
return names;
}
@ UU/IT
2008-01-30 | #15
5
Uppsala University
Map
import java.util.*;
class Counter {
int i = 1;
public String toString() {return Integer.toString(i);}
}
class Statistics {
public static void main(String[] args) {
HashMap ht = new HashMap();
for (int i = 0; i < 10000; i++) {
Integer r = new Integer((int)(Math.random() * 20));
if (ht.containsKey(r))
((Counter)ht.get(r)).i++;
else
ht.put(r, new Counter());
}
System.out.println(ht);
}
}
@ UU/IT
2008-01-30 | #16
Uppsala University
Collections
public class Log {
private static final int MAX = 1000;
private Map map = new HashMap();
private List list = new Vector();
private Set set = new HashSet();
Log() {
Random rnd = new Random();
for (int i = 0; i < 10; i++) {
map.put(new Integer((int)(System.currentTimeMillis() % MAX)), "foo");
list.add(new Integer(rnd.nextInt(MAX)));
set.add(new Integer(rnd.nextInt(MAX)));
}
dump(map.keySet());
dump(list);
dump(set);
}
public void dump(Collection c) {
Iterator iterator = c.iterator();
System.out.println("\n" + "Collection: " + c);
while (iterator.hasNext()) {
System.out.println("" + iterator.next());
}
}
public static void main(String[] argv){ Log l = new Log(); }
}
@ UU/IT
2008-01-30 | #17
Uppsala University
SortedSet
// create a sorted set
SortedSet s = new TreeSet();
s.add("string1");
s.add("STRING2");
s.add("STRING3");
s.add("string4");
s.add("STRING5");
// get a subset of the set and display it
Set sub = s.subSet("A", "ZZZZZZ");
Iterator i = sub.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
System.out.println();
// remove an element from the original set
s.remove("STRING3");
// display the subset again
i = sub.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
@ UU/IT
2008-01-30 | #18
6
Uppsala University
Inner class
public class Foo {
int i;
public class Bar {
int k = i;
}
void test() {
A a = new A();
B b = new B();
}
}
@ UU/IT
2008-01-30 | #19
Uppsala University
Inner classes, upcasting
interface Bills { int getNoOf100(); }
public class ATM {
public Bills getBillStatus() {
return new S();
}
// we only have 500!
private class S implements Bills {
public int getNoOf100() { return 0; }
public int get500() { return 42; }
}
}
@ UU/IT
2008-01-30 | #20
Uppsala University
Inner classes, anonymous
interface Bills { int getNoOf100(); }
public class ATM {
public Bills getBillStatus() {
return new Bills() {
public int getNoOf100(){ return 0; }
};
};
}
@ UU/IT
2008-01-30 | #21
7
Uppsala University
Is there a difference?
interface Bills { int get100(); }
Bills s1 =
new Bills() {
public int get100() { return 0; }
};
class Foo implements Bills {
public int get100() { return 0; }
}
Bills s2 = new Foo();
@ UU/IT
2008-01-30 | #22
Uppsala University
Scope for inner class
ƒ If you’re defining an anonymous inner class
and want to use an object that’s defined
outside the anonymous inner class, the
compiler requires that the argument reference
be final, like the argument to dest( ). If you
forget, you’ll get a compile-time error
message.
ƒ An inner class can see
• Outer surrounding class (it’s globala attributes)
• final
@ UU/IT
2008-01-30 | #23
Uppsala University
Scope
class Bank {
int i;
void bills() {
int k = 0;
final f = 7;
Bills s1 = new Bills() {
// see i & f, not k
public int get100() { return 0; }
};
}
}
@ UU/IT
2008-01-30 | #24
8
Uppsala University
ActionListener (AL)
// Not OOP
class Foo implements ActionListener {
Foo() {
JButton b = new JButton();
b.addActionListener(this); // ugly!
public void actionPerformed(ActionEvent e) {
// doit
}
}
}
@ UU/IT
2008-01-30 | #25
Uppsala University
AL 2
// Sometimes ok, mostly not
class Foo {
class Bar implements ActionListener {
public void actionPerformed(ActionEvent e) {
// doit
}
}
Foo() {
JButton b = new JButton();
b.addActionListener(new Bar());
}
}
@ UU/IT
2008-01-30 | #26
Uppsala University
AL 2.1
class Foo {
class Bar implements ActionListener {
public void actionPerformed(ActionEvent e) {
// doit
}
}
Foo() {
Bar bar = new Bar();
JButton b1 = new JButton();
JButton b2 = new JButton();
b1.addActionListener(bar);
b2.addActionListener(bar);
}
// smart, shares action
}
@ UU/IT
2008-01-30 | #27
9
Uppsala University
AL 3
// Good!
class Foo {
Foo() {
JButton b = new JButton();
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// doit
}
});
}
}
@ UU/IT
2008-01-30 | #28
Uppsala University
AL 4 – scope?
class Foo {
private int i;
Foo() {
int k = 0;
final m = 0;
JButton b = new JButton();
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// can see i and m, not k
}
});
}
}
@ UU/IT
2008-01-30 | #29
10
Related documents