Midterm Exam - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Midterm Exam

Description:

PDF/PS with index and corrections coming. Look at exercises ... Rationale. Why style principles? Comparison. Pros and Cons of using Named Constants & Literals. ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 19
Provided by: Prasun2
Learn more at: http://www.cs.unc.edu
Category:

less

Transcript and Presenter's Notes

Title: Midterm Exam


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

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 dollars100

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 m100
  • public int convert (int i)
  • return i100

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 m100
  • public int convert (int i)
  • return i100

8
Understanding Code
  • Evaluate, Parenthesize, and Type Expressions
  • Expression Type Value
  • 5 3 4 int 8

9
Understanding Code
  • Parenthesize Expressions
  • Expression Parenthesized
  • 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 m100
  • public int convert (int i)
  • return i100

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
  • 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.
14
Pure Vs Impure Functions
setWeight(77)
getWeight()
77
calculateBMI(77,1.77)
24.57
...
setWeight(71)
...
calculateBMI(77,1.77)
24.57
getWeight()
71
15
Scope
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)
16
Scope
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight,
bmi public double getHeight() return
height ...
17
Identifier Scope
  • Region of code where the identifier is visible.
  • Arbitrary scopes not possible
  • Least Privilege gt Make scope as small as possible

18
Scope
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
Write a Comment
User Comments (0)
About PowerShow.com