Title: Midterm Exam
1Midterm Exam
- 75 points
- 1 min per point
- Allocate time proportionate to points
- Closed book
- Chapters 1-5 (except char)
- PDF/PS with index and corrections coming
- Look at exercises
2Exam Question Difficulty
- Obvious questions
- Questions requiring studying
- Challenging questions
3Question Nature
- Essay Questions
- Understanding Code
- Writing/Modifying Code
4Essay Questions
- Definitions
- Define class.
- Distinction
- Distinguish Expression from Statement.
- Rationale
- Why style principles?
- Comparison
- Pros and Cons of using Named Constants Literals.
5Understanding Code
- Identify Errors and Style Principle Violations
- class aMoneyconverter
- int Cents (dollars)
- return dollars100
-
-
6Understanding Code
- Classify Components
- properties, variables, signatures .
- public class C ()
- public int m 100
- public int getD()
- return m
-
- public void setD(int i)
- m i
-
- public int getC()
- return m100
-
- public int convert (int i)
- return i100
-
-
7Understanding Code
- Evaluate Method Calls
- public class C ()
- public int m 100
- public int getD()
- return m
-
- public void setD(int i)
- m i
-
- public int getC()
- return m100
-
- public int convert (int i)
- return i100
-
-
8Understanding Code
- Evaluate, Parenthesize, and Type Expressions
- Expression Type Value
- 5 3 4 int 8
9Understanding Code
- Parenthesize Expressions
- Expression Parenthesized
- 5 - 3 - 3 (5-3) - 3
10Writing Code
- Improve Code
- public class C ()
- public int m 100
- public int getD()
- return m
-
- public void setD(int i)
- m i
-
- public int getC()
- return m100
-
- public int convert (int i)
- return i100
-
-
11Writing Code
- Write Expressions
- i is 2
- Answer i 2
- Method
- cube(x)
- Class, Interface
- Like assignments
12Declarations Vs Statement Order
- Order of variable and method declarations in a
class does not matter in Java. - Order of statements in a method body matters.
- Statements executed sequentially.
13Expressions Vs Statements
- Expression Piece of code yielding value
- 5
- setWeight called
- newHeight
- xx
- weight/(heightheight)
- Statement computer instruction executed
autonomously - System.out.println(seWeight called)
- return xx
- bmi weight/(heightheight)
Expression always evaluated as part of some
statement.
14Pure Vs Impure Functions
setWeight(77)
getWeight()
77
calculateBMI(77,1.77)
24.57
...
setWeight(71)
...
calculateBMI(77,1.77)
24.57
getWeight()
71
15Scope
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight,
bmi public double getHeight() return
height public void setHeight(double
newHeight) height newHeight bmi
calculateBMI() public double getWeight()
return weight public void
setWeight(double newWeight) weight
newWeight bmi calculateBMI() public
double getBMI() return bmi double
calculateBMI() double heightInMetres
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) / (heightInMetresheightInMetre
s)
16Scope
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight,
bmi public double getHeight() return
height ...
17Identifier Scope
- Region of code where the identifier is visible.
- Arbitrary scopes not possible
- Least Privilege gt Make scope as small as possible
18Scope
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight,
bmi public void setHeight(double newHeight)
double height heightInMetres bmi
calculateBMI() ... final double LBS_IN_KG
2.2 final double CMS_IN_INCH 2.54 double
calculateBMI() double heightInMetres
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) / (heightInMetresheightInMetre
s)
heightInMetres Scope