class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } }

Similar documents
Chapter Nine: Interfaces and Polymorphism. Big Java by Cay Horstmann Copyright 2008 by John Wiley & Sons. All rights reserved.

3/7/2012. Chapter Nine: Interfaces and Polymorphism. Chapter Goals

10/27/2011. Chapter Goals

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

Chapter 9 Interfaces and Polymorphism. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

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

Chapter 9. Interfaces and Polymorphism. Chapter Goals. Chapter Goals. Using Interfaces for Code Reuse. Using Interfaces for Code Reuse

Interfaces and Polymorphism Advanced Programming

public DataSet(Measurer ameasurer) { sum = 0; count = 0; minimum = null; maximum = null; measurer = ameasurer; }

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

Object Oriented Programming

User interfaces and Swing

Submit your solution to the Desire2Learn drop box. You may submit as many versions as you like the last one will be graded.

Check out ThreadsIntro project from SVN. Threads and Animation

Chapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

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

Graphical User Interfaces 2

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

Programming Graphics (P1 2006/2007)

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

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

Handling Mouse and Keyboard Events

APPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }

Graphical User Interfaces 2

Programming Languages and Techniques (CIS120)

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

H212 Introduction to Software Systems Honors

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

Programming Languages and Techniques (CIS120)

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

3/7/2012. Chapter Ten: Inheritance. Chapter Goals

Graphical User Interfaces 2

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

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

Programming Languages and Techniques (CIS120)

// autor igre Ivan Programerska sekcija package mine;

Programming Languages and Techniques (CIS120)

COMP 102: Test 2 Model Solutions

Sample Solution A10 R6.2 R6.3 R6.5. for (i = 10; i >= 0; i++)... never ends. for (i = -10; i <= 10; i = i + 3)... 6 times

THE UNIVERSITY OF AUCKLAND

Programming Languages and Techniques (CIS120)

P 6.3) import java.util.scanner;

SampleApp.java. Page 1

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

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

Sample Solution Assignment 5. Question R3.1

G51PRG: Introduction to Programming Second semester Applets and graphics

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

CHAPTER 9 INTERFACES AND POLYMORPHISM

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

FirstSwingFrame.java Page 1 of 1

DM503 Programming B. Peter Schneider-Kamp.

Computer Science 210: Data Structures. Intro to Java Graphics

Lecture 5: Java Graphics

H212 Introduction to Software Systems Honors

ว ฒนพงศ ส ทธภ กด Java Programming ( )

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Graphical User Interface

Dartmouth College Computer Science 10, Fall 2015 Midterm Exam

Section Basic graphics

javax.swing Swing Timer

Outline. More on the Swing API Graphics: double buffering and timers Model - View - Controller paradigm Applets

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

import java.applet.applet; import java.applet.audioclip; import java.net.url; public class Vjesala2 {

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

Exam: Applet, Graphics, Events: Mouse, Key, and Focus

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

AP CS Unit 12: Drawing and Mouse Events

EXAMINATIONS 2010 END-OF-YEAR COMP 102 INTRODUCTION TO COMPUTER PROGRAM DESIGN

AnimatedImage.java. Page 1

Dr. Hikmat A. M. AbdelJaber

Lecture 3: Java Graphics & Events

2IS45 Programming

Outline. Anonymous Classes. Annoucements. CS1007: Object Oriented Design and Programming in Java

Example: Building a Java GUI

Unit 7: Event driven programming

Java Coordinate System

Example: Building a Java GUI

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

Workbook 7. Remember to check the course website regularly for announcements and errata:

Graphic User Interfaces. - GUI concepts - Swing - AWT

CompSci 230 S Programming Techniques. Basic GUI Components

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

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

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

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

PIC 20A GUI with swing

AppBisect > PrBisect > class Functie. AppBisect > PrBisect > class Punct. public class Functie { double x(double t) { return t;

JComponent. JPanel. JFrame. JFrame JDialog, JOptionPane. JPanel. JPanel

UCLA PIC 20A Java Programming

TYPES OF INTERACTORS Prasun Dewan Department of Computer Science University of North Carolina at Chapel Hill

Object Orientated Programming in Java. Benjamin Kenwright

CS 2113 Software Engineering

AP CS Unit 11: Graphics and Events

Java for Interfaces and Networks (DT3010, HT10)

COMP16121 Sample Code Lecture 1

Advanced Java Unit 6: Review of Graphics and Events

Building Java Programs Bonus Slides

Transcription:

9.12) public interface Filter boolean accept(object x); Describes any class whose objects can measure other objects. public interface Measurer double measure(object anobject); This program tests the use of a Measurer and a Filter. public class DataSetTester class BankMeasurer implements Measurer public double measure(object anobject) BankAccount ba = (BankAccount) anobject; return ba.getbalance(); class BankFilter implements Filter public boolean accept(object x) BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; Measurer m = new BankMeasurer(); Filter f = new BankFilter(); DataSet data = new DataSet(m, f); data.add(new BankAccount(1)); data.add(new BankAccount(100));

data.add(new BankAccount(2000)); data.add(new BankAccount(950)); data.add(new BankAccount(4000)); System.out.println("Average balance: " + data.getaverage()); System.out.println("Expected: 3000"); BankAccount b = (BankAccount) data.getmaximum(); double balance = b.getbalance(); System.out.println("Highest balance: " + balance); System.out.println("Expected: 4000"); 9.21) import java.awt.event.actionevent; import java.awt.event.actionlistener; An action listener that prints a message. public class ClickListener implements ActionListener private int count; public ClickListener() count = 0; public void actionperformed(actionevent event) count++; if (count == 1) System.out.println("I was clicked 1 time!"); else System.out.println("I was clicked " + count + " times!"); import java.awt.event.actionlistener; import javax.swing.jbutton;

import javax.swing.jframe; import javax.swing.jpanel; This program demonstrates how to install an action listener. public class ButtonViewer private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 100; JFrame frame = new JFrame(); JPanel panel = new JPanel(); JButton button = new JButton("Click me!"); panel.add(button); JButton button2 = new JButton("Click me too!"); panel.add(button2); frame.add(panel); ActionListener listener = new ClickListener(); button.addactionlistener(listener); ActionListener listener2 = new ClickListener(); button2.addactionlistener(listener2); frame.setsize(frame_width, FRAME_HEIGHT); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); 9.28) import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.joptionpane; import javax.swing.timer; import java.util.date;

Displays the current time once every second. public class TimePrinter class CurrentTime implements ActionListener public void actionperformed(actionevent event) Date now = new Date(); System.out.println(now); CurrentTime listener = new CurrentTime(); ticks final int DELAY = 1000; // milliseconds between timer Timer t = new Timer(DELAY, listener); t.start(); JOptionPane.showMessageDialog(null, "Quit?"); System.exit(0); 9.34) Allows the user to specify a circle by typing the radius in a text field and then clicking on the center. public class CircleDrawerFrame extends JFrame private CircleDrawerComponent component; private static final int FRAME_WIDTH = 400; private static final int FRAME_HEIGHT = 400; public int radius;

public CircleDrawerFrame() component = new CircleDrawerComponent(); component.setpreferredsize(new Dimension(400,350)); // add mouse press listener class MousePressListener implements MouseListener public void mousepressed(mouseevent event) int x = event.getx(); int y = event.gety(); radius"); String ansradius = JOptionPane.showInputDialog("Enter radius = Integer.parseInt(ansRadius); component.setpositionandsize(x, y, radius); // do-nothing methods public void mousereleased(mouseevent event) public void mouseclicked(mouseevent event) public void mouseentered(mouseevent event) public void mouseexited(mouseevent event) MouseListener listener = new MousePressListener(); component.addmouselistener(listener); setsize(frame_width, FRAME_HEIGHT); add(component); public class CircleDrawerComponent extends JComponent private Ellipse2D.Double circle; private int x; private int y; private int radius; public CircleDrawerComponent() x = 0;

y = 0; radius = 0; circle = null; public void setpositionandsize(int ax, int ay, int aradius) x = ax; y = ay; radius = aradius; // the circle that the paintcomponent method draws circle = new Ellipse2D.Double(x - radius, y - radius, radius * 2, radius * 2); repaint(); public void paintcomponent(graphics g) if (circle == null) return; Graphics2D g2 = (Graphics2D) g; g2.draw(circle); import javax.swing.jframe; Allows the user to specify a circle by typing the radius in a text field and then clicking on the center. public class CircleDrawer JFrame frame = new CircleDrawerFrame(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.settitle("circledrawer"); frame.setvisible(true);