Download presentation
Presentation is loading. Please wait.
1
Midterm 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
2
Exam Question Difficulty Obvious questions Questions requiring studying Challenging questions
3
Question Nature Essay Questions Understanding Code Writing/Modifying Code Example syntax
4
Essay Questions Definitions –Define class. Distinction –Distinguish Expression from Statement. Rationale –Why style principles? Comparison –Pros and Cons of using Named Constants & Literals.
5
Understanding Code Identify Errors and Style Principle Violations class aMoneyconverter { int Cents (dollars) { return dollars*100; } }
6
Understanding 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 m*100; } public int convert (int i) { return i*100; } `}`}
7
Understanding 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 m*100; } public int convert (int i) { return i*100; } `}
8
Understanding Code Evaluate, Parenthesize, and Type Expressions ExpressionTypeValue 5 + 3 + 4int8
9
Understanding Code Parenthesize Expressions ExpressionParenthesized 5 - 3 - 3(5-3) - 3
10
Writing 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 m*100; } public int convert (int i) { return i*100; } `}
11
Writing Code Write Expressions –i is 2 –Answer: i == 2 Method –cube(x) Class, Interface –Like assignments
12
Declarations 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.
13
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(“seW eight called”); return x*x bmi = weight/(height*height); Expression always evaluated as part of some statement.
14
Pure Vs Impure Functions ABMISpreadsheet Instance getWeight weight Body accesses calculateBMI(77,1.77) 24.57 getWeight() setWeight(77) 77 71... setWeight(71) ABMICalculator Instance calculateBMI weight Body accesses height
15
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 = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Scope heightInMetres Scope height Scope illegal Scope
16
public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; }... } Scope getHeight() Scope ObjectEditor ABMISpreadsheet
17
Identifier Scope Region of code where the identifier is visible. Arbitrary scopes not possible Least Privilege => Make scope as small as possible
18
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 = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Scope heightInMetres Scope
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.