comp 110 state
DESCRIPTION
Comp 110 State. Instructor: Jason Carter. Outline. Instance Variables Procedures Properties Print Statements Println vs. Print Overloading. “What if” BMI Calculations With General Purpose Calculator. Must re-enter height each time!. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/1.jpg)
COMP 110STATE
Instructor: Jason Carter
![Page 2: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/2.jpg)
2
OUTLINE Instance Variables Procedures Properties Print Statements Println vs. Print Overloading
![Page 3: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/3.jpg)
3
“WHAT IF” BMI CALCULATIONS WITH GENERAL PURPOSE CALCULATOR
Must re-enter height each time!
![Page 4: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/4.jpg)
4
“WHAT IF” BMI CALCULATIONS WITH SPECIALIZED CALCULATOR
public double calculateMyBMI(double weight) { final double MY_HEIGHT = 1.77; return (new ABMICalculator).calculateBMI(weight, MY_HEIGHT);}
Must only enter the weightBut the height is hardwired! Must create a
separate class for each user!
General purpose solution that does not require re-entry of height each time?
![Page 5: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/5.jpg)
5
Caculate two BMIs using one instance of ABMISpreadsheet and changing only the weight
BMI SPREADSHEET
State: Data remembered by an object between
computations
![Page 6: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/6.jpg)
6
INSTANCE VARIABLESABMICalculator Instance
calculateBMI
Parameters
Body
accesses
ABMISpreadsheet Instance
getBMI
InstanceVariables
Body accesses
Belong to a single method
Local variable
Belong to all methods of an instance
Global variable
![Page 7: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/7.jpg)
7
STATE-LESS VS. STATE-FULL OBJECTS
Identical Instances ~ car radios with no presets
Different Instances ~ car radios with presets
![Page 8: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/8.jpg)
8
DECLARING INSTANCE VARIABLES
public class ABMISpreadsheet {double height;...double weight; ...public double getBMI() {
return weight/(height*height);}
…}
Missing Code
No Parameters
Instance Variables
![Page 9: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/9.jpg)
9
OBJECT ACCESS TO A CLASS
public class ABMISpreadsheet {double height;...double weight; ...public double getBMI() {
return weight/(height*height);}
…}
ObjectEditorOutside Access:
Variables should not be public
But ObjectEditor needs their values
![Page 10: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/10.jpg)
10
ACCESSING INSTANCE VARIABLES VIA PUBLIC METHODS
ABMISpreadsheet Instance
weight height
getWeight()
setWeight()
getHeight()
setHeight() getBMI()
ObjectEditor
calls
writes
weight
calls
reads
height
calls
reads writes
callsnew
weight
new height
reads
![Page 11: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/11.jpg)
11
CODING GETTER AND SETTER METHODS
ABMISpreadsheet Instance
weight
getWeight()
setWeight()
ObjectEditor
calls
writes
weight
calls
reads
new weight
![Page 12: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/12.jpg)
12
FUNCTION VS. PROCEDURE
procedure: deposit
function: withdraw
![Page 13: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/13.jpg)
13
CODING GETTER AND SETTER METHODS
ABMISpreadsheet Instance
weight
getWeight()
setWeight()
ObjectEditor
calls
writes
weight
calls
reads
new weight
public double getWeight(){ return weight;}
public void setWeight(double newWeight){ weight = newWeight;}
procedure – returns nothing
function
![Page 14: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/14.jpg)
14
GETTER AND SETTER METHODSpublic class ABMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double
newHeight) {height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double
newWeight) {weight = newWeight;
}public double getBMI() {
return weight/(height*height);}
}
procedure – returns nothing
function
![Page 15: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/15.jpg)
15
ASSIGNMENT STATEMENT
public void setHeight(double newHeight) { height = newHeight;}
variables memory
heightweight 0.0
0.0
setHeight(1.77)
newheight 0.01.77
1.77
=<variable> <expression>
Code that yields a value
height1.75*height
RHSLHS
![Page 16: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/16.jpg)
16
PROPERTIES
public class ABMISpreadsheet {double height;public double getHeight() {
return height;}public void setHeight(double newHeight)
{height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double
newWeight) {weight = newWeight;
}public double getBMI() {
return weight/(height*height);}
}
![Page 17: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/17.jpg)
17
READ-ONLY AND EDITABLE PROPERTIES
public class C{
}
public T getP() { ...}
public void setP(T newValue) { ...}
Typed, Named Unit of Exported Object State
Name P
Type T
Read-only
Editable
Getter methodSetter method
newPobtainP Violates Bean convention
Bean
Bean convention:For humans
and tools
![Page 18: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/18.jpg)
18
PROPERTIES
public class ABMISpreadsheet {double height;public double getHeight() {
return height;}public void setHeight(double newHeight)
{height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double
newWeight) {weight = newWeight;
}public double getBMI() {
return weight/(height*height);}
}
Height
Weight
BMI
![Page 19: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/19.jpg)
19
PROPERTIES CLASSIFICATIONpublic class ABMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight)
{height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double
newWeight) {weight = newWeight;
}public double getBMI() {
return weight/(height*height);}
}
Height
Weight
BMI
EditableIndependent
EditableIndependent
Read-onlyDependent
![Page 20: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/20.jpg)
20
PROPERTIES CLASSIFICATION
public class ABMICalculator { public double calculateBMI (double weight,
double height) {return weight/ (height * height);
}}
No Properties
![Page 21: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/21.jpg)
21
CALLING GETTER AND SETTER METHODS
public class ABMISpreadsheet { double height; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}
![Page 22: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/22.jpg)
22
TRACING METHOD CALLSpublic class ABMISpreadsheet { double height; public double getHeight() { System.out.println(“getHeight Called”); return height; } public void setHeight(double newHeight) { System.out.println(“setHeight Called”); height = newHeight; } double weight; public double getWeight() { System.out.println(“getWeight Called”); return weight; } public void setWeight(double newWeight) { System.out.println(“setWeight Called”); weight = newWeight; } public double getBMI() { System.out.println(“getBMI Called”); return weight/(height*height); }}
![Page 23: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/23.jpg)
23
ACTUAL TRACE
Extra getWeight() call made by the undo-redo mechanism in ObjectEditor
Load
Change weight
![Page 24: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/24.jpg)
24
PRINT LINE
System.out.println(“setWeight Called”);
Target Object Method Name Actual Parameters
Programmed Call
Interactive Call
![Page 25: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/25.jpg)
25
ACTUAL TRACE
![Page 26: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/26.jpg)
26
PRINTING WEIGHT
System.out.println(“setWeight called”);System.out.println(newWeight);
![Page 27: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/27.jpg)
27
PRINTING WEIGHT
System.out.print(“setWeight called: ”);System.out.println(newWeight);
![Page 28: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/28.jpg)
28
PRINTING WEIGHT
System.out.println(“setWeight called: ” + newWeight);
![Page 29: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/29.jpg)
29
PRINT VS. + (EDIT)
public void setWeight(double newWeight) {System.out.print (“old weight = “ +
weight);weight = newWeight;System.out.println(“ new weight = “ +
weight);}
Cannot use + instead of print()
public void setWeight(double newWeight) {System.out.println( “old weight = “ + weight + “ new
weight = “ + newWeight );weight = newWeight;
}
![Page 30: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/30.jpg)
30
PRINT VS. +
public void setWeight(double newWeight) {System.out.print (“old weight = “ +
weight);weight = newVal;System.out.println(“ new weight = “ +
weight);}
Cannot use + instead of print()
public void setWeight(double newWeight) {System.out.println( “old weight = “ + weight +
“ new weight = “ + newWeight);weight = newVal;
}
We will later see examples when you cannot use +
![Page 31: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/31.jpg)
31
RECAPPING THE STORY: CLASS DECLARATIONClass
ABMISpreadsheet Class XyZ must be stored in XyZ.java
File Name:
<class header>
<class body>
public class ABMISpreadsheetSpelling and use of upper and
lower case letters is important!
Class Header Info:Is this a class?Is it accessible to other
classes?What is the name of the
class?
![Page 32: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/32.jpg)
32
RECAPPING THE STORY: CLASS BODYClass
ABMISpreadsheet<class header>
<class body>
public class ABMISpreadsheet
Class Body Info:How can the class be used?
(i.e. methods)Instance variables
Curly braces after class header declare start of class
body
Curly braces at the very end declare end of class body
{
double height; ... double weight; ... public double setBMI() { … }
}
![Page 33: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/33.jpg)
33
RECAPPING THE STORY: DECLARATIONSdouble height;public double getHeight() { return height;}public void setHeight(double newHeight) { height = newHeight;}double weight;public double getWeight() { return weight;}public void setWeight(double newWeight) { weight = newWeight;}public double getBMI() { return weight/(height*height);}
<class body> Declarations:Declaring instance variables
Declaring methodsDeclaration order:Does not matter:- e.g. declare a method before or after any instance variables it accesses- e.g. can declare height and weight instance variables at the bottom of the class- e.g. declare methods in any orderDeclaration terminator:
Instance variables: “;”Methods: end of method body
![Page 34: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/34.jpg)
34
RECAPPING THE STORY: VARIABLESdouble height;public double getHeight() { return height;}public void setHeight(double newHeight) { height = newHeight;}double weight;public double getWeight() { return weight;}public void setWeight(double newWeight) { weight = newWeight;}public double getBMI() { return weight/(height*height);}
<class body>
Variable declaration:Must have variable type and
variable name
Kinds of variables:Formal parameter
Internal method variableNamed constantInstance variable
Usefulness of each kind of variable?
![Page 35: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/35.jpg)
35
RECAPPING THE STORY: METHODSdouble height;public double getHeight() { return height;}public void setHeight(double newHeight) { height = newHeight;}double weight;public double getWeight() { return weight;}public void setWeight(double newWeight) { weight = newWeight;}public double getBMI() { return weight/(height*height);}
<class body>
Method header info:Is it accessible to other
objects?Return type
NameParameter list
Kinds of methods:Functions
Procedures
Parameter list:Enclosed in brackets
Comma separated list of formal parameters
![Page 36: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/36.jpg)
36
RECAPPING THE STORY: METHOD BODY
double weight;…public double getWeight() { return weight;}…public void setWeight(double newWeight) { System.out.print ( “old weight = “ + weight); weight = newVal; System.out.println( “ new weight = “ + weight);}
<class body> Method body:Sequence of one or more “;”
terminated statements
Curly braces after class header declare start of class
body
Curly braces at the very end declare end of class body
![Page 37: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/37.jpg)
37
RECAPPING THE STORY: STATEMENTS
double weight;…public double getWeight() { return weight;}…public void setWeight(double newWeight) { System.out.print ( “old weight = “ + weight); weight = newVal; System.out.println( “ new weight = “ + weight);}
<class body> Statements order:Order matters:
Statements executed in sequence
Kinds of statements :Return statements
Assignment statementsPrint statements
Statements is a:
An instruction that the computer must execute
![Page 38: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/38.jpg)
38
RECAPPING THE STORY: STATEMENTS
double weight;…public double getWeight() { return weight;}…public void setWeight(double newWeight) { System.out.print ( “old weight = “ + weight); weight = newVal; System.out.println( “ new weight = “ + weight);}
<class body> Return statement:return <expression>;
Assignment statement :
<variable> = <expression>;
Print statement :System.out.println( <expres
sion>);
![Page 39: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/39.jpg)
39
RECAPPING THE STORY: EXPRESSIONS VS. STATEMENTS
Expression: Piece of code yielding value 5 “setWeight Called” newHeight x*x weight/
(height*height)
Statement: computer instruction executed autonomously System.out.println(“
setWeight called”); return x*x bmi =
weight/(height*height);
Expression always evaluated as part of some statement.
![Page 40: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/40.jpg)
40
RECAPPING THE STORY: EVALUATING EXPRESSIONS
X = (a+b) + (c*(d+e)/(f+g))Mathematical expression
Look for a closing brace and evaluate the sub-expression
(a+b) + (f+g)/(d+e)c *
![Page 41: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/41.jpg)
41calculateBMI( , ) toKgs
(new ABMICalculator )
RECAPPING THE STORY: EVALUATING EXPRESSIONS
public class APoundInchBMICalculator {
public double calculateBMI( double weightInLbs, double heightInInches) {
return (new ABMICalculator()).calculateBMI( toKgs(weightInLbs), toMetres(heightInInches)); }
public double toMetres(double heightInInches) { … } public double toKgs(double weightInLbs) { … }}
APoundInchBMICalculator
( )
(weightInLbs) toMetres (heightInInches
)
![Page 42: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/42.jpg)
42
RECAPPING THE STORY: INTERNAL VS. EXTERNAL METHOD CALLS
public class APoundInchBMICalculator {
public double calculateBMI( double weightInLbs, double heightInInches) {
return (new ABMICalculator()).calculateBMI( toKgs(weightInLbs), toMetres(heightInInches)); }
public double toMetres(double heightInInches) { … } public double toKgs(double weightInLbs) { … }}
APoundInchBMICalculator
ExternalCaller and callee
methods are in different objectsMust specify target object
InternalCaller and callee methods
are in the same objectTarget object is implicit
(this)
Actual parameters:
<expression>
![Page 43: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/43.jpg)
43
OVERLOADING
System.out.println(“setWeight called”);System.out.println(newWeight);
public void println(String val) {…}public void println(double val) {…}
Operation Definitions
Context of actual parameters
Two different operations with the same name
Stringdoubl
e
Two different words with the
same nameLook at that plane fly.
The fly is bothering me.
![Page 44: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/44.jpg)
44
AMBIGUOUS CONTEXT
System.out.println(“setWeight called”);System.out.println(newWeight);
public void println(String val) {…}public void println(String val) {…}
Time flies like an arrow.
Fruit flies like an orange.
Operation Definitions
Java cannot use context to disambiguate
Defining two versions of a method ? Why is overloading useful?
![Page 45: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/45.jpg)
45
PRINTING MULTIPLE VALUES ON ONE LINE
System.out.println(“setWeight called”);
System.out.println(newWeight);
System.out.println(“setWeight called” + newWeight);5 + 6
Operator Overloading
![Page 46: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/46.jpg)
46
VARIABLE DECLARATION ERRORSpublic class ABMISpreadsheet { double height; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}
![Page 47: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/47.jpg)
47
VARIABLE DECLARATION ERRORSpublic class ABMISpreadsheet { public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}
Undefined variable
![Page 48: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/48.jpg)
48
VARIABLE DECLARATION ERRORSpublic class ABMISpreadsheet { double weight; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}
Multiply defined variable
![Page 49: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/49.jpg)
49
PURE VS. IMPURE FUNCTIONSABMICalculator Instance
weight
Body
accesses
ABMISpreadsheet Instance
getWeight
weight
Body accesses
height
calculateBMI
calculateBMI(77,1.77)
calculateBMI(77,1.77)24.57
24.57...
getWeight()
getWeight()
setWeight(77)77
71
...setWeight(71)
![Page 50: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/50.jpg)
50
STATE-LESS OBJECTEDITOR
![Page 51: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/51.jpg)
51
STATE-FULL OBJECTEDITOR
![Page 52: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/52.jpg)
52
STATE-FULL OBJECTEDITOR
![Page 53: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/53.jpg)
53
STATE-FULL OBJECTEDITOR
![Page 54: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/54.jpg)
54
GETTING ALL CLASSES IN CURRENT DIRECTORY
![Page 55: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/55.jpg)
55
INCONSISTENT BMI STATE
(new ABMISpreadsheet());
![Page 56: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/56.jpg)
56
FIXING INCONSISTENT BMI STATEABMISpreadsheet aBMISpreadsheet = new ABMISpreadsheet();aBMISpreadsheet.setHeight(1.77);aBMISpreadsheet.setWeight(75.0);
![Page 57: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/57.jpg)
57
CONSISTENT BMI STATEABMISpreadsheet aBMISpreadsheet = new ABMISpreadsheet( 1.77, 75.0);
![Page 58: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/58.jpg)
58
CONSTRUCTORpublic class ABMISpreadsheet { double height, weight; public ABMISpreadsheet( double theInitialHeight, double theInitialWeight) { setHeight(theInitialHeight); setWeight(theInitialWeight); }
public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}
Calling setter methods instead
of modifying variable directly
makes debugging easier
![Page 59: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/59.jpg)
59
CONSTRUCTORpublic class ABMISpreadsheet { double height, weight; public ABMISpreadsheet( double theInitialHeight, double theInitialWeight) { setHeight(theInitialHeight); setWeight(theInitialWeight); }
public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}
Constructor name must be the name
of the class
Constructor name is also the type of object returned
![Page 60: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/60.jpg)
60
INSTANTIATING A CLASS WITH A CONSTRUCTOR
![Page 61: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/61.jpg)
61
SETTING THE CONSTRUCTOR PARAMETERS
![Page 62: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/62.jpg)
62
INITIALIZED OBJECT STATE
![Page 63: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/63.jpg)
63
EVERY CLASS HAS A CONSTRUCTOR
public class ABMISpreadsheet { double height, weight;
public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}
![Page 64: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/64.jpg)
64
EQUIVALENT CLASS CREATED BY JAVA
public class ABMISpreadsheet { double height, weight;
public ABMISpreadsheet() { }
public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}
If Programmer Specifies no
Constructor, Java inserts a null constructor
Inserted In Object Code not in Source Code
![Page 65: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/65.jpg)
65
OVERLOADING THE CONSTRUCTOR
public class ABMISpreadsheet { double height, weight;
public ABMISpreadsheet() { }
public ABMISpreadsheet( double theInitialHeight, double theInitialWeight) { setHeight(theInitialHeight); setWeight(theInitialWeight); }
…}
Null constructor has been
manually added
![Page 66: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/66.jpg)
66
INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS
![Page 67: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/67.jpg)
67
INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS
![Page 68: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/68.jpg)
68
INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS
![Page 69: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/69.jpg)
69
INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS
![Page 70: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/70.jpg)
70
INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS
![Page 71: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/71.jpg)
71
INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS
![Page 72: Comp 110 State](https://reader036.vdocument.in/reader036/viewer/2022062520/56816356550346895dd40180/html5/thumbnails/72.jpg)
72
INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS