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

Similar documents
Defining Classes and Methods

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Defining Classes and Methods

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Defining Classes and Methods

Chapter 4 Defining Classes I

5. Defining Classes and Methods

Defining Classes and Methods

EECS168 Exam 3 Review

5. Defining Classes and Methods

APCS Semester #1 Final Exam Practice Problems

Inheritance. Chapter 7. Chapter 7 1

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

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

Chapter 4. Defining Classes I

Defining Classes and Methods

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 6 Introduction to Defining Classes

CS180 Recitation. More about Objects and Methods

Chapter 4: Writing Classes

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

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

Methods and Data (Savitch, Chapter 5)

Introduction to Programming Using Java (98-388)

Exam 1 - (20 points)

Principles of Object Oriented Programming. Lecture 4

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CS111: PROGRAMMING LANGUAGE II

Chapter 6 Lab Classes and Objects

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

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

Chapter 5: Enhancing Classes

Java Object Oriented Design. CSC207 Fall 2014

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Midterms Save the Dates!

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Anatomy of a Class Encapsulation Anatomy of a Method

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Introduction. Introduction

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

CS1083 Week 2: Arrays, ArrayList

CS113: Lecture 4. Topics: Functions. Function Activation Records

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Java for Non Majors Spring 2018

Introduction to Programming (Java) 4/12

Variables and Primitive Types

Defining Classes and Methods

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

What will this print?

CS171:Introduction to Computer Science II

Computer Science II (20082) Week 1: Review and Inheritance

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Objects and Classes -- Introduction

CompuScholar, Inc. 9th - 12th grades

TeenCoder : Java Programming (ISBN )

Programming II (CS300)

Objects as a programming concept

9 Working with the Java Class Library

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Programming II (CS300)

(Not Quite) Minijava

Defining Classes and Methods

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

CS 335 Lecture 02 Java Programming

PASS4TEST IT 인증시험덤프전문사이트

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Lab5. Wooseok Kim

Overview CSE 142. Naming Revisited Declarations. Defining Parts of Objects

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Object-Oriented Programming in Java

Lecture 5: Methods CS2301

Computer Science II (20073) Week 1: Review and Inheritance

1B1b Classes in Java Part I

CS 1316 Exam 1 Summer 2009

Course Outline. Introduction to java

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

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

Programming: You will have 6 files all need to be located in the dir. named PA4:

CS 177 Recitation. Week 8 Methods

ITP 342 Mobile App Dev. Fundamentals

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

Chapter 7. Inheritance

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

CS111: PROGRAMMING LANGUAGE II

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Motivation was to facilitate development of systems software, especially OS development.

Transcription:

Recitation 02/02/07 Defining Classes and Methods 1

Miscellany Project 2 due last night Exam 1 (Ch 1-4) Thursday, Feb. 8, 8:30-9:30pm PHYS 112 Sample Exam posted Project 3 due Feb. 15 10:00pm check newsgroup! 2

Methods Two kinds of methods: Methods that return a single value nextint() Methods that perform some action other than returning a single value void methods println( m@ ) 3

void Method Definitions example public void writeouput() { System.out.println( Name: + name); System.out.println( Age: + age); } Such methods are called void methods. 4

Methods That Return a Value example public int fivefactorial(); { int factorial = 5*4*3*2*1; return factorial; } As before, the method definition consists of the method heading and the method body. The return type replaces void. 5

Accessor and Mutator Methods Appropriate access to an instance variable declared private is provided by an accessor method which is declared public. Typically, accessor methods begin with the word get, as in getname. Only provide when access is needed 6

Accessor and Mutator Methods, cont. Appropriate changes to an instance variable declared private are provided by an mutator method which is declared public. Typically, mutator methods begin with the word set, as in setname. Only provide when needed 7

Allocating Memory for a Reference and an Object A declaration such as SpeciesFourthTry s; creates a variable s that can hold a memory address (compile time) A statement such as s = new SpeciesFourthTry(); allocates memory for an object of type SpeciesFourthTry. (runtime) 8

== with Variables of a Class Type 9

== with Variables of a Class Type, cont. When used with variables of a class type, == tests if the variables are aliases of each other, not if they reference objects with identical data. To test for equality of objects in the intuitive sense, define and use an appropriate equals method. 10

== with Variables of a Class Type class Species 11

== with Variables of a Class Type class SpeciesEqualsDemo 12

Method equals The definition of method equals depends on the circumstances. In some cases, two objects may be equal when the values of only one particular instance variable match. In other cases, two objects may be equal only when the values of all instance variables match. Always name the method equals. 13

Boolean-Valued Methods A method that returns a value of type boolean is called a boolean-valued method. Method equals produces and returns a value of type boolean. The invocation of a boolean-valued method can be used as the condition of an if-else statement, a while statement, etc. 14

Boolean-Valued Methods, cont. The value returned by a boolean-valued method can be stored in a variable boolean areequal = s1.equals(s2); Any method that returns a boolean value can be used in the same way. 15

Class Parameters Recall When the assignment operator is used with objects of a class type, a memory address is copied, creating an alias. When the assignment operator is used with a primitive type, a copy of the primitive type is created. 16

Class Parameters, cont. When a parameter in a method invocation is a primitive type, the corresponding formal parameter is a copy of the primitive type. 17

Class Parameters, cont. When a parameter in a method invocation is a reference to a class type (i.e. a named object), the corresponding formal parameter is a copy of that reference (i.e. an identically valued reference to the same memory location). 18

Class Parameters, cont. Example if (s1.equals(s2)) public boolean equals (Species otherobject) causes otherobject to become an alias of s2, referring to the same memory location, which is equivalent to otherobject = s2; 19

Class Parameters, cont. Any changes made to the object named otherobject will be done to the object named s2, and vice versa, because they are the same object. If otherobject is a formal parameter of a method, the otherobject name exists only as long as the method is active. 20

Comparing Class Parameters and Primitive-Type Parameters A method cannot change the value of a variable of a primitive type passed into the method. A method can change the value(s) of the instance variable(s) of a class type passed into the method. 21

The Graphics Class An object of the class Graphics represents an area of the screen. The class Graphics also has methods that allow it do draw figures and text in the area of the screen it represents. 22

23

The Graphics Class, cont. A Graphics object has instance variables that specify an area of the screen In examples seen previously, the Graphics object represented the area corresponding to the inside of an applet. 24

The Graphics Class, cont. When an applet is run, a suitable Graphics object is created automatically and is used as an argument to the applet s paint method when the paint method is (automatically) invoked. The applet library code does all this for us. To add this library code to an applet definition, use extends JApplet 25

26

27

Programming Example 28

The init Method An init method can be defined when an applet is written. The method init (like the method paint) is called automatically when the applet is run. The paint method is used only for things like drawing. All other actions in an applet (adding labels, buttons, etc.) either occur or start in the init method. 29

Adding Labels to an Applet A label is another way to add text to an applet. 30

Adding Labels to an Applet, cont. class LabelDemo 31

The Content Pane Think of the content pane as inside of the applet. Container contentpane = getcontentpane(); When components are added to an applet, they are added to its content pane. Think: adding picture to picture frame The content pane is an object of type Container. 32

The Content Pane, cont. A named content pane can do things such as setting color contentpane.setbackground(color.white); or specifying how the components are arranged contentpane.setlayout (new FlowLayout()); 33

The Content Pane, cont. or adding labels JLabel label1 = new JLabel( Hello ); contentpane.add(label1); 34

Exam 1 Information Time/Location Thursday, Feb 8, 8:30-9:30pm, Physics 112 Material Covered Chapters 1-4 Format Multiple Choice Programming Old exam on course website 35

Exam 1 Information Topics Encapsulation, Polymorphism, information hiding Accessor/Mutator methods Objects, Classes public/private modifiers Java naming conventions Primitive types vs. class types 36

Exam 1 Information Topics, cont Defining classes and methods (including the main method) Void methods vs. methods that return a value Looping structures (while, do-while, for) If-else, switch Primitive types vs. class types (and memory representation) Scanner class for input, System.out for output 37

Exam 1 Information Topics, cont Arithmetic expressions Boolean variables String methods == vs. equals methods Basic graphics methods 38

Questions 39