Download presentation
Presentation is loading. Please wait.
2
Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm
3
Fun For Today: n Review for the Coming Exam (gasp!) n Concepts in Assignment 4 n A little bit of “Operator Precedence” stuff
4
READ! READ MORE! KEEP READING! DON’T STOP! Even you, that person in the back that doesn’t think they have to. First Step Towards a Better Exam Grade Yeah, you. Exam Review
5
Figure out what you do NOT know, and ask someone. Anyone. Before the Professor asks you. Second Step Towards a Better Exam Grade Exam Review ("Never, ever, enter a battle of wits half-armed." - Cavanaugh)
6
Exam Review Exam Specs: 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
7
Exam Review class aMoneyconverter { int Cents (dollars) { return dollars*100; } No parameter type It’s not Capitalized What else?
8
Exam Review 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; } What are the components?
9
Exam Review Declaration vs. Statement Order Inside a class, it does not matter in what order variables and methods are declared. Order of Statements Matter.
10
Exam Review Expressions Vs. Statments Expression: Piece of code yielding value Statement: computer instruction executed autonomously Expressions: 5 “setWeight called” newHeight x*x weight/(height*height) Statements: System.out.println(“setWeight called”); return x*x bmi = weight/(height*height);
11
Exam Review Pure vs. Impure Functions Pure vs. Impure Functions (Yes, again) Pure: “I will always return to you the same value calculated from my parameters everytime you call me, until the end of time.” Impure: “Okay, I use instance variables, so you’re never going to be really sure what my final answer will be.”
12
Exam Review Pure vs. Impure Functions Pure vs. Impure Functions (Yes, again) Impure: “Okay, I use instance variables, so you’re never going to be really sure what my final answer will be.” public int heyImPure(int var1, int var2 ) { return var1*var2; } public int weight; public int imImpure( ) { return weight*weight; } Dependent on Object State! Pure: “I will always return to you the same value calculated from my parameters everytime you call me, until the end of time.”
13
Exam Review 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); } height Scope heightInMetres Scope illegal Scope
14
Exam Review.Suppose we made the instance variable, weight, an int. Will the following getter method compile correctly? public double getWeight() { return weight; } Pretend Questions:
15
Exam Review Pretend Questions: Assuming weight is an int variable assigned the value 75 and height is a double with value 1.77, what value is returned by the following definition of getBMI. public double getBMI() { return (weight/((int) height* (int) height)); }
16
Exam Review Good Luck.
17
Concepts In Assignment 4 Named Constants VS. Variables - A named constant won’t change. Ever. Java won’t let it. - Variables values can change, hence the name “variable”. (In Assignment 4, you may use other named constants, but only one variable.)
18
Concepts In Assignment 4 Boolean Variables (chapter 5, pg. 35) - Just like playing “Twenty Questions” with the computer; only “yes” or “no” answers. - These answers are True and False, and can be used to evaluate a state.
19
Concepts In Assignment 4 Rational Operators The “questions” you can ask the computer: Name == != > < >= <= Action equal? not equal? greater than? less than? greater than or equal? less than or equal? (“if” statements not necessary for the value to be returned)
20
Concepts In Assignment 4 Type Casting -Computer uses a set of rules to evaluate an expression with type casting; you must be careful that the answer you want is the answer returned. What happens with: int i = (int) (5/2.0) double d = 5/(int)2.0
21
Concepts In Assignment 4 Type Casting -Computer uses a set of rules to evaluate an expression with type casting; you must be careful that the answer you want is the answer returned. What happens with: int i = (int) (5/2.0) double d = 5/(int)2.0 Answer: int I = 2; Answer: double d = 2.0
22
Concepts In Assignment 4 Do you know about the Math library? Could help you… Look in Chapter 5, page 26 if you don’t know what this slide is talking about.
23
Operator Precedence ! - (T) * / % + - > = == != & | && || Order of Evaluation: 1) 2) 3) 4) 5) 6) 7) 8) 9)
24
Operator Precedence Examples: -5 -4 5/4*3 (int) 5 / 2.0
25
Operator Precedence Examples: -5 -4 5/4*3 (int) 5 / 2.0 Answers: -9 (5/4)*3 2.5
26
Concepts In Assignment 4 Don’t Forget: READ!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.