Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the...
-
date post
20-Dec-2015 -
Category
Documents
-
view
218 -
download
2
Transcript of Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the...
![Page 1: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/1.jpg)
Chapter 3
Classes and Methods
![Page 2: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/2.jpg)
2
Knowledge Goals
• Appreciate the difference between a class in the abstract sense and a class as a Java construct
• Know what attributes are• Know what responsibilities are• Know how assignment of an object reference
differs from assignment of a standard type
![Page 3: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/3.jpg)
3
Knowledge Goals
• Appreciate how aliases can lead to errors• Know what garbage collection is• Understand the distinction between instance
methods and class methods• Know what a constructor does• Appreciate the difference between void and
value-returning methods• Understand what immutability is
![Page 4: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/4.jpg)
4
Skill Goals
• Determine the attributes and responsibilities of a class
• Write the heading for a new class• Write the class declaration for a new class• Write an instance method• Write a class method• Write a constructor
![Page 5: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/5.jpg)
5
Skill Goals
• Write a helper method• Write a value-returning method• Assemble class declarations into a working
class
![Page 6: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/6.jpg)
6
New Classes of Objects
A new class is just like an applicationexcept that it doesn't have a main method
![Page 7: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/7.jpg)
7
New Classes of Objects
Can youname some
attributesfor the
cat class?
![Page 8: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/8.jpg)
8
New Classes of Objects
Instance values
A value that is associated with a specific object
Class value
A value that is associated with a class and is the same for every object of the class
Attributes
The values defined by a class that are used to represent its objects; the combination of instance and class values
Were any cat attributes class values?
![Page 9: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/9.jpg)
9
New Classes of Objects
See thedifferencebetween
classand
instancevalues?
![Page 10: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/10.jpg)
10
New Classes of Objects
A classand
threeinstantiated
objects
![Page 11: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/11.jpg)
11
New Classes of Objects
With each new class comes responsibilities…
Responsibilities
The operations that are provided by a class of objects
Class responsibility
An operation performed with respect to a class of objects
Instance responsibility
An operation performed with respect to a specific object
![Page 12: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/12.jpg)
12
New Classes of Objects
Method
A Java subprogram
Class method
A method that implements a class responsibility
Instance method
A method that implements an instance responsibility
![Page 13: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/13.jpg)
13
New Classes of Objects
Constructor
An operation (responsibility) that creates a new instance of a class
Constructor (in Java)
A method with the same name as the class, which is neither a void or a value-returning method
How many kinds of attributes and responsibilities are there?
![Page 14: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/14.jpg)
14
New Classes and Objects
Every classmust have
aconstructor;if you don'tdefine one,
Javasupplies
one
![Page 15: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/15.jpg)
15
Overview of Java Data Types
![Page 16: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/16.jpg)
16
Overview of Java Data Types
Primitive values require a fixed number of bytes;object values can be any size, so…
![Page 17: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/17.jpg)
17
Overview of Java Data Types
A variable of a primitive type contains the value of the variable
A variable of an object type contains the address of where the value (object) begins
![Page 18: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/18.jpg)
18
Overview of Java Data Types
![Page 19: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/19.jpg)
19
Overview of Java Data Types
letter
char letter;
String title;
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title; ExampleWalkthrough
![Page 20: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/20.jpg)
20
Overview of Java Data Types
letter
title
char letter;
String title;
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title; ExampleWalkthrough
![Page 21: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/21.jpg)
21
Overview of Java Data Types
letter
title
book
char letter;
String title;
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title; ExampleWalkthrough
![Page 22: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/22.jpg)
22
Overview of Java Data Types
letter
title
book
char letter;
String title;
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title;
‘J’
ExampleWalkthrough
![Page 23: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/23.jpg)
23
Overview of Java Data Types
letter
title
book
char letter;
String title;
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title;
2003
“Problem Solving”
Memory Location 2003
‘J’
ExampleWalkthrough
![Page 24: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/24.jpg)
24
Overview of Java Data Types
letter
title
book
char letter;
String title;
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title;
2003
“Problem Solving”
Memory Location 2003
2003
‘J’
ExampleWalkthrough
![Page 25: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/25.jpg)
25
Overview of Java Data Types
letter
title
book
char letter;
String title;
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title;
2003
“Problem Solving”
Memory Location 2003
2003
‘J’
WalkthroughComplete
![Page 26: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/26.jpg)
26
Overview of Java Data Types
AliasWhen
multiplevariables
referto thesame
object, acting assynonyms
![Page 27: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/27.jpg)
27
Overview of Java Data Types
Address 000011010101 is unreachable
![Page 28: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/28.jpg)
28
Recycling Discarded ObjectsGarbage
Objects that are unreachable
Garbage collection
Recycling of memory when objects are no longer needed
Explicit memory management
Recycling of object memory that must be manually coded
Memory leak
Failure to recycle memory when explicit memory management is being used
Javahas
automaticgarbagecollection
![Page 29: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/29.jpg)
29
Responsibilities as Methods
Heading
Body
Parameter list contains information the method must have in order to execute; this information is given at the call
![Page 30: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/30.jpg)
30
Responsibilities as Methods
Argumentssubstituted
forparameters
at time of call
![Page 31: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/31.jpg)
31
Responsibilities as Methods
![Page 32: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/32.jpg)
32
Responsibilities as Methods
Class variable
Instance variables
How does Java distinguish them?
![Page 33: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/33.jpg)
33
Responsibilities as methods
Reviewvoid methods
Called as a statement
value-returning methods
Called in an expression; return a value
![Page 34: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/34.jpg)
34
Responsibilities as methods
Constructor
An operation (method) that creates a new instance of a class
Same name as class Default constructor (no parameters) Parameterized constructor (with parameters) A class can have more than one constructor Is almost always public (Why?)
![Page 35: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/35.jpg)
35
Responsibilities as methods
ObserverAn operation that returns information from an object without changing the content of the objectImmutabilityThe property of a class that its instances cannot be modified once they are createdConstructor-Observer classesClasses that have only constructors and observers and are therefore immutable (all we've seen so far)
![Page 36: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/36.jpg)
36
Cat Class
public class Cat
{
String name;
String color;
float weight;
// constructors
public Cat()
{
name = " ";
color = " ";
weight = 0.0;
}
![Page 37: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/37.jpg)
37
Cat Class
public Cat(String newName, String newColor, float newWeight)
{
name = newName;
weight = newWeight;
colr = newColor;
}
public String getName()
{
return name;
}
Methods that return the value of a variableare often "get" + identifier. (Why?)
![Page 38: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/38.jpg)
38
Cat Class
public float getWeight()
{
return weight;
}
public String getColor()
{
return color;
}
}
Is class Cat immutable?
![Page 39: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/39.jpg)
39
Driver for Cat Classpublic class CatDriver{ public static void main (String[] args) { Cat myCat = new Cat("Meow", "ginger", 10.5); Cat cat = new Cat(); System.out.print("myCat "); System.out.println(myCat.getName() + " " + myCat.getColor() + " " + myCat.getWeight()); System.out.print("cat "); System.out.println(cat.getName() + " " + cat.getColor() + " " + cat.getWeight()); }}
![Page 40: Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d4c5503460f94a29ad0/html5/thumbnails/40.jpg)
40
Extras
I amcalled the
firstprogrammer
Can youremembermy name?
Myfather'sname?