chapter 4 defining classes and methods

39
Chapter 4 1 Recitation 02/02/07 Defining Classes and Methods Chapter 4

Upload: others

Post on 03-Nov-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 4 Defining Classes and Methods

Chapter 4 1

Recitation 02/02/07Defining Classes and Methods

Chapter 4

Page 2: Chapter 4 Defining Classes and Methods

Chapter 4 2

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!

Page 3: Chapter 4 Defining Classes and Methods

Chapter 4 3

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@”)

Page 4: Chapter 4 Defining Classes and Methods

Chapter 4 4

void Method Definitions

• examplepublic void writeOuput(){

System.out.println(“Name: “ + name);System.out.println(“Age: “ + age);

}

• Such methods are called void methods.

Page 5: Chapter 4 Defining Classes and Methods

Chapter 4 5

Methods That Return a Value

• examplepublic 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.

Page 6: Chapter 4 Defining Classes and Methods

Chapter 4 6

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

Page 7: Chapter 4 Defining Classes and Methods

Chapter 4 7

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

Page 8: Chapter 4 Defining Classes and Methods

Chapter 4 8

Allocating Memory for a Reference and an Object

• A declaration such asSpeciesFourthTry s;

creates a variable s that can hold a memory address (compile time)

• A statement such ass = new SpeciesFourthTry();

allocates memory for an object of type SpeciesFourthTry. (runtime)

Page 9: Chapter 4 Defining Classes and Methods

Chapter 4 9

== with Variables of a Class Type

Page 10: Chapter 4 Defining Classes and Methods

Chapter 4 10

== 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.

Page 11: Chapter 4 Defining Classes and Methods

Chapter 4 11

== with Variables of a Class Type• class Species

Page 12: Chapter 4 Defining Classes and Methods

Chapter 4 12

== with Variables of a Class Type• class SpeciesEqualsDemo

Page 13: Chapter 4 Defining Classes and Methods

Chapter 4 13

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.

Page 14: Chapter 4 Defining Classes and Methods

Chapter 4 14

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.

Page 15: Chapter 4 Defining Classes and Methods

Chapter 4 15

Boolean-Valued Methods, cont.

• The value returned by a boolean-valued method can be stored in a variableboolean areEqual = s1.equals(s2);

• Any method that returns a boolean value can be used in the same way.

Page 16: Chapter 4 Defining Classes and Methods

Chapter 4 16

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.

Page 17: Chapter 4 Defining Classes and Methods

Chapter 4 17

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.

Page 18: Chapter 4 Defining Classes and Methods

Chapter 4 18

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).

Page 19: Chapter 4 Defining Classes and Methods

Chapter 4 19

Class Parameters, cont.

• Exampleif (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 tootherObject = s2;

Page 20: Chapter 4 Defining Classes and Methods

Chapter 4 20

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.

Page 21: Chapter 4 Defining Classes and Methods

Chapter 4 21

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.

Page 22: Chapter 4 Defining Classes and Methods

Chapter 4 22

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.

Page 23: Chapter 4 Defining Classes and Methods

Chapter 4 23

Page 24: Chapter 4 Defining Classes and Methods

Chapter 4 24

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.

Page 25: Chapter 4 Defining Classes and Methods

Chapter 4 25

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, useextends JApplet

Page 26: Chapter 4 Defining Classes and Methods

Chapter 4 26

Page 27: Chapter 4 Defining Classes and Methods

Chapter 4 27

Page 28: Chapter 4 Defining Classes and Methods

Chapter 4 28

Programming Example

Page 29: Chapter 4 Defining Classes and Methods

Chapter 4 29

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.

Page 30: Chapter 4 Defining Classes and Methods

Chapter 4 30

Adding Labels to an Applet

• A label is another way to add text to an applet.

Page 31: Chapter 4 Defining Classes and Methods

Chapter 4 31

Adding Labels to an Applet, cont.

• class LabelDemo

Page 32: Chapter 4 Defining Classes and Methods

Chapter 4 32

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.

Page 33: Chapter 4 Defining Classes and Methods

Chapter 4 33

The Content Pane, cont.

• A named content pane can do things such as setting colorcontentPane.setBackground(Color.WHITE);

or specifying how the components are arrangedcontentPane.setLayout

(new FlowLayout());

Page 34: Chapter 4 Defining Classes and Methods

Chapter 4 34

The Content Pane, cont.

or adding labelsJLabel label1 = new JLabel(“Hello”);

contentPane.add(label1);

Page 35: Chapter 4 Defining Classes and Methods

Chapter 4 35

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

Page 36: Chapter 4 Defining Classes and Methods

Chapter 4 36

Exam 1 Information

• Topics– Encapsulation, Polymorphism, information

hiding– Accessor/Mutator methods– Objects, Classes– public/private modifiers– Java naming conventions– Primitive types vs. class types

Page 37: Chapter 4 Defining Classes and Methods

Chapter 4 37

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

Page 38: Chapter 4 Defining Classes and Methods

Chapter 4 38

Exam 1 Information

• Topics, cont– Arithmetic expressions– Boolean variables– String methods– == vs. equals methods– Basic graphics methods

Page 39: Chapter 4 Defining Classes and Methods

Chapter 4 39

Questions