csc207 week 4ylzhang/csc207/files/lec04.pdf · csc207 week 4 larry zhang 1. logistics a1 part 1,...
TRANSCRIPT
![Page 1: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/1.jpg)
CSC207 Week 4Larry Zhang
1
![Page 2: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/2.jpg)
Logistics● A1 Part 1, read Arnold’s emails.
● Follow the submission schedule.
● Read the Q&A session in the handout.
● Ask questions on the discussion board.
● Submit on time! Don’t forget to push!
● After each submission checkpoint, if you think you can improve what you
previously submitted, improve it anyway. Doesn’t hurt.
2
![Page 3: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/3.jpg)
Resolving Git Conflicts● Rename your current repo folder to something like “repo_i_messed_up”.
○ Don’t delete it because it may contain uncommitted work.
● git clone again to re-download a fresh copy of the repo from the server
● Go into the fresh repo, see what’s the status in there, and manually apply the new changes (copy-n-paste lines of code) from files in “repo_i_messed_up” to the fresh repo.
● Commit and push the new changes.
● In Eclipse, you may need to re-create the workspace and the projects.
3
![Page 4: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/4.jpg)
Dynamic Binding
4
class Person { … public String hello() { return “I am a person”; }}
class Student extends Person { … @Override public String hello() { return “I am a student”; }}
Student s1 = new Student(“Alice”, 22);Person p1 = s1;String x = p1.hello();// Which hello() will be called?
● p1 is a reference that refers Student s1 as a Person.
● Java remembers that s1 is created as a Student.
● When calling p1.hello(), Java knows that what p1 is referring to is a Student, and it will invoke the method hello() that is implemented in the Student class.
● So p1.hello() returns is “I am a student”.
● Read more about dynamic binding here: https://stackoverflow.com/questions/19017258/static-vs-dynamic-binding-in-java
![Page 5: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/5.jpg)
Static Binding vs Dynamic BindingWhen you have multiple methods with the same name, how does Java decide which one to invoke?
5
// Static binding happens at compile
// time, based on type information.
public int hello(int x) {
return 1;
}
public int hello(String s) {
reteurn 2;
}
int r1 = hello(3); // r1 = 1
int r2 = hello(“3”); // r2 = 2
// Dynamic binding happens at runtime
// time (cannot be sure until running, so
// impossible to bind at compile time),
// based on the calling Object.
Person p;
int r = random.nextInt(5);
if (r == 3) {
p = new Student(“Alice”);
} else {
p = new Doctor(“Who”);
}
p.hello();
//depend on what Object p is referring to.
![Page 6: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/6.jpg)
NEXT TOPIC
Interface
6
![Page 7: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/7.jpg)
Interface in JavaInterface is like a special kind of “class”.
An interface is even more abstract than an abstract class.
● An interface is basically a group of public methods, declared but not implemented.
● This group of methods defines how an object interacts with the outside world.● Unlike abstract class, interface CANNOT contain instance or static
variables, except for public static final variables (constants).● Unlike abstract class, interface CANNOT contain implemented methods, i.e.,
all methods in an interface must be unimplemented.● Interface CANNOT contain non-public methods.
Another class can IMPLEMENT an interface. 7
![Page 8: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/8.jpg)
Interface Example
8
![Page 9: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/9.jpg)
When to use Abstract Class vs InterfaceConsider using Abstract Class if
● You want several closely-related classes to share the implementation of a common method.
● The classes extending this class share non-public methods or variables.● You want to declare non-static or non-final variables.
Consider using Interface if
● You expected that unrelated classes would implement this interface. e.g., the Comparable interface is implemented by many unrelated classes.
● You want to take advantage of multiple inheritance (a class can implement multiple interfaces).
9
![Page 10: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/10.jpg)
NEXT TOPIC
UML
10
![Page 11: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/11.jpg)
UML● Unified Modeling Language (UML) allows us to express the design of a
program before writing any code.
● It is language-independent.
● An extremely expressive language.
● We’ll use only a small part of the language, Class Diagrams, to represent basic OO design.
11
![Page 12: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/12.jpg)
12
![Page 13: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/13.jpg)
13
![Page 14: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/14.jpg)
DEMOUML Visualization using ObjectAid
http://www.objectaid.com/installation
and many other UML tools are available for Eclipse.
14
![Page 15: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/15.jpg)
15
![Page 16: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/16.jpg)
NEXT TOPIC
Graphical User Interface in Java
16
![Page 17: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/17.jpg)
Creating GUI with Java.Swing and Java.awtSwing and AWT are collections of class for creating Graphical User Interfaces.
They contains class for Windows, Buttons, Text Fields, Layouts, etc.
Read some background info on Swing and AWT:
http://edn.embarcadero.com/article/26970
17
![Page 18: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/18.jpg)
Basic Workflow of Creating GUIAll of the following is run in an Event-Dispatching Thread (EDT) launched by SwingUtilities.
1. Get the frame: Create a Window (JFrame) to hold the entire GUI.
2. Define how it looks: Create UI components (individual or grouped, with layout defined) and add them to the JFrame.
3. Define how it behaves: Attached handlers to UI components to handle events (button click, key press, etc).
4. Show it: Set size and location of the JFrame and make it visible.
18
![Page 19: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/19.jpg)
DEMO: Hello World ExampleHelloWorldGUI.java
19
![Page 20: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/20.jpg)
Different LayoutsThe layout defines the relative positions of the UI components.
See this link to explore:
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
20
![Page 21: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/21.jpg)
DEMO: LayoutsFlowFrame.javaGridFrame.java
ComplexFrame.java
21
![Page 22: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/22.jpg)
ActionListenerIt is an interface for which you need implement the method actionPerformed();
An ActionListener listens for events happens at where it is attached to, and performs the action defined in actionPerformed() when an event is detected.
For example, you can attach an ActionListener object to a JButton object in the UI, then the implementation of this listener’s actionPerformed() describes what happens whenever the button is pressed.
22
![Page 23: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/23.jpg)
DEMO: ActionListnerHiBye.java
HiByeActionListener.java
23
![Page 24: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/24.jpg)
DEMO: Key Press Event HandlingKeypressDemo.javaKeypressPanel.java
24
![Page 25: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/25.jpg)
DEMO: Timer EventsTimerDemo.java
TickTimerHandler.javaTockTimerHandler.java
25
![Page 26: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/26.jpg)
26
Practice for Home
Combine KeypressDemo and TimerDemo, so that the character moves automatically to the right by 5 pixels every second.
![Page 27: CSC207 Week 4ylzhang/csc207/files/lec04.pdf · CSC207 Week 4 Larry Zhang 1. Logistics A1 Part 1, read Arnold’s emails. Follow the submission schedule. ... layout defined) and add](https://reader033.vdocuments.site/reader033/viewer/2022051920/600d45ba090a14667d272bbb/html5/thumbnails/27.jpg)
Another Example to Play with NumberGuessGame.java
27