We are on the GUI fast track path

Similar documents
Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

25. Interfaces. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CS171:Introduction to Computer Science II. Li Xiong

Chapter 9 Abstract Classes and Interfaces

Chapter 13 Abstract Classes and Interfaces

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

Chapter 14 Abstract Classes and Interfaces

26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

CS1150 Principles of Computer Science Objects and Classes

Chapter 13 Abstract Classes and Interfaces

Motivations. Chapter 13 Abstract Classes and Interfaces

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Chapter 13 Abstract Classes and Interfaces

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

What is an interface? Why is an interface useful?

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Abstract Classes Interfaces

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Inheritance and Polymorphism

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Chapter 11 Inheritance and Polymorphism

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

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

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Introduction Programming Using Python Lecture 8. Dr. Zhang COSC 1437 Fall 2017 Nov 30, 2017

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

CS-202 Introduction to Object Oriented Programming

Chapter 11 Inheritance and Polymorphism

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

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

25. Generic Programming

Abstract Classes and Interfaces. CSE 114, Computer Science 1 Stony Brook University

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Chapter 8 Objects and Classes

Interface Class. Lecture 22. Based on Slides of Dr. Norazah Yusof

ITI Introduction to Computing II

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

ITI Introduction to Computing II

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Inheritance (continued) Inheritance

Chapter 11 Inheritance and Polymorphism

AP CS Unit 11: Graphics and Events

OO Programming Concepts

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

8. Polymorphism and Inheritance

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

Polymorphism and Interfaces. CGS 3416 Spring 2018

Graphic User Interfaces. - GUI concepts - Swing - AWT

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Graphical User Interfaces. Comp 152

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.

Chapter 8 Objects and Classes Part 1

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

Chapter 6: Inheritance

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

Java Programming Lecture 6

Java Object Oriented Design. CSC207 Fall 2014

Inheritance, Polymorphism, and Interfaces

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

RAIK 183H Examination 2 Solution. November 10, 2014

Packages: Putting Classes Together

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

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

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

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Introduction to the JAVA UI classes Advanced HCI IAT351

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

2. (True/False) All methods in an interface must be declared public.

Graphical User Interfaces

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

Midterm Test II Object Oriented Programming in Java Computer Science, University of Windsor Fall 2014 Time 2 hours. Answer all questions

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

SELF-STUDY. Glossary

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

Midterm assessment - MAKEUP Fall 2010

COP 3330 Final Exam Review

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

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

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

1 Shyam sir JAVA Notes

C20a: Selected Interfaces in Java API

Transcription:

We are on the GUI fast track path Chapter 13: Exception Handling Skip for now Chapter 14: Abstract Classes and Interfaces Sections 1 9: ActionListener interface Chapter 15: Graphics Skip for now Chapter 16: Event-Driven Programming Sections 1 7: Events, registering listeners, JTextField input Chapter 17: Creating Graphical User Interfaces Sections 1 8: JButton, JCheckBox, JRadioButton, JLabel JTextField, JTextArea, JComboBox Chapter 18: Applets and Multimedia Sections 1 7, 10, 11: Applets, Apps, and Audio Chapter 33: Swing Containers, Layouts, and Borders Sections 1 3, 8: CardLayout, BoxLayout, null Layout, Border Chapter 34: Menus, Toolbars, and Dialogs Copyright 2012 R.M. Laurie 1 Ch 14: Abstract Classes and Interfaces Write Event Handler code to respond to user actions such as clicking a button Abstract Classes are general and not concrete Cannot create new objects from Abstract Classes Example: public abstract class GeometricObject Abstract methods describe common methods for subclasses that will be defined in the subclass Abstract methods make the class abstract Denoted by Italics names in UML Abstract methods are defined without implementation public abstract double getarea(); Constructors of abstract class is defined protected protected GeometicObject(); It may only be used by subclasses and objects cannot be created Polymorphic way to utilize commonly named methods Copyright 2012 R.M. Laurie 2 Copyright 2012 R.M. Laurie 1

Abstract Classes and Abstract Methods The # sign indicates protected modifier Abstract methods are italicized -radius: double +Circle() Circle +Circle(radius: double) -color: String -filled: boolean +Circle(radius: double, color: string, filled: boolean) +getradius(): double +setradius(radius: double): void +getdiameter(): double GeometricObject -datecreated: java.util.date #GeometricObject() #GeometricObject(color: string, filled: boolean) +getcolor(): String +setcolor(color: String): void +isfilled(): boolean +setfilled(filled: boolean): void +getdatecreated(): java.util.date +tostring(): String +getarea(): double +getperimeter(): double -width: double -height: double +Rectangle() Rectangle +Rectangle(width: double, height: double) +Rectangle(width: double, height: double, color: string, filled: boolean) +getwidth(): double +setwidth(width: double): void +getheight(): double Abstract class +setheight(height: double): void In a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass. A subclass can be abstract even if its superclass is concrete. Methods getarea and getperimeter are overridden in Circle and Rectangle. Superclass methods are generally omitted in the UML diagram for subclasses. You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. Copyright 2012 R.M. Laurie 3 Interface in Java An interface is a class like program construct Contains only constants and abstract methods Similar to an abstract class, but the intent different Interface specifies behavior for objects For example, using interfaces objects specified as Comparable Edible Cloneable public interface InterfaceName { constant declarations; method signatures; interface is compiled into a separate bytecode file Like an abstract class, you cannot create an instance from an interface using the new operator An interface can be used as a data type for a variable and for casting Copyright 2012 R.M. Laurie 4 Copyright 2012 R.M. Laurie 2

Interface is a Special Class 1. public class TestEdible { 2. public static void main(string[] args) { 3. Object[] objects = {new Tiger(), new Chicken(), new Apple(); 4. for (int i = 0; i < objects.length; i++) 5. if (objects[i] instanceof Edible) 6. System.out.println(((Edible)objects[i]).howToEat()); 7. 8. 9. class Animal { 10. // Data fields, constructors, and methods omitted here 11. 12. class Chicken extends Animal implements Edible { 13. public String howtoeat() { 14. return "Chicken: Fry it"; 15. 16. 17. class Tiger extends Animal { 18. 19. abstract class Fruit implements Edible { 20. // Data fields, constructors, and methods omitted here 21. 22. class Apple extends Fruit { 23. public String howtoeat() { 24. return "Apple: Make apple cider"; 25. 26. 27. class Orange extends Fruit { 28. public String howtoeat() { 29. return "Orange: Make orange juice"; 30. 31. 1. public interface Edible { 2. /** Describe how to eat */ 3. public abstract String howtoeat(); 4. You can now use the Edible interface to specify whether an object is edible. This is accomplished by letting the class for the object implement this interface using the implements keyword. For example, the classes Chicken and Fruit implement the Edible interface. Omitting Modifiers in Interfaces All data fields are public final static and all methods are public abstract in an interface. For this reason, these modifiers can be omitted, as shown below: public interface T1 { public static final int K = 1; Equivalent public interface T1 { int K = 1; public abstract void p(); void p(); A constant defined in an interface can be accessed using syntax InterfaceName.CONSTANT_NAME (e.g., T1.K). Copyright 2012 R.M. Laurie 6 Copyright 2012 R.M. Laurie 3

The Comparable Interface For comparing two objects of the same type Java provides the comparable Interface // This interface is defined in java.lang package package java.lang; public interface Comparable { public int compareto(object o); Many classes in Java library implement Comparable Define a natural order for the objects public class String extends Object implements Comparable { // class body omitted public class Date extends Object implements Comparable { // class body omitted Many classes (e.g., String and Date) in the Java library implement Comparable to define a natural order for the objects. If you examine the source code of these classes, you will see the keyword implements used in the classes, as shown below: Copyright 2012 R.M. Laurie 7 Defining Classes to Implement Comparable Notation: The interface name and the method names are italicized. The dashed lines and hollow triangles are used to point to the interface. GeometricObject Rectangle ComparableRectangle «interface» java.lang.comparable +compareto(o: Object): int ComparableRectangle rectangle1 = new ComparableRectangle(4, 5); ComparableRectangle rectangle2 = new ComparableRectangle(3, 6); System.out.println(Max.max(rectangle1, rectangle2)); 1. public class ComparableRectangle extends Rectangle 2. implements Comparable { 3. /** Construct a ComparableRectangle with specified properties */ 4. public ComparableRectangle(double width, double height) { 5. super(width, height); 6. 7. /** Implement the compareto method defined in Comparable */ 8. public int compareto(object o) { 9. if (getarea() > ((ComparableRectangle)o).getArea()) 10. return 1; 11. else if (getarea() < ((ComparableRectangle)o).getArea()) 12. return -1; 13. else 14. return 0; 15. 16. Copyright 2012 R.M. Laurie 8 Copyright 2012 R.M. Laurie 4

ActionListener Interface for GUI Source object (e.g., button) Listener object contains method for processing the event, such as button click Button is source object where action event occurs Listener object handles the action event Must be instance (object) of the ActionListener interface 1. class OKListenerClass implements ActionListener { 2. public void actionperformed(actionevent e) { 3. System.out.println("OK button clicked"); 4. 5. ActionListener object must be registered with source object using method source.addactionlistener(listener) 1. public HandleEvent() { 2. OKListenerClass listener1 = new OKListenerClass(); 3. jbtok.addactionlistener(listener1); 4. Copyright 2012 R.M. Laurie 9 1. import javax.swing.*; 2. import java.awt.event.*; 3. public class HandleEvent extends JFrame { 4. public HandleEvent() { 5. JButton jbtok = new JButton("OK"); 6. JButton jbtcancel = new JButton("Cancel"); 7. JPanel panel = new JPanel(); 8. panel.add(jbtok); 9. panel.add(jbtcancel); 10. add(panel); // Add panel to the frame ActionListener HandleEvent 2. Click OK 11. // Register listeners 12. OKListenerClass listener1 = new OKListenerClass(); 13. CancelListenerClass listener2 = new CancelListenerClass(); 14. jbtok.addactionlistener(listener1); 15. jbtcancel.addactionlistener(listener2); 16. 17. public static void main(string[] args) { 18. JFrame frame = new HandleEvent(); 19. frame.settitle("handle Event"); 20. frame.setsize(200, 150); 21. frame.setlocation(200, 100); 22. frame.setdefaultcloseoperation(jframe.exit_on_close); 23. frame.setvisible(true); 24. 25. 26. class OKListenerClass implements ActionListener { 27. public void actionperformed(actionevent e) { 28. System.out.println("OK button clicked"); 29. 30. 31. class CancelListenerClass implements ActionListener { 32. public void actionperformed(actionevent e) { 33. System.out.println("Cancel button clicked"); 34. 1. Start from the main method to create a window and display it 3. Click OK. The JVM invokes the listener s actionperformed method OK button clicked Cancel button clicked OK button clicked Copyright 2012 R.M. Laurie 5

The Cloneable Interfaces Marker Interface An empty interface marker interface does not contain constants or methods It is used to denote that a class possesses certain desirable properties A class that implements the Cloneable interface is marked cloneable, and its objects can be cloned using the clone() method from the Object class package java.lang; public interface Cloneable { Copyright 2012 R.M. Laurie 11 Interfaces vs. Abstract Classes Interface data must be constants methods have only signature and not implemented Allows multiple extensions, unlike single inheritance of class Abstract class data can have all types of data abstract class can have concrete methods. Variables Constructors Methods Abstract class No restrictions Constructors are invoked by subclasses through constructor chaining. An abstract class cannot be instantiated using the new operator. No restrictions. Interface All variables must be public static final No constructors. An interface cannot be instantiated using the new operator. All methods must be public abstract instance methods Copyright 2012 R.M. Laurie 12 Copyright 2012 R.M. Laurie 6

Interfaces vs. Abstract Classes All classes share a single root, the Object class Interfaces have no single root Like a class, an interface also defines a type. A variable of an interface type can reference any instance of the class that implements the interface. If a class extends an interface, this interface plays the same role as a superclass. You can use an interface as a data type and cast a variable of an interface type to its subclass, and vice versa. Suppose that c is an instance of Class2. c is also an instance of Object, Class1, Interface1, Interface1_1, Interface1_2, Interface2_1, and Interface2_2. Interface1_2 Interface2_2 Interface1_1 Interface1 Interface2_1 Object Class1 Class2 Copyright 2012 R.M. Laurie 13 Whether to use an interface or a class? Abstract classes are best for: strong is-a relationship that clearly describes a parent-child relationship should be modeled using classes For example, a staff member is a person. So their relationship should be modeled using class inheritance Interfaces are best for: A weak is-a relationship, also known as an is-kind-of relationship, indicates that an object possesses a certain property A weak is-a relationship can be modeled using interfaces. You can also use interfaces to circumvent single inheritance restriction if multiple inheritance is desired. In the case of multiple inheritance, you have to design one as a superclass, and others as interface. Copyright 2012 R.M. Laurie 14 Copyright 2012 R.M. Laurie 7

Wrapper Classes Boolean Character Short Byte Integer Long Float Double NOTES: 1. The wrapper classes do not have no-arg constructors. 2. The instances of all wrapper classes are immutable, i.e., their internal values cannot be changed once the objects are created. java.lang.comparable java.lang.object Number Character Boolean Double Float Long Integer Short Byte Copyright 2012 R.M. Laurie 15 Copyright 2012 R.M. Laurie 8