CS180 Recitation. More about Objects and Methods

Similar documents
More About Objects and Methods

More About Objects and Methods

Chapter 5: Enhancing Classes

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

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

Window Interfaces Using Swing. Chapter 12

Chapter 6 Introduction to Defining Classes

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

Inheritance. Chapter 7. Chapter 7 1

More About Objects and Methods

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

RAIK 183H Examination 2 Solution. November 10, 2014

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Chapter 4 Defining Classes I

Introduction to Programming Using Java (98-388)

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Chapter 4: Writing Classes

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

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

RAIK 183H Examination 2 Solution. November 11, 2013

Inheritance and Polymorphism. CS180 Fall 2007

Review sheet for Final Exam (List of objectives for this course)

Final Exam CS 251, Intermediate Programming December 13, 2017

To gain experience using GUI components and listeners.

More About Objects and Methods

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

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

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Lecture 5: Methods CS2301

Objectives. Introduce static keyword examine syntax describe common uses

JAVA MOCK TEST JAVA MOCK TEST III

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

Window Interfaces Using Swing Objects

8. Polymorphism and Inheritance

CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal

CS 251 Intermediate Programming GUIs: Event Listeners

Review. these are the instance variables. these are parameters to the methods

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

GUI Forms and Events, Part II

Anatomy of a Class Encapsulation Anatomy of a Method

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

CS 302 Week 9. Jim Williams

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Practice Questions for Chapter 9

Objects and Classes -- Introduction

Bitwise Operators Objects and Methods

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CSE 70 Final Exam Fall 2009

B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA

CS212 - COMPUTATIONAL STRUCTURES AND ALGORITHMS

Objects and Iterators

OLLSCOIL NA heireann THE NATIONAL UNIVERSITY OF IRELAND, CORK. COLAISTE NA hollscoile, CORCAIGH UNIVERSITY COLLEGE, CORK

COMP 401 Recitation 8. Observer Pattern

Chapter 7. Inheritance

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

CS 231 Data Structures and Algorithms, Fall 2016

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

EECS168 Exam 3 Review

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS1150 Principles of Computer Science Methods

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

Class, Variable, Constructor, Object, Method Questions

Graphical Applications

Programming II (CS300)

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

More Swing. CS180 Recitation 12/(04,05)/08

CS180 Review. Recitation Week 15

&KDSWHU(QKDQFLQJ&ODVVHV

Programming II (CS300)

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

CSC324 Principles of Programming Languages

ITI Introduction to Computing II

CMSC 132: Object-Oriented Programming II

DHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR (ODD SEM)

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

About This Lecture. Data Abstraction - Interfaces and Implementations. Outline. Object Concepts. Object Class, Protocol and State.

MIT AITI Swing Event Model Lecture 17

Non-instantiable Classes

Chapter 5. Feb 08, Chapter 5 1

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

PIC 20A GUI with swing

Chapter 4. Defining Classes I

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

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

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

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

ITI Introduction to Computing II

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

Java IDE Programming-I

Object Oriented Modeling

Window Interfaces Using Swing Objects

1B1b. Classes in Java Part III. Review. Static. Static (2) Example. Static (3) 1B1b Lecture Slides. Copyright 2004, Graham Roberts 1

Transcription:

CS180 Recitation More about Objects and Methods

Announcements Project3 issues Output did not match sample output. Make sure your code compiles. Otherwise it cannot be graded. Pay close attention to file names, have them match expected names. Thoroughly test before submitting.

Operation Overloading It is sometimes useful to have two methods of the same name but accept different parameters (very common in Math library) May not differ only by return type or variable name Constructors can be overloaded like other methods Valid: public String f(int x) { } public String f(double x) { } public String f(float f) { } Invalid: public int f(int x) { } public double f(int x) { }

Default Constructors If you dont write your own, Java will provide one for you. Write your own! Good programming practice Use mutators to modify class variables. Better code reuse. Single point of error.

Copy Constructors and Memory Management Copy constructors are useful in making a deep copy of an object Shallow copy: simple reference assignment with = (what problems may crop up?) Deep copy: copy over all values and objects (deep copy usually) explicitly into new memory

Memory Management public class A { } private int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a.get(); } public void set(int x) { this.x = x; } public int get() { return x; } A a1 = new A(); A a2 = new A(2); a1 = a2; a2.set(3); A a3 = new A(a1); a1.set(4); a1.get()? a2.get()? a3.get()?

static keyword Useful for methods which do not need to access class variables or methods Called using the class name Think: Math library Used for class variables which are shared over instances of the class class A { private static int a; public void seta(int x) { a = x; } A a1 = new A(); A a2 = new A(); a1.seta(5); System.out.println( a: +a2.geta()); } public int geta() { return a; }

static Memory Management public class A { private static int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a.get(); } A a1 = new A(); A a2 = new A(2); a1 = a2; A.set(3); A a3 = new A(a1); a1.set(4); a3.set(1); } public static void set(int x) { this.x = x; } public static int get() { return x; } a1.get()? a2.get()? a3.get()?

null keyword Indicates the non-existence of an object (think address 0x00000000) Can be used to represent any object type A null object can NOT call any methods This will result in a runtime error called a NullPointerException Get in the habit of checking against the null object!

null Memory Management public class A { } private int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a.get(); } public void set(int x) { this.x = x; } public int get() { return x; } A a1 = null; a1 = new A(); A a2 = new A(a1); A a3 = null; if (a3 == null) a3 = new A(); else a3.set(3); a1.set(4); a1.get()? a2.get()? a3.get()?

Pass-By-Value Parameters in Java are passed by value (as opposed to pass by reference). public class A { private int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a.get(); } A a1 = new A(); a1.set(4); A.reset(a1); a1.get()? } public static void reset(a a) { a = new A(); } public void set(int x) { this.x = x; } public int get() { return x; }

Wrapper Classes Recall that arguments of primitive type treated differently from those of a class type May need to treat primitive value as an object Java provides wrapper classes for each primitive type Methods provided to act on values Java library methods work with Objects, not Promitives. 12

Wrapper Classes Allow programmer to have an object that corresponds to value of primitive type Contain useful predefined constants and methods Wrapper classes have no default constructor Programmer must specify an initializing value when creating new object Wrapper classes have no set methods 13

Testing Methods To test a method use a driver program Example code in listing 6.13 Every method in a class should be tested Bottom-up testing Test code at end of sequence of method calls first Use a stub simplified version of a method for testing purposes Used in top-down testing 14

Adding Buttons Create object of type Jbutton Then add to content pane Possible to associate an action with a button View applet example, listing 6.21 class PreliminaryButtonDemo 15

Adding Buttons Applet Output If the user clicks either of these buttons, nothing happens. 16

Event-Driven Programming Applets use events and event handlers An event An object that represents some user action which elicits a response Example: clicking on button with mouse Listener objects are specified to receive the events Listener objects have event handler methods 17

Event-Driven Programming Figure 6.6 Event firing and an event listener This event object is the result of a button click. The event object goes from the button to the listener. This listener object performs some action, such as making text visible in the applet, when it receives the event object. 18

Programming Buttons When an event is "sent" to the listener object A method of the listener object is invoked The event object is given to the listener object method as the argument For each button Specify the listener object (register the listener) Methods to be invoked must be defined 19

Programming Buttons Figure 6.7 Buttons and an action listener 20

Programming Buttons Buttons fire events as objects of class ActionEvent Event objects handled by action listeners To make a class an action listener Add phrase implements ActionListener to heading of class definition Register the action listener by invoking addactionlistener Add definition named actionperformed to class 21

Programming Buttons To be an action listener, a class must have A method named actionperformed The method has a parameter of type ActionEvent This is the only method required by the ActionListener interface Syntax 22

Programming Example A Complete Applet with Buttons View applet code, listing 6.22 class ButtonDemo Note features Specification implements ActionListener Invocation of addactionlistener Method actionperformed 23

Programming Example Initial applet output 24

Programming Example Applet output after clicking Sunny 25

Programming Example Applet output after clicking Cloudy 26

Adding Icons An icon is a picture Usually small (but not necessarily) Often a.gif or.jpeg file Picture file stored in same folder as program Icon can be added to a label, button, or other component Class ImageIcon used to convert digital image to icon 27

Adding Icons View sample applet, listing 6.23 class IconDemo Note Creation of icon Attaching icon to label Sample screen output 28

Adding Icons Figure 6.8 A button containing an icon 29

Changing Visibility Components have a method named setvisible Changes component from visible to invisible (or the other way) An invisible component is considered not there Thus an invisible button would be inoperable 30

Programming Example An Example of Changing Visibility View sample applet, listing 6.24 class VisibliityDemo 31