is majoring in

Download Report

Transcript is majoring in

Ch 7 Designing Java Classes
& Class structure
Classes comprise fields and methods
Fields – have data values define/describe an instance
Constructors –values are assigned to fields when an
instance/object is created (special kind of method)
Methods – what does an instance know? What can an
instance do?
– getters
– setters
– equals
– toString
– … other
ACS-1903
1
UML Diagram of a Class
Class name
A class has
fields
fields and
methods (including constructors)
methods
ACS-1903
2
e.g. Math class
Math
Math has two static fields
+E
+PI
Math has a private constructor
You cannot instantiate a Math object
Math has many static methods
-Math()
+abs(double a)
+abs(float a)
+ abs(int a)
…
+ max(double a, double b)
+ max(int a, int b)
…
To use π you write
Math.PI
To use the static method max you write
Math.max(n1, n2)
ACS-1903
3
e.g. Random class
must instantiate an object to get a random sequence
Random gen = new Random(84854);
instance fields
seed
multiplier = 0x5DEECE66DL
Random
-seed
-multiplier
+Random()
+Random(long seed)
+nextBoolean()
+nextInt()
…
constructors
instance methods
gen.nextBoolean()
gen.nextInt()
ACS-1903
4
Fields
Fields may be primitive variables
Or, they may be of some other type (e.g. String, …)
May be public or private
public - anyone can use it
private – limited access
ACS-1903
5
Methods
•Methods are either:
– value-returning
must have a return statement
e.g. getters  naming convention is …
– void
no return statement
e.g. setters  naming convention is …
ACS-1903
6
Methods
•public vs private
public - anyone can use it
private - special cases
Math constructor is private – you cannot instantiate
a Math object … try to do it
nextId() in Student is private
It has a special purpose … used to generate the next id.
It is only called from within the Student class
ACS-1903
7
equals Method
equals(…)
– Value-returning
– Returns a boolean
– Usually an equals method is designed for a class. Designer must
determine the condition when two objects are considered
equal.
– E.g. String class has an equals method
string1.equals(string2)
ACS-1903
8
toString Method
toString()
– Value-returning
– Returns a string
– A method automatically called when an object is displayed
E.g. System.out.println(myObject);
– The designer of a class determines what it returns
– E.g. ArrayList has a toString() method … result is of the form:
[
object1, object2, … objectn ]
ACS-1903
9
Example - Student
Consider the student class 
ACS-1903
10
Class Diagram for Student
show 1, 2, or 3
compartments/ info
as needed
-id
-firstName
-lastName
-gender
-active
+ means there is
public access to the
method
getters
- means there is no
public access to the
field or method
setters
ACS-1903
11
Java code for Student - fields
instance vs class
e.g. consider Student class
Which fields are class?
Which fields are instance?
ACS-1903
12
Java code for Student - fields
private vs public
private:
only directly accessible from within the class/object,
and from outside the class via getters/setters
public : accessible from anywhere
A design principle is to make fields private but give public access to the getters
and setters
ACS-1903
13
Java code for Student - constructors
The no-arg constructor
Constructor with 4 parameters
ACS-1903
14
Java code for Student - getters
Notice
Getters (accessors) for most private fields
Naming convention:
Start with “get” followed by the
field name but this starts with a
capital letter
ACS-1903
15
Java code for Student - setters
Notice
Setters (mutators) for most private fields
ACS-1903
Naming convention:
Start with “set” followed by the
field name but this starts with a
capital letter
16
Java code for Student – other methods
Private method – used to
control the id assigned to a new
student object
toString
Executes when a student is printed
equals
Tests two student objects to see
if they are ‘equal’
ACS-1903
17
Java Classes
Class is a template for objects
How are these shown in UML? (Figures 7.2, 7.4, 7.5
ACS-1903
18
Objects
Objects:
instantiated/created via new – lots of examples
also called an instance – so we can speak of instance
fields/methods
How are these shown in UML? (Figures 7.3, 7.4)
ACS-1903
19
Associations
Classes will have relationships with other classes. When designing you must
decide whether to implement an association, and how to implement it.
Figure 7.1
A student is majoring in a subject
A student will have at most one major
The student class has a private field with getter/setter
Implementation in Student:
Private field
getter
setter
ACS-1903
20
Associations
Classes will have relationships with other classes. When designing you must
decide whether to implement an association, and how to implement it.
Figure 7.1
A subject has majors (students)
A subject may have many majors
The Subject class has an ArrayList with getter/setter & addMajor(…)
Implementation in Subject:
ACS-1903
21
Java Classes – making a connection between objects
Associations:
Figure 7.1
Consider SamDeclaresMathMajor.java
1.
2.
3.
4.
Instantiate a subject … math
Instantiate a student … sam
Set Sam’s major to be math
Add Sam to the list of math majors
ACS-1903
22
Java Classes- reusing code
Methods are used for two purposes
1. To make a program more readable through decomposition
2. To reuse code instead of duplicating code
Consider that the code in SamDeclaresMathMajor.java could be replicated
for every student declaring a major
jill.setMajor (math) ;
math.addMajor (jill) ;
sam.setMajor (math) ;
acs.addMajor (sam) ;
bob.setMajor (math) ;
acs.addMajor (bob) ;
For jill
For sam
For bob
ACS-1903
We can replace this
kind of code by writing
a method that sets a
student’s major and
adds the student to a
subject 
23
Java Classes- reusing code
Methods are used for two purposes
1. To make a program more readable through decomposition
2. To reuse code instead of duplicating code
Consider that the code in SamDeclaresMathMajor.java could be replicated
for every student declaring a major
Three calls to the
method below
A method to handle
declaring a major
ACS-1903
24
Java Classes - Parameters and arguments
Arguments passed to a method
Parameters defined
for a method
Arguments are copied into the parameters on entry, but
there is no copying on return.
Arguments must match parameters by type.
ACS-1903
25
Java Classes - Parameters and arguments
Parameter Lists / Arguments
A parameter list defines the type of data that will be passed in to a
method
Arguments appear in the call statement.
Arguments are copied into the parameters on entry, but there is no
copying on return.
But for objects its possible to modify them in the called method
See ObjectModifiedByCalledMethod.java
ACS-1903
26