Unformatted text preview:

Midterm ExamExam Question DifficultyQuestion NatureEssay QuestionsUnderstanding CodeSlide 6Slide 7Slide 8Slide 9Writing CodeSlide 11Declarations Vs Statement OrderExpressions Vs StatementsPure Vs Impure FunctionsScopeSlide 16Identifier ScopeSlide 18Midterm 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 exercisesExam Question Difficulty• Obvious questions• Questions requiring studying• Challenging questionsQuestion Nature•Essay Questions•Understanding Code•Writing/Modifying CodeExample syntaxEssay Questions•Definitions–Define class.•Distinction–Distinguish Expression from Statement.•Rationale–Why style principles?•Comparison–Pros and Cons of using Named Constants & Literals.Understanding Code•Identify Errors and Style Principle Violationsclass aMoneyconverter { int Cents (dollars) { return dollars*100; }}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;}` }Understanding Code•Evaluate Method Callspublic 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;}` }Understanding Code•Evaluate, Parenthesize, and Type ExpressionsExpression Type Value5 + 3 + 4 int 8Understanding Code•Parenthesize ExpressionsExpression Pa renthesized5 - 3 - 3 (5-3) - 3Writing Code•Improve Codepublic 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;}` }Writing Code•Write Expressions–i is 2–Answer: i == 2•Method–cube(x)•Class, Interface–Like assignmentsDeclarations 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.Expressions Vs StatementsExpression: Piece of code yielding value•5•“setWeight called”•newHeight•x*x•weight/(height*height)Statement: computer instruction executed autonomously•System.out.println(“seWeight called”);•return x*x•bmi = weight/(height*height);Expression always evaluated as part of some statement.Pure Vs Impure FunctionsABMISpreadsheet InstancegetWeightweight Body accessescalculateBMI(77,1.77)calculateBMI(77,1.77) 24.5724.57getWeight()getWeight()setWeight(77)7771......setWeight(71)ABMICalculator InstancecalculateBMIweight Body accessesheightpublic 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);}}ScopeheightInMetres Scopeheight Scopeillegal Scopepublic class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;public double getHeight() {return height;}...}ScopegetHeight() ScopeObjectEditorABMISpreadsheetIdentifier Scope•Region of code where the identifier is visible.•Arbitrary scopes not possible•Least Privilege => Make scope as small as possiblepublic cla ss AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;public void setHeight(double newHeight) {double height = heightInMetres;bmi = calculateBMI();}...final dou ble LBS_IN_KG = 2.2;final dou ble CMS_IN_INCH = 2.54;double calculateBMI() { dou ble heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres) ;}}ScopeheightInMetres


View Full Document

UNC-Chapel Hill COMP 14 - COMP 14 Midterm Ecam

Download COMP 14 Midterm Ecam
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view COMP 14 Midterm Ecam and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view COMP 14 Midterm Ecam 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?