Java for Interfaces and Networks (DT3010, HT10)

Size: px
Start display at page:

Download "Java for Interfaces and Networks (DT3010, HT10)"

Transcription

1 Java for Interfaces and Networks (DT3010, HT10) Inner Classes Federico Pecora School of Science and Technology Örebro University Federico Pecora Java for Interfaces and Networks Lecture 8 1 / 16

2 Outline 1 Inner classes (and interfaces) 2 (Static) Member Classes 3 (Anonymous) Local Classes 4 Anonymous classes for GUIs 5 Concrete examples Federico Pecora Java for Interfaces and Networks Lecture 8 2 / 16

3 Inner classes (and interfaces) Outline 1 Inner classes (and interfaces) 2 (Static) Member Classes 3 (Anonymous) Local Classes 4 Anonymous classes for GUIs 5 Concrete examples Federico Pecora Java for Interfaces and Networks Lecture 8 3 / 16

4 Inner classes (and interfaces) What are inner classes? A normal class is a direct member of a package, a top-level class Inner classes are classes that nest within other classes available since Java 1.1 Inner classes come in four flavors: static member classes member classes local classes anonymous classes Federico Pecora Java for Interfaces and Networks Lecture 8 4 / 16

5 (Static) Member Classes Outline 1 Inner classes (and interfaces) 2 (Static) Member Classes 3 (Anonymous) Local Classes 4 Anonymous classes for GUIs 5 Concrete examples Federico Pecora Java for Interfaces and Networks Lecture 8 5 / 16

6 (Static) Member Classes (Static) Member classes Static member classes are classes that are static members of a class Like any other static member, they have access to all static members of the parent class A member class is also defined as a member of a class Unlike static member classes, the member class is instance specific Unlike static member classes, the member class has access to any and all methods and members this includes the parent s this reference Federico Pecora Java for Interfaces and Networks Lecture 8 6 / 16

7 (Static) Member Classes (Static) Member classes (Static) Member classes have some restrictions A member class cannot have the same name as any of its enclosing classes Member classes cannot contain static fields, methods, or classes the only exception is for constants (i.e., declared both static and final) Member interfaces are implicitly static an interface cannot be instantiated, so there is nothing to associate with an instance of the enclosing class (see static member classes) Federico Pecora Java for Interfaces and Networks Lecture 8 6 / 16

8 (Static) Member Classes (Static) Member classes MemberClassTest.java 1 public class MemberClassTest { 2 public void foo() { 3 System.out.println("Outer class FOO"); 4 } 5 public class MemberClass { 6 public void foo() { 7 System.out.println("Member class"); 8 } 9 public void test() { 10 this.foo(); 11 MemberClassTest.this.foo(); 12 } 13 } 14 //continues... Federico Pecora Java for Interfaces and Networks Lecture 8 6 / 16

9 (Static) Member Classes (Static) Member classes MemberClassTest.java 15 public static void main(string[] args) { 16 MemberClassTest o = new MemberClassTest(); 17 //Compilation error -- Needs an enclosing instance. 18 //MemberClassTest.MemberClass i = 19 // new MemberClassTest.MemberClass(); 20 MemberClassTest.MemberClass i = o.new MemberClass(); 21 i.test(); 22 } 23 } Federico Pecora Java for Interfaces and Networks Lecture 8 6 / 16

10 (Static) Member Classes (Static) Member classes linux> java MemberClassTest Member class Outer class FOO linux> Federico Pecora Java for Interfaces and Networks Lecture 8 6 / 16

11 (Static) Member Classes (Static) Member classes StaticMemberClassTest.java 1 public class StaticMemberClassTest { 2 public static void bar() { 3 System.out.println("Outer class BAR"); 4 } 5 public static class StaticMemberClass { 6 public void bar() { 7 System.out.println("Static Member class"); 8 } 9 public void test() { 10 this.bar(); 11 StaticMemberClassTest.bar(); 12 //Compilation error -- No enclosing instance! 13 //StaticMemberClassTest.this.bar(); 14 } 15 } 16 //continues... Federico Pecora Java for Interfaces and Networks Lecture 8 6 / 16

12 (Static) Member Classes (Static) Member classes StaticMemberClassTest.java 17 public static void main(string[] args) { 18 //Yes -- does not need an enclosing instance 19 StaticMemberClassTest.StaticMemberClass o1 = 20 new StaticMemberClassTest.StaticMemberClass(); 21 o1.test(); 22 } 23 } Federico Pecora Java for Interfaces and Networks Lecture 8 6 / 16

13 (Static) Member Classes (Static) Member classes linux> java StaticMemberClassTest Static Member class Outer class BAR linux> Federico Pecora Java for Interfaces and Networks Lecture 8 6 / 16

14 (Anonymous) Local Classes Outline 1 Inner classes (and interfaces) 2 (Static) Member Classes 3 (Anonymous) Local Classes 4 Anonymous classes for GUIs 5 Concrete examples Federico Pecora Java for Interfaces and Networks Lecture 8 7 / 16

15 (Anonymous) Local Classes Local classes Local classes are classes that are declared within a block of code They are visible only within that block, just as any other method variable Like member classes, local classes are associated with a containing instance Local classes can access any member, including private members, of the containing class An anonymous class is a local class that has no name Federico Pecora Java for Interfaces and Networks Lecture 8 8 / 16

16 (Anonymous) Local Classes Local classes A local class can never be used outside its block Local classes can access local variables, method parameters, or exception parameters that are Why? declared in the scope of the local method definition, and declared final the lifetime of a local class instance can be longer than the execution of the method in which the class is defined final ensures that its copy of a local variable and the original will always be the same Federico Pecora Java for Interfaces and Networks Lecture 8 8 / 16

17 (Anonymous) Local Classes Local classes A local class cannot be declared public, protected, private, or static Local classes cannot contain static fields, methods, or classes the only exception is for constants that are declared both static and final Local interfaces are implicitly static A local class cannot have the same name as any of its enclosing classes Federico Pecora Java for Interfaces and Networks Lecture 8 8 / 16

18 (Anonymous) Local Classes Local classes LocalClassTest.java 1 class A { protected char a = a ; } 2 class B { protected char b = b ; } 3 public class C extends A { 4 private char c = c ; 5 public static char d = d ; 6 public void createlocalobject(final char e) 7 { 8 final char f = f ; 9 int i = 0; 10 class Local extends B 11 { 12 char g = g ; 13 public void printvars() 14 {... } 15 } 16 Local l = new Local(); 17 l.printvars(); 18 } 19 } Federico Pecora Java for Interfaces and Networks Lecture 8 8 / 16

19 (Anonymous) Local Classes Local classes LocalClassTest.java (Local.printVars()) 1 public void printvars() 2 { 3 System.out.println(g); //g is a field of Local 4 System.out.println(f); //f is a final local variable 5 System.out.println(e); //e is a final local parameter 6 System.out.println(d); //d is field of containing class 7 System.out.println(c); //c is field of containing class 8 System.out.println(b); //b is inherited by Local 9 System.out.println(a); //a is inherited by C 10 } Federico Pecora Java for Interfaces and Networks Lecture 8 8 / 16

20 (Anonymous) Local Classes Local classes Are references to the local class instances destroyed when the enclosing method returns? Weird.java 1 public class Weird { 2 public static interface IntHolder { public int getvalue(); } 3 public static void main(string[] args) { 4 IntHolder[] holders = new IntHolder[10]; 5 for(int i = 0; i < 10; i++) { 6 final int fi = i; 7 class MyIntHolder implements IntHolder { 8 public int getvalue() { return fi; } 9 } 10 holders[i] = new MyIntHolder(); 11 } 12 for(int i = 0; i < 10; i++) 13 System.out.print(holders[i].getValue() + " "); 14 } 15 } Federico Pecora Java for Interfaces and Networks Lecture 8 8 / 16

21 (Anonymous) Local Classes Local classes Are references to the local class instances destroyed when the enclosing method returns? linux> java Weird linux> The lexical scope of the methods of a local class has nothing to do with when the interpreter enters and exits the block of code that defines the local class Each instance of a local class has its own private copy of the scope within which it was created Federico Pecora Java for Interfaces and Networks Lecture 8 8 / 16

22 (Anonymous) Local Classes Anonymous classes An anonymous class is a local class that has no name An anonymous class definition is an expression it can be included as part of a larger expression, such as a method call Anonymous classes allow to define a one-shot class exactly where it is needed Anonymous classes have a succinct syntax that reduces clutter in your code Since anonymous classes are local classes, they share the same capabilities and limitations... although they cannot have constructors Federico Pecora Java for Interfaces and Networks Lecture 8 9 / 16

23 (Anonymous) Local Classes Anonymous classes Code snippet - Local class 1 //... 2 public java.util.enumeration enumerate() { 3 //Enumerator as a local class 4 class Enumerator implements java.util.enumeration { 5 Linkable current; 6 public Enumerator() { current = head; } 7 public boolean hasmoreelements() { 8 return (current!= null); } 9 public Object nextelement() { 10 if (current == null) 11 throw new java.util.nosuchelementexception(); 12 Object value = current; 13 current = current.getnext(); 14 return value; 15 } 16 } 17 return new Enumerator(); 18 } 19 //... Federico Pecora Java for Interfaces and Networks Lecture 8 9 / 16

24 (Anonymous) Local Classes Anonymous classes Code snippet - Anonymous class 1 //... 2 public java.util.enumeration enumerate() { 3 //Enumeration extended as part of the return statement 4 return new java.util.enumeration() { 5 Linkable current; 6 //Replace constructor with an instance initializer 7 { current = head; } 8 public boolean hasmoreelements() { 9 return (current!= null); } 10 public Object nextelement() { 11 if (current == null) 12 throw new java.util.nosuchelementexception(); 13 Object value = current; 14 current = current.getnext(); 15 return value; 16 } 17 }; //<-- Note the required semicolon (return statement) 18 } 19 //... Federico Pecora Java for Interfaces and Networks Lecture 8 9 / 16

25 (Anonymous) Local Classes Anonymous classes So, when to use anonymous classes? An object of the anonymous class has no reason to exist in the absence of an object of the enclosing class An object of the anonymous class is not needed outside a method of the enclosing class Methods of the anonymous class need access to members of the enclosing class or local final variables/parameters Only one instance of the anonymous class is needed The class does not need a name that is accessible elsewhere in the program Federico Pecora Java for Interfaces and Networks Lecture 8 9 / 16

26 (Anonymous) Local Classes Anonymous classes Anonymous classes are often used as adapters classes that define code that is invoked by some other object Code snippet (adapter class) 1 //... 2 File f = new File("/src"); //The directory to list 3 String[] filelist = f.list(new FilenameFilter() { 4 public boolean accept(file f, String s) { 5 return s.endswith(".java"); 6 } 7 }); //<-- Don t forget to end the method call! 8 //... list() method requires FilenameFilter argument FilenameFilter interface is implemented by an anonymous class (defiend within the method invocation) Federico Pecora Java for Interfaces and Networks Lecture 8 9 / 16

27 (Anonymous) Local Classes Anonymous classes Defining an anonymous class is an implicit way to extend a class or implement an interface new FilenameFilter() {...} new <class-name> implements FilenameFilter {...} new Vector() {...} new <class-name> extends Vector {...} Notice that anonymous classes cannot have a contructor because they have no name... but you can pass parameters to the super-constructor and specify an instance initializer Code snippet (instance initializer) 1 universityregistration.addnames(new Vector(3) { 2 //Super-constructor is called, then instance initializer: 3 { add("heinz"); add("john"); add("anton"); } 4 }); //<-- End addnames method invocation Federico Pecora Java for Interfaces and Networks Lecture 8 9 / 16

28 (Anonymous) Local Classes Inner classes: summary Inner classes bring an object-oriented advantage a tree class has many helper methods that perform a search or walk of the tree from an object-oriented point of view, the tree is a tree, not a search algorithm however, you need intimate knowledge of the tree s data structures to accomplish a search encapsulate helper methods in an inner class Inner classes provide a means to accomplish namespaces Inner classes provide a means to accomplish callbacks anonymous classes are particularly fit for this purpose particularly useful in GUI creation Federico Pecora Java for Interfaces and Networks Lecture 8 10 / 16

29 Anonymous classes for GUIs Outline 1 Inner classes (and interfaces) 2 (Static) Member Classes 3 (Anonymous) Local Classes 4 Anonymous classes for GUIs 5 Concrete examples Federico Pecora Java for Interfaces and Networks Lecture 8 11 / 16

30 Anonymous classes for GUIs An ordinary GUI implementation SomeGUI.java 1 public class SomeGUI extends JFrame implements ActionListener { 2 protected JButton button1; 3 protected JButton button2; 4 //... 5 protected JButton buttonn; 6 public void actionperformed(actionevent e) { 7 if(e.getsource()==button1) { /* do something */ } 8 else if(e.getsource()==button2) { /* do something */ } 9 // else if(e.getsource()==buttonn) { /* do something */ } 11 } 12 // } Federico Pecora Java for Interfaces and Networks Lecture 8 12 / 16

31 Anonymous classes for GUIs An ordinary GUI implementation Our class SomeGUI implements ActionListener button press logic mingles with GUI implementation logic monolithic actionperformed() method There are N buttons in the worst case, the GUI will perform N comparisons Using inner classes, we could do away with chain of if blocks GUI always knows which code to execute O(1) instead of O(N) Using inner classes we can avoid that SomeGUI implements ActionListener Federico Pecora Java for Interfaces and Networks Lecture 8 12 / 16

32 Anonymous classes for GUIs An ordinary GUI implementation SomeGUI.java 1 //SomeGUI does NOT implement ActionListener 2 public class SomeGUI extends JFrame { 3 protected JButton button1; 4 protected JButton button2; 5 //... 6 protected JButton buttonn; 7 protected void buildgui() { 8 button1 = new JButton(); 9 button1.addactionlistener( 10 new ActionListener() { 11 public void actionperformed(actionevent e) { 12 /* do something */ 13 } 14 } 15 ); //<-- close addactionlistener method 16 // } 18 } Federico Pecora Java for Interfaces and Networks Lecture 8 12 / 16

33 Anonymous classes for GUIs An ordinary GUI implementation SomeGUI.java 1 //SomeGUI does NOT implement ActionListener 2 public class SomeGUI extends JFrame { 3 protected JButton button1; 4 protected JButton button2; 5 //... 6 protected JButton buttonn; 7 class Button1Handler implements ActionListener { 8 public void actionperformed(actionevent e) { 9 /* do something */ 10 } 11 } 12 // protected void buildgui() { 14 button1 = new JButton(); 15 button1.addactionlistener(new Button1Handler()); 16 // } 18 } Federico Pecora Java for Interfaces and Networks Lecture 8 12 / 16

34 Anonymous classes for GUIs An ordinary GUI implementation Better to use member classes or anonymous (local) classes? That is a question of style Also depends on application some buttons share functionality why re-write an anonymous class? just use an inner class! all buttons have very specific behavior why bother giving the class a name? just inline its definition! NB: Inner classes (especially anonymous classes) can get out of hand if too long! NB: If the class functionality makes sense outside the method, do not make it an anonymous class! Federico Pecora Java for Interfaces and Networks Lecture 8 12 / 16

35 Concrete examples Outline 1 Inner classes (and interfaces) 2 (Static) Member Classes 3 (Anonymous) Local Classes 4 Anonymous classes for GUIs 5 Concrete examples Federico Pecora Java for Interfaces and Networks Lecture 8 13 / 16

36 Concrete examples The BoringWindow A window with four buttons, each with a different behavior Tripp: prints Tripp! Trapp: prints Trapp! and a private instance variable of BoringWindow Trull/Hejsan: print Trull! / Hejsan!, a private instance variable of BoringWindow, and a local variable of BoringWindow s contructor Federico Pecora Java for Interfaces and Networks Lecture 8 14 / 16

37 Concrete examples The BoringWindow Federico Pecora Java for Interfaces and Networks Lecture 8 14 / 16

38 Concrete examples The BoringWindow TrippButtonListener.java 1 class TrippButtonListener implements ActionListener { 2 public void actionperformed(actionevent event) { 3 System.out.println("Tripp!"); 4 } 5 } //class TrippButtonListener TrippButtonListener can be a top-level class because it does not need to access any member/local variables The same is not true for the other three buttons... Federico Pecora Java for Interfaces and Networks Lecture 8 14 / 16

39 Concrete examples The BoringWindow BoringWindow.java 1 public class BoringWindow { 2 private int bwx = 1; 3 private class TrappButtonListener 4 implements ActionListener { 5 public void actionperformed(actionevent event) { 6 System.out.println("Trapp!"); 7 System.out.println("bwx = " + bwx); 8 } 9 } //class TrappButtonListener 10 //continues... TrappButtonListener is a member class so it has access to private int bwx Federico Pecora Java for Interfaces and Networks Lecture 8 14 / 16

40 Concrete examples The BoringWindow BoringWindow.java (cont.) 11 public BoringWindow() { 12 int kx = 2; 13 final int fkx = 3; 14 class TrullButtonListener implements ActionListener { 15 public void actionperformed(actionevent event) { 16 System.out.println("Trull!"); 17 System.out.println("bwx = " + bwx); 18 //Compilation error! 19 //local variable kx is accessed from within inner 20 //class; needs to be declared final 21 //System.out.println("kx = " + kx); 22 System.out.println("fkx = " + fkx); 23 } 24 } //class TrullButtonListener 25 //continues... TrullButtonListener is a local class so it has access to final int fkx (and private int bwx) Federico Pecora Java for Interfaces and Networks Lecture 8 14 / 16

41 Concrete examples The BoringWindow BoringWindow.java (cont.) 26 JFrame frame = new JFrame("A boring window"); 27 frame.setsize(350, 50); 28 Container cp = frame.getcontentpane(); 29 cp.setlayout(new GridLayout(1, 4)); 30 JButton trippb = new JButton("Tripp!"); 31 JButton trappb = new JButton("Trapp!"); 32 JButton trullb = new JButton("Trull!"); 33 JButton hejsanb = new JButton("Hejsan!"); 34 cp.add(trippb); 35 cp.add(trappb); 36 cp.add(trullb); 37 cp.add(hejsanb); 38 trippb.addactionlistener(new TrippButtonListener()); 39 trappb.addactionlistener(new TrappButtonListener()); 40 trullb.addactionlistener(new TrullButtonListener()); 41 //continues... Federico Pecora Java for Interfaces and Networks Lecture 8 14 / 16

42 Concrete examples The BoringWindow BoringWindow.java (cont.) 42 hejsanb.addactionlistener(new ActionListener() { 43 public void actionperformed(actionevent event) { 44 System.out.println("Hejsan!"); 45 System.out.println("bwx = " + bwx); 46 //Compilation error! 47 //local variable kx is accessed from within inner 48 //class; needs to be declared final 49 //System.out.println("kx = " + kx); 50 System.out.println("fkx = " + fkx); 51 } 52 }); 53 frame.setvisible(true); 54 } //BoringWindow constructor 55 //continues... trullb s listener is an anonymous class, and has access to final int fkx (and private int bwx) Federico Pecora Java for Interfaces and Networks Lecture 8 14 / 16

43 Concrete examples The BoringWindow BoringWindow.java (cont.) 56 public static void main(string[] args) { 57 final BoringWindow b = new BoringWindow(); 58 int mx = 4; 59 final int fmx = 5; 60 class UnusedButtonListener implements ActionListener { 61 public void actionperformed(actionevent event) { 62 System.out.println("Impossible!"); 63 //Compilation error! 64 //non-static variable bwx cannot be referenced 65 //from a static context 66 //System.out.println("bwx = " + bwx); 67 System.out.println("b.bwx = " + b.bwx); 68 //Compilation error! (as before) 69 //System.out.println("mx = " + mx); 70 System.out.println("fmx = " + fmx); 71 } 72 } //class UnusedButtonListener 73 //continues... Federico Pecora Java for Interfaces and Networks Lecture 8 14 / 16

44 Concrete examples The BoringWindow BoringWindow.java (cont.) 74 //Some useless instantiations of the button listeners 75 new TrippButtonListener(); 76 //Compilation error! 77 //non-static variable this cannot be referenced from 78 //a static context 79 //new TrappButtonListener(); 80 //Compilation error! 81 //cannot resolve symbol 82 //new TrullButtonListener(); 83 new UnusedButtonListener(); 84 } //main 85 } //class BoringWindow Federico Pecora Java for Interfaces and Networks Lecture 8 14 / 16

45 Concrete examples The BoringWindow TrippButtonListener is a top-level class TrappButtonListener is a member class i.e., a non-static member which therefore cannot be accessed in main() if it were a static member class it would not make sense to instantiate it! TrullButtonListener is a local class and it is out of its scope HejsanB s listener has no name therefore it cannot be referenced outside the expression UnusedButtonListener is a local class (in scope) Federico Pecora Java for Interfaces and Networks Lecture 8 14 / 16

46 Concrete examples The ChatClient A multi-thread chat client with extra buttons Send insult: sends an insult to the server Send praise: sends a nice sentence to the server Close: closes the connection sending null to server and exiting (works with Server developed in lecture 5) Federico Pecora Java for Interfaces and Networks Lecture 8 15 / 16

47 Concrete examples The ChatClient Federico Pecora Java for Interfaces and Networks Lecture 8 15 / 16

48 Concrete examples The ChatClient ClientWithButtons.java 1 public class ClientWithButtons { 2 public static final int PORT = 2000; 3 private Socket socket; 4 private BufferedReader in; 5 private PrintWriter out; 6 BufferedReader kbd_reader; 7 //continues... We want our button listeners and server thread to have access to the socket (BufferedReader in and PrintWriter out) Therefore we will make them member classes Federico Pecora Java for Interfaces and Networks Lecture 8 15 / 16

49 Concrete examples The ChatClient ClientWithButtons.java (cont.) 8 private class ServerListener extends Thread { 9 public void run() { 10 String linefromserver; 11 try { 12 while ((linefromserver = in.readline())!= null && 13!lineFromServer.equals("quit")) { 14 System.out.println("From server: " 15 + linefromserver); 16 } 17 } 18 catch (IOException e) { 19 System.out.println("Exception captured: " + e); 20 } 21 } 22 } //class ServerListener 23 //continues... Federico Pecora Java for Interfaces and Networks Lecture 8 15 / 16

50 Concrete examples The ChatClient ClientWithButtons.java (cont.) 24 private class ChatButtonWindow extends JFrame { 25 public ChatButtonWindow() { 26 super("extra chat buttons"); 27 Container cp = getcontentpane(); 28 cp.setlayout(new FlowLayout()); 29 JButton button1 = new JButton("Send insult"); 30 cp.add(button1); 31 JButton button2 = new JButton("Send praise"); 32 cp.add(button2); 33 JButton button3 = new JButton("Close"); 34 cp.add(button3); 35 setsize(200, 200); 36 setvisible(true); 37 //continues... Federico Pecora Java for Interfaces and Networks Lecture 8 15 / 16

51 Concrete examples The ChatClient ClientWithButtons.java (cont.) 38 button1.addactionlistener(new ActionListener() { 39 public void actionperformed(actionevent event) { 40 out.println("you ratbag!"); 41 System.out.println("Insult sent."); 42 } 43 }); 44 button2.addactionlistener(new ActionListener() { 45 public void actionperformed(actionevent event) { 46 out.println("you re the best!"); 47 System.out.println("Praise sent."); 48 } 49 }); 50 button3.addactionlistener(new ActionListener() { 51 public void actionperformed(actionevent event) { 52 System.out.println("Closed."); 53 System.exit(0); 54 } 55 }); 56 //continues... Federico Pecora Java for Interfaces and Networks Lecture 8 15 / 16

52 Concrete examples The ChatClient ClientWithButtons.java (cont.) 57 public ClientWithButtons(String servername) 58 throws IOException { 59 InetAddress addr = InetAddress.getByName(serverName); 60 Socket socket = new Socket(addr, PORT); 61 System.out.println("the new socket: " + socket); 62 in = new BufferedReader( 63 new InputStreamReader(socket.getInputStream())); 64 out = new PrintWriter( 65 new BufferedWriter( 66 new OutputStreamWriter( 67 socket.getoutputstream())), true); 68 kbd_reader = new BufferedReader( 69 new InputStreamReader(System.in)); 70 ServerListener t = new ServerListener(); 71 t.start(); 72 ChatButtonWindow w = new ChatButtonWindow(); 73 } //ClientWithButtons contructor 74 //continues... Federico Pecora Java for Interfaces and Networks Lecture 8 15 / 16

53 Concrete examples The ChatClient ClientWithButtons.java (cont.) 75 void listen() throws IOException { 76 String buf; 77 while (true) { 78 buf = kbd_reader.readline(); 79 System.out.println("From keyboard: " + buf); 80 System.out.println("To server: " + buf); 81 out.println(buf); 82 } 83 } 84 //continues... Federico Pecora Java for Interfaces and Networks Lecture 8 15 / 16

54 Concrete examples The ChatClient ClientWithButtons.java (cont.) 85 public static void main(string[] args) throws IOException { 86 ClientWithButtons c; 87 if (args.length >= 1) 88 c = new ClientWithButtons(args[0]); 89 else 90 c = new ClientWithButtons(null); 91 c.listen(); 92 } //main 93 } //class ClientWithButtons Federico Pecora Java for Interfaces and Networks Lecture 8 15 / 16

55 Concrete examples The ChatClient ClientWithButtons.java (cont.) 85 public static void main(string[] args) throws IOException { 86 ClientWithButtons c; 87 if (args.length >= 1) 88 c = new ClientWithButtons(args[0]); 89 else 90 c = new ClientWithButtons(null); 91 c.listen(); 92 } //main 93 } //class ClientWithButtons NB: out is used three nestings down: this can make code difficult to follow... Distance decreased with a local copy in ChatButtonWindow Tip: when too many levels, think about providing it through constructor/local copy Federico Pecora Java for Interfaces and Networks Lecture 8 15 / 16

56 Concrete examples Inner Classes Thank you! Federico Pecora Java for Interfaces and Networks Lecture 8 16 / 16

Java for Interfaces and Networks

Java for Interfaces and Networks Java for Interfaces and Networks Threads and Networking Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces and Networks Lecture

More information

Java for Interfaces and Networks (DT3029)

Java for Interfaces and Networks (DT3029) Java for Interfaces and Networks (DT3029) Lecture 3 Threads and Networking Federico Pecora federico.pecora@oru.se Center for Applied Autonomous Sensor Systems (AASS) Örebro University, Sweden Capiscum

More information

Java for Interfaces and Networks (DT3010, HT11)

Java for Interfaces and Networks (DT3010, HT11) Java for Interfaces and Networks (DT3010, HT11) Networking with Threads and Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces

More information

Java for Interfaces and Networks (DT3029)

Java for Interfaces and Networks (DT3029) Java for Interfaces and Networks (DT3029) Lecture 4 Networking with Threads and Containers Federico Pecora federico.pecora@oru.se Center for Applied Autonomous Sensor Systems (AASS) Örebro University,

More information

Java for Interfaces and Networks (DT3010, HT10)

Java for Interfaces and Networks (DT3010, HT10) Java for Interfaces and Networks (DT3010, HT10) More on Swing and Threads Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces and

More information

Java: introduction to object-oriented features

Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Graphical User Interface (Part-1) Supplementary Material for CPSC 233 Graphical User Interface (Part-1) Supplementary Material for CPSC 233 Introduction to Swing A GUI (graphical user interface) is a windowing system that interacts with the user The Java AWT (Abstract Window

More information

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions G51PGP Programming Paradigms Lecture 009 Concurrency, exceptions 1 Reminder subtype polymorphism public class TestAnimals public static void main(string[] args) Animal[] animals = new Animal[6]; animals[0]

More information

Midterm assessment - MAKEUP Fall 2010

Midterm assessment - MAKEUP Fall 2010 M257 MTA Faculty of Computer Studies Information Technology and Computing Date: /1/2011 Duration: 60 minutes 1-Version 1 M 257: Putting Java to Work Midterm assessment - MAKEUP Fall 2010 Student Name:

More information

Java for Interfaces and Networks (DT3029)

Java for Interfaces and Networks (DT3029) Java for Interfaces and Networks (DT3029) Lecture 9 More on Swing and Threads Federico Pecora federico.pecora@oru.se Center for Applied Autonomous Sensor Systems (AASS) Örebro University, Sweden www.clipartlord.com

More information

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread G51PGP Programming Paradigms Lecture 008 Inner classes, anonymous classes, Swing worker thread 1 Reminder subtype polymorphism public class TestAnimals public static void main(string[] args) Animal[] animals

More information

Java for Interfaces and Networks (DT3010, HT10)

Java for Interfaces and Networks (DT3010, HT10) Java for Interfaces and Networks (DT3010, HT10) More on Garbage Collection, More on Swing Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java

More information

GUI Forms and Events, Part II

GUI Forms and Events, Part II GUI Forms and Events, Part II Quick Start Compile step once always mkdir labs javac PropertyTax6.java cd labs Execute step mkdir 6 java PropertyTax6 cd 6 cp../5/propertytax5.java PropertyTax6.java Submit

More information

MIT AITI Swing Event Model Lecture 17

MIT AITI Swing Event Model Lecture 17 MIT AITI 2004 Swing Event Model Lecture 17 The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the user. But how do GUIs interact with users? How do applications

More information

CS 11 java track: lecture 3

CS 11 java track: lecture 3 CS 11 java track: lecture 3 This week: documentation (javadoc) exception handling more on object-oriented programming (OOP) inheritance and polymorphism abstract classes and interfaces graphical user interfaces

More information

Swing from A to Z Some Simple Components. Preface

Swing from A to Z Some Simple Components. Preface By Richard G. Baldwin baldwin.richard@iname.com Java Programming, Lecture Notes # 1005 July 31, 2000 Swing from A to Z Some Simple Components Preface Introduction Sample Program Interesting Code Fragments

More information

Class 16: The Swing Event Model

Class 16: The Swing Event Model Introduction to Computation and Problem Solving Class 16: The Swing Event Model Prof. Steven R. Lerman and Dr. V. Judson Harward 1 The Java Event Model Up until now, we have focused on GUI's to present

More information

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Frames, GUI and events Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Introduction to Swing The Java AWT (Abstract Window Toolkit)

More information

Array. Prepared By - Rifat Shahriyar

Array. Prepared By - Rifat Shahriyar Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types

More information

encompass a group of features for building Graphical User Interfaces (GUI).

encompass a group of features for building Graphical User Interfaces (GUI). Java GUI (intro) JFC Java Foundation Classes encompass a group of features for building Graphical User Interfaces (GUI). javax.swing.* used for building GUIs. Some basic functionality is already there

More information

CMP 326 Midterm Fall 2015

CMP 326 Midterm Fall 2015 CMP 326 Midterm Fall 2015 Name: 1) (30 points; 5 points each) Write the output of each piece of code. If the code gives an error, write any output that would happen before the error, and then write ERROR.

More information

RAIK 183H Examination 2 Solution. November 10, 2014

RAIK 183H Examination 2 Solution. November 10, 2014 RAIK 183H Examination 2 Solution November 10, 2014 Name: NUID: This examination consists of 5 questions and you have 110 minutes to complete the test. Show all steps (including any computations/explanations)

More information

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure. Java in 21 minutes hello world basic data types classes & objects program structure constructors garbage collection I/O exceptions Strings Hello world import java.io.*; public class hello { public static

More information

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference CSI1102: Introduction to Software Design Chapter 5: Enhancing Classes Learning objectives: Enhancing Classes Understand what the following entails Different object references and aliases Passing objects

More information

PART1: Choose the correct answer and write it on the answer sheet:

PART1: Choose the correct answer and write it on the answer sheet: PART1: Choose the correct answer and write it on the answer sheet: (15 marks 20 minutes) 1. Which of the following is included in Java SDK? a. Java interpreter c. Java disassembler b. Java debugger d.

More information

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User CSE3461 Control Flow Paradigms: Reacting to the User Control Flow: Overview Definition of control flow: The sequence of execution of instructions in a program. Control flow is determined at run time by

More information

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7) Software Development & Education Center Java Platform, Standard Edition 7 (JSE 7) Detailed Curriculum Getting Started What Is the Java Technology? Primary Goals of the Java Technology The Java Virtual

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. SOLUTION HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2006 FINAL EXAMINATION 7pm to 10pm, 19 DECEMBER 2006, Jeffrey Hall 1 st Floor Instructor:

More information

Java for Interfaces and Networks (DT3010, HT10)

Java for Interfaces and Networks (DT3010, HT10) Java for Interfaces and Networks (DT3010, HT10) More Basics: Classes, Exceptions, Garbage Collection, Interfaces, Packages Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se

More information

Declarations and Access Control SCJP tips

Declarations and Access Control  SCJP tips Declarations and Access Control www.techfaq360.com SCJP tips Write code that declares, constructs, and initializes arrays of any base type using any of the permitted forms both for declaration and for

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double(3.14); 4... Zheng-Liang Lu Java Programming 290 / 321 Wrapper Classes To treat values as objects, Java supplies standard wrapper classes for each primitive type. For example, you can construct a wrapper object from a primitive value or from a string representation

More information

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts SE1021 Exam 2 Name: You may use a note-sheet for this exam. But all answers should be your own, not from slides or text. Review all questions before you get started. The exam is printed single-sided. Write

More information

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013 Nested Classes in Java Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013 1 In This Tutorial Explanation of the nested class concept. Access modifiers and nested classes. The types

More information

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features: Lecture 28 Exceptions and Inner Classes Goals We are going to talk in more detail about two advanced Java features: Exceptions supply Java s error handling mechanism. Inner classes ease the overhead of

More information

Timing for Interfaces and Abstract Classes

Timing for Interfaces and Abstract Classes Timing for Interfaces and Abstract Classes Consider using abstract classes if you want to: share code among several closely related classes declare non-static or non-final fields Consider using interfaces

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2006 FINAL EXAMINATION 7pm to 10pm, 19 DECEMBER 2006, Jeffrey Hall 1 st Floor Instructor: Alan

More information

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics 1 Collections (from the Java tutorial)* A collection (sometimes called a container) is simply an object that

More information

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University March 10, 2008 / Lecture 8 Outline Course Status Course Information & Schedule

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

About this exam review

About this exam review Final Exam Review About this exam review I ve prepared an outline of the material covered in class May not be totally complete! Exam may ask about things that were covered in class but not in this review

More information

Final Exam CS 251, Intermediate Programming December 10, 2014

Final Exam CS 251, Intermediate Programming December 10, 2014 Final Exam CS 251, Intermediate Programming December 10, 2014 Name: NetID: Answer all questions in the space provided. Write clearly and legibly, you will not get credit for illegible or incomprehensible

More information

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class CHAPTER GRAPHICAL USER INTERFACES 10 Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11 10.1 Frame Windows Java provides classes to create graphical applications that can run on any major graphical

More information

Goal. Generic Programming and Inner classes. Minor rewrite of linear search. Obvious linear search code. Intuitive idea of generic linear search

Goal. Generic Programming and Inner classes. Minor rewrite of linear search. Obvious linear search code. Intuitive idea of generic linear search Goal Generic Programming and Inner classes First version of linear search Input was array of int More generic version of linear search Input was array of Comparable Can we write a still more generic version

More information

enum Types 1 1 The keyword enum is a shorthand for enumeration. Zheng-Liang Lu Java Programming 267 / 287

enum Types 1 1 The keyword enum is a shorthand for enumeration. Zheng-Liang Lu Java Programming 267 / 287 enum Types 1 An enum type is an reference type limited to an explicit set of values. An order among these values is defined by their order of declaration. There exists a correspondence with string names

More information

RAIK 183H Examination 2 Solution. November 11, 2013

RAIK 183H Examination 2 Solution. November 11, 2013 RAIK 183H Examination 2 Solution November 11, 2013 Name: NUID: This examination consists of 5 questions and you have 110 minutes to complete the test. Show all steps (including any computations/explanations)

More information

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide Topics for Exam 2: Queens College, CUNY Department of Computer Science CS 212 Object-Oriented Programming in Java Practice Exam 2 CS 212 Exam 2 Study Guide Linked Lists define a list node define a singly-linked

More information

Lecture 2, September 4

Lecture 2, September 4 Lecture 2, September 4 Intro to C/C++ Instructor: Prashant Shenoy, TA: Shashi Singh 1 Introduction C++ is an object-oriented language and is one of the most frequently used languages for development due

More information

CMSC 331 Second Midterm Exam

CMSC 331 Second Midterm Exam 1 20/ 2 80/ 331 First Midterm Exam 11 November 2003 3 20/ 4 40/ 5 10/ CMSC 331 Second Midterm Exam 6 15/ 7 15/ Name: Student ID#: 200/ You will have seventy-five (75) minutes to complete this closed book

More information

Java Help Files. by Peter Lavin. May 22, 2004

Java Help Files. by Peter Lavin. May 22, 2004 Java Help Files by Peter Lavin May 22, 2004 Overview Help screens are a necessity for making any application user-friendly. This article will show how the JEditorPane and JFrame classes, along with HTML

More information

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277 CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) 562-5800 x 6277 elsaddik @ site.uottawa.ca abed @ mcrlab.uottawa.ca http://www.site.uottawa.ca/~elsaddik/

More information

Lecture 5: Java Graphics

Lecture 5: Java Graphics Lecture 5: Java Graphics CS 62 Spring 2019 William Devanny & Alexandra Papoutsaki 1 New Unit Overview Graphical User Interfaces (GUI) Components, e.g., JButton, JTextField, JSlider, JChooser, Containers,

More information

Graphics User Defined Forms, Part I

Graphics User Defined Forms, Part I Graphics User Defined Forms, Part I Quick Start Compile step once always mkdir labs javac PropertyTax5.java cd labs mkdir 5 Execute step cd 5 java PropertyTax5 cp /samples/csc/156/labs/5/*. cp PropertyTax1.java

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to define and invoke void and return java methods JAVA

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 35 November 29, 2017 Swing II: Building GUIs Inner Classes Chapter 29 Announcements Game Project Complete Code Due: December 11 th NO LATE SUBMISSIONS

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #19: November 4, 2015 1/14 Third Exam The third, Checkpoint Exam, will be on: Wednesday, November 11, 2:30 to 3:45 pm You will have 3 questions, out of 9,

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid Systems Programming Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid Leganés, 21st of March, 2014. Duration: 75 min. Full

More information

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068 Welcome to! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) Overview JAVA and GUIs: SWING Container, Components, Layouts Using SWING Streams and Files Text

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC124, WINTER TERM, 2009 FINAL EXAMINATION 7pm to 10pm, 18 APRIL 2009, Dunning Hall Instructor: Alan McLeod If the

More information

A sample print out is: is is -11 key entered was: w

A sample print out is: is is -11 key entered was: w Lab 9 Lesson 9-2: Exercise 1, 2 and 3: Note: when you run this you may need to maximize the window. The modified buttonhandler is: private static class ButtonListener implements ActionListener public void

More information

AP CS Unit 11: Graphics and Events

AP CS Unit 11: Graphics and Events AP CS Unit 11: Graphics and Events This packet shows how to create programs with a graphical interface in a way that is consistent with the approach used in the Elevens program. Copy the following two

More information

Introduction to Functional Programming in Java 8

Introduction to Functional Programming in Java 8 1 Introduction to Functional Programming in Java 8 Java 8 is the current version of Java that was released in March, 2014. While there are many new features in Java 8, the core addition is functional programming

More information

Class, Variable, Constructor, Object, Method Questions

Class, Variable, Constructor, Object, Method Questions Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx

More information

Introduction IS

Introduction IS Introduction IS 313 4.1.2003 Outline Goals of the course Course organization Java command line Object-oriented programming File I/O Business Application Development Business process analysis Systems analysis

More information

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended JAVA Classes Class definition complete definition [public] [abstract] [final] class Name [extends Parent] [impelements ListOfInterfaces] {... // class body public public class abstract no instance can

More information

Java for Interfaces and Networks (DT3010, HT10)

Java for Interfaces and Networks (DT3010, HT10) Java for Interfaces and Networks (DT3010, HT10) Mouse Events, Timers, Serialization Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces

More information

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println("The age is " + age); }

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println(The age is  + age); } Question No :1 What is the correct ordering for the import, class and package declarations when found in a Java class? 1. package, import, class 2. class, import, package 3. import, package, class 4. package,

More information

CS 180 Final Exam Review 12/(11, 12)/08

CS 180 Final Exam Review 12/(11, 12)/08 CS 180 Final Exam Review 12/(11, 12)/08 Announcements Final Exam Thursday, 18 th December, 10:20 am 12:20 pm in PHYS 112 Format 30 multiple choice questions 5 programming questions More stress on topics

More information

Object Oriented Software Design

Object Oriented Software Design Object Oriented Software Design I/O subsystem API Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 10, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction to Java

More information

CIS 120 Programming Languages and Techniques. Midterm II. November 12, 2010

CIS 120 Programming Languages and Techniques. Midterm II. November 12, 2010 CIS 120 Programming Languages and Techniques Midterm II November 12, 2010 Name: Pennkey: Scores: 1 2 3 4 5 6 Total (50 max) 1. (14 points) Pages 7 to 9 define a simplified version of the Java Collection

More information

Chapter 2. Network Chat

Chapter 2. Network Chat Chapter 2. Network Chat In a multi-player game, different players interact with each other. One way of implementing this is to have a centralized server that interacts with each client using a separate

More information

Object Oriented Software Design

Object Oriented Software Design Object Oriented Software Design I/O subsystem API Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 10, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction to Java

More information

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015 CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015 Name: This exam consists of 6 problems on the following 6 pages. You may use your double- sided hand- written 8 ½ x 11 note sheet

More information

Lecture 2: Java & Javadoc

Lecture 2: Java & Javadoc Lecture 2: Java & Javadoc CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Instance Variables or member variables or fields Declared in a class, but outside of any method, constructor or block

More information

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015 CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015 Name: This exam consists of 5 problems on the following 7 pages. You may use your double- sided hand- written 8 ½ x 11 note sheet

More information

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours Java for Programmers Course (equivalent to SL 275) 36 Contact Hours Course Overview This course teaches programmers the skills necessary to create Java programming system applications and satisfies the

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09 Object-Oriented Programming: Revision / Graphics / Subversion Inf1 :: 2008/09 Breaking out of loops, 1 Task: Implement the method public void contains2(int[] nums). Given an array of ints and a boolean

More information

Another IS-A Relationship

Another IS-A Relationship Another IS-A Relationship Not all classes share a vertical relationship. Instead, some are supposed to perform the specific methods without a vertical relationship. Consider the class Bird inherited from

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

Casting -Allows a narrowing assignment by asking the Java compiler to trust us Primitives Integral types: int, short, long, char, byte Floating point types: double, float Boolean types: boolean -passed by value (copied when returned or passed as actual parameters) Arithmetic Operators:

More information

JavaScript: Sort of a Big Deal,

JavaScript: Sort of a Big Deal, : Sort of a Big Deal, But Sort of Quirky... March 20, 2017 Lisp in C s Clothing (Crockford, 2001) Dynamically Typed: no static type annotations or type checks. C-Like Syntax: curly-braces, for, semicolons,

More information

SampleApp.java. Page 1

SampleApp.java. Page 1 SampleApp.java 1 package msoe.se2030.sequence; 2 3 /** 4 * This app creates a UI and processes data 5 * @author hornick 6 */ 7 public class SampleApp { 8 private UserInterface ui; // the UI for this program

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

CS11 Java. Fall Lecture 4

CS11 Java. Fall Lecture 4 CS11 Java Fall 2014-2015 Lecture 4 Java File Objects! Java represents files with java.io.file class " Can represent either absolute or relative paths! Absolute paths start at the root directory of the

More information

M257 Past Paper Oct 2007 Attempted Solution

M257 Past Paper Oct 2007 Attempted Solution M257 Past Paper Oct 2007 Attempted Solution Part 1 Question 1 The compilation process translates the source code of a Java program into bytecode, which is an intermediate language. The Java interpreter

More information

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping. HW#1: Pencil Me In Status!? CS193J: Programming in Java Summer Quarter 2003 Lecture 6 Inner Classes, Listeners, Repaint Manu Kumar sneaker@stanford.edu How was Homework #1? Comments please? SITN students

More information

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN CSSE 220 Event Based Programming Check out EventBasedProgramming from SVN Interfaces are contracts Interfaces - Review Any class that implements an interface MUST provide an implementation for all methods

More information

CSE 307: Principles of Programming Languages

CSE 307: Principles of Programming Languages 1 / 26 CSE 307: Principles of Programming Languages Names, Scopes, and Bindings R. Sekar 2 / 26 Topics Bindings 1. Bindings Bindings: Names and Attributes Names are a fundamental abstraction in languages

More information

Lab 1 : Java Sockets

Lab 1 : Java Sockets Lab 1 : Java Sockets 1. Goals In this lab you will work with a low-level mechanism for distributed communication. You will discover that Java sockets do not provide: - location transparency - naming transparency

More information

Final Exam CS 251, Intermediate Programming December 13, 2017

Final Exam CS 251, Intermediate Programming December 13, 2017 Final Exam CS 251, Intermediate Programming December 13, 2017 Name: NetID: Answer all questions in the space provided. Write clearly and legibly, you will not get credit for illegible or incomprehensible

More information

COP 3330 Final Exam Review

COP 3330 Final Exam Review COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time

More information

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Handout 7. Defining Classes part 1. Instance variables and instance methods. Handout 7 CS180 Programming Fundamentals Spring 15 Page 1 of 8 Handout 7 Defining Classes part 1. Instance variables and instance methods. In Object Oriented programming, applications are comprised from

More information

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

JRadioButton account_type_radio_button2 = new JRadioButton(Current); ButtonGroup account_type_button_group = new ButtonGroup(); Q)Write a program to design an interface containing fields User ID, Password and Account type, and buttons login, cancel, edit by mixing border layout and flow layout. Add events handling to the button

More information

Interfaces and related issues Reading for these lectures: Weiss, Section 4.4 (The interface), p no multiple inheritance with classes

Interfaces and related issues Reading for these lectures: Weiss, Section 4.4 (The interface), p no multiple inheritance with classes Interfaces and related issues Reading for these lectures: Weiss, Section 4.4 (The interface), p. 110. no multiple inheritance with classes public class Student { public class Employee { public class StudentEmployee

More information