Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CIS 120 Final Exam 16 December Name (printed): Pennkey (login id):

CS 134 Programming Exercise 7:

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

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

CIS 120 Final Exam 15 December 2017 SOLUTIONS

CIS 120 Final Exam 15 December 2017

Midterm assessment - MAKEUP Fall 2010

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

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

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Assignment Tutorial.

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120e)

CSE 1325 Project Description

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

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

News and info. Array. Feedback. Lab 4 is due this week. It should be easy to change the size of the game grid.

ITEC 120 4/14/11. Review. Need. Objectives. GUI basics JFrame JPanel JLabel, JButton, JTextField Layout managers. Lecture 38 GUI Interactivity

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 2: April 16, 2004

Lecture 19 GUI Events

Jonathan Aldrich Charlie Garrod

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Quiz 2: April 16, 2004

CSE 331 Software Design & Implementation

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

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

Java Programming Lecture 6

Announcements. Lab 03 inbase late submissions Midterm #1 Friday. Project 2 is posted. Covers everything through basics of OOP

Programming Languages and Techniques (CIS120)

To gain experience using GUI components and listeners.

CSIS 10A Assignment 7 SOLUTIONS

To gain experience using recursion and recursive data structures.

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

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

CS Exam 1 Review Suggestions

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

MIT AITI Swing Event Model Lecture 17

Charlie Garrod Michael Hilton

Phase 2: Due 05/02/18 The Gui now stores and displays a graph in response to user mouse clicks on the screen.

Building custom components IAT351

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Class 16: The Swing Event Model

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

San Jose State University College of Science Department of Computer Science CS151, Object-Oriented Design, Sections 1, 2, and 3, Spring 2018

Programming Languages and Techniques (CIS120)

CS11 Java. Fall Lecture 3

1. (5 points) In your own words, describe what an instance is.

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

Chapter 5: Enhancing Classes

Announcements. 1. Forms to return today after class:

To help you prepare for Problem 2, you are to write a simple Swing application which uses an anonymous inner class to control the application.

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

Objects. Prof. Clarkson Fall ob-ject: to feel distaste for something Webster's Dictionary. Today s music: Kung Fu Fighting by CeeLo Green

Phase 2: Due 11/28/18: Additional code to add, move and remove vertices in response to user mouse clicks on the screen.

We are on the GUI fast track path

Logistics. Final Exam on Friday at 3pm in CHEM 102

Java: introduction to object-oriented features

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

Introduction to concurrency and GUIs

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

1.00/ Introduction to Computers and Engineering Problem Solving. Quiz 2 / November 5, 2004

Final Exam CS 251, Intermediate Programming December 13, 2017

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

Anonymous Classes. A short-cut for defining classes when you want to create only one object of the class.

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Oracle. All rights reserved. This content is excluded from our Creative Commons license. For more information, see

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

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Mid-Semester Feedback

CSE115 Lab 9 Fall 2016

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Introduction to Computers and Engineering Problem Solving Spring 2012 Problem Set 5: MBTA routes Due: 12 noon, Friday, March 23

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

Graphical Applications

First Name: AITI 2004: Exam 2 July 19, 2004

Throughout the exam, write concisely and underline key words or phrases. Have fun! Exam 2. Week 7 (Winter 2013). Dr. Yoder. Sec 031.

Systems Programming Graphical User Interfaces

Lecture 5: Java Graphics

CS415 Human Computer Interaction

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Mobile Development Workshop DAY 2: INTRODUCTION TO JAVA

Object Orientation Fourth Story. Bok, Jong Soon

AP CS Unit 11: Graphics and Events

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Charlie Garrod Bogdan Vasilescu

Transcription:

Programming Languages and Techniques (CIS120) Lecture 34 April 11, 2016 Swing II: Inner Classes and Layout

Announcements (Review) Final exam: May 2, 3-5PM If you have two finals at the same time, you can postpone one If you have 3 finals on one day, you may postpone the middle one If either case applies to you, add your name to the form on the website Must discuss all other exam conflicts with course instructors

Game project (Review) Game Design Proposal Milestone Due: (12 points) Friday April 13 th at NOON!!!! (Should take about 1 hour) Submit on GRADESCOPE TAs will give you feedback over the weekend Final Program Due: (88 points) Wednesday, April 25 th at 11:59pm Submit zipfile online, submission only checks if your code compiles Eclipse is STRONGLY recommended for this project May distribute your game (after the deadline) if you do not use any of our code Grade based on demo with your TA during reading days Grading rubric on the assignment website Recommendation: don t be too ambitious. NO LATE SUBMISSIONS PERMITTED

How to have first-class computation? class ButtonListener implements ActionListener { private LightBulb bulb; public ButtonListener (LightBulb b) { bulb = b; @Override public void actionperformed(actionevent e) { bulb.flip(); bulb.repaint(); // somewhere in run LightBulb bulb = new LightBulb(); JButton button = new JButton("On/Off"); button.addactionlistener(new ButtonListener(bulb)); let bulb, bulb_flip = make_bulb () let onoff,_, bnc = button "ON/Off" ;; bnc.add_event_listener (mouseclick_listener bulb_flip)

OnOff.java revisited public void run() { //... other code omitted LightBulb bulb = new LightBulb(); panel.add(bulb); JButton button = new JButton("On/Off"); panel.add(button); button.addactionlistener(new ButtonListener(bulb)); //... other code omitted

Too much boilerplate code ButtonListener really only needs to do bulb.flip() But we need all this boilerplate code to build the class Often we will only instantiate one instance of a given Listener class in a GUI class ButtonListener implements ActionListener { private LightBulb bulb; public ButtonListener (LightBulb b) { bulb = b; @Override public void actionperformed(actionevent e) { bulb.flip(); bulb.repaint();

Inner Classes

Inner Classes Useful in situations where two objects require deep access to each other s internals Replaces tangled workarounds like owner object Solution with inner classes is easier to read No need to allow public access to instance variables of outer class Also called dynamic nested classes

Basic Example Key idea: Classes can be members of other classes class Outer { private int outervar; public Outer () { outervar = 6; public class Inner { private int innervar; public Inner(int z) { innervar = outervar + z; public int getinnervar() { return innervar; Name of this class is Outer.Inner (which is also the static type of objects that this class creates) Reference from inner class to instance variable bound in outer class

Constructing Inner Class Objects class Outer { private int outervar; public Outer () { outervar = 6; public class Inner { private int innervar; public Inner(int z) { innervar = outervar + z; public int getinnervar() { return innervar; Based on your understanding of the Java object model, which of the following make sense as ways to construct an object of an inner class type? 1. Outer.Inner obj = new Outer.Inner(2); 2. Outer.Inner obj = (new Outer()).new Inner(2); 3. Outer.Inner obj = new Inner(2); 4. Outer.Inner obj = Outer.Inner.new(2); Answer: 2 the inner class instances can refer to non-static fields of the outer class (even in the constructor), so the invocation of "new" must be relative to an existing instance of the Outer class.

Object Creation Inner classes can refer to the instance variables and methods of the outer class Inner class instances usually created by the methods/constructors of the outer class public Outer () { Inner b = new Inner (); Inner class instances cannot be created independently of a containing class instance. Outer.Inner b = new Outer.Inner() Outer a = new Outer(); Outer.Inner b = a.new Inner(); Actually this.new Outer.Inner b = (new Outer()).new Inner();

Quiz public class Demo { private JLabel label1 = new JLabel("a label"); void m(jlabel label2) { JLabel label3 = new JLabel("another label"); JButton button = new JButton("button"); button.addactionlistener(new ActionListener() { ); @Override public void actionperformed(actionevent e) { label1.settext("label1"); // 1 label2.settext("label2"); // 2 label3.settext("label3"); // 3 Which reference is allowed? 1. label1 only 2. label2 only 3. label3 only 4. all are 5. none are

Anonymous Inner Classes Define a class and create an object from it all at once, inside a method Puts button action right with button definition final LightBulb bulb = new LightBulb(); JButton button = new JButton("On/Off"); button.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { bulb.flip(); bulb.repaint(); ); Can access fields and methods of outer class, as well as final local variables

Anonymous Inner class New expression form: define a class and create an object from it all at once New keyword new InterfaceOrClassName() { public void method1(int x) { // code for method1 public void method2(char y) { // code for method2 Normal class definition, no constructors allowed Static type of the expression is the Interface/superclass used to create it Dynamic class of the created object is anonymous! Can't refer to it.

Like first-class functions Anonymous inner classes are the real Java equivalent of Ocaml first-class functions Both create "delayed computation" that can be stored in a data structure and run later Code stored by the event / action listener Code only runs when the button is pressed Could run once, many times, or not at all Both sorts of computation can refer to variables in the current scope OCaml: Any available variable Java: only instance variables (fields) and variables marked final

Lambda expressions: Java 8 Define a class and create an object from it all at once, inside a method final LightBulb bulb = new LightBulb(); JButton button = new JButton("On/Off"); button.addactionlistener((e) -> { bulb.flip(); bulb.repaint(); ); Can access fields and methods of outer class, as well as final local variables

Swing Programming Demo LayoutDemo.java

What components would you use for this app?

JPanel (control_panel) GameCourt subclass of JPanel (court) JButton (reset) JPanel (status_panel) JLabel (status)

Did you attend lecture today? 1. yes 2. yes 3. yes 4. yes

What layout would you use for this app? What components would you use?

Canvas subclass of JPanel (canvas) JPanel (toolbar) JRadioButton (point, line) JCheckbox (thick) JButton (quit)