comp 14 (3) by stephan sherman office hours, w, 3:00-3:50pm
Post on 20-Dec-2015
216 views
TRANSCRIPT
Comp 14 (3)Comp 14 (3)By Stephan Sherman
Office hours, W, 3:00-3:50pm
Fun For Today:Fun For Today: Review for the Coming Exam
(gasp!) Concepts in Assignment 4 A little bit of “Operator
Precedence” stuff
READ! READ MORE!
KEEP READING!
DON’T STOP!
Even you, that personin the back that doesn’tthink they have to.
First Step Towards a Better First Step Towards a Better Exam GradeExam Grade
Yeah, you.
Exam Review
Figure out what you do NOTknow, and ask someone.
Anyone.
Before the Professor asks you.
Second Step Towards a Better Second Step Towards a Better Exam GradeExam Grade
Exam Review
("Never, ever, enter a battle of wits half-armed." - Cavanaugh)
Exam Review
Exam Specs: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
Exam Review
class aMoneyconverter { int Cents (dollars) { return dollars*100; }}
No parameter type
It’s notCapitalized
What else?
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?
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.
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);
Exam Review
Pure vs. Impure FunctionsPure 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.”
Exam Review
Pure vs. Impure FunctionsPure 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.”
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
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:
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));
}
Exam Review
Good Luck.
Concepts In Assignment 4
Named Constants VS. VariablesNamed Constants VS. Variables
- A named constant won’t change.Ever. Java won’t let it.
- Variables values can change, hencethe name “variable”.
(In Assignment 4, you may use othernamed constants, but only one variable.)
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.
Concepts In Assignment 4
Rational OperatorsRational Operators
The “questions” you can askthe computer:
Name==!=><>=<=
Actionequal?not equal?greater than?less than?greater than or equal?less than or equal?
(“if” statements not necessary for the value to be returned)
Concepts In Assignment 4
Type Casting
-Computer uses a set of rules toevaluate an expression with typecasting; you must be careful that theanswer you want is the answerreturned.
What happens with:
int i = (int) (5/2.0)
double d = 5/(int)2.0
Concepts In Assignment 4
Type Casting
-Computer uses a set of rules toevaluate an expression with typecasting; you must be careful that theanswer you want is the answerreturned.
What happens with:
int i = (int) (5/2.0)
double d = 5/(int)2.0
Answer: int I = 2;
Answer: double d = 2.0
Concepts In Assignment 4
Do you know about the Mathlibrary? Could help you…
Look in Chapter 5, page 26 ifyou don’t know what this slideis talking about.
Operator Precedence
Operator Precedence
! - (T) * / % + - > < <= >=== !=&|&&||
Order of Evaluation:1)2)3)4)5)6)7)8)9)
Operator Precedence
Operator Precedence
Examples:-5 -45/4*3(int) 5 / 2.0
Operator Precedence
Operator Precedence
Examples:-5 -45/4*3(int) 5 / 2.0
Answers:-9(5/4)*32.5
Concepts In Assignment 4
Don’t Forget: READ!