Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

25
Comp 14 (3) Comp 14 (3) By Stephan Sherman Office hours, W, 3:00- 3:50pm
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    2

Transcript of Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

Page 1: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

Comp 14 (3)Comp 14 (3)By Stephan Sherman

Office hours, W, 3:00-3:50pm

Page 2: 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

Page 3: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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

Page 4: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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)

Page 5: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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

Page 6: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

Exam Review

class aMoneyconverter { int Cents (dollars) { return dollars*100; }}

No parameter type

It’s notCapitalized

What else?

Page 7: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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?

Page 8: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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.

Page 9: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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);

Page 10: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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.”

Page 11: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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.”

Page 12: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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

Page 13: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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:

Page 14: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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));

}

Page 15: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

Exam Review

Good Luck.

Page 16: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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.)

Page 17: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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.

Page 18: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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)

Page 19: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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

Page 20: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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

Page 21: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

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.

Page 22: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

Operator Precedence

Operator Precedence

! - (T) * / % + - > < <= >=== !=&|&&||

Order of Evaluation:1)2)3)4)5)6)7)8)9)

Page 23: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

Operator Precedence

Operator Precedence

Examples:-5 -45/4*3(int) 5 / 2.0

Page 24: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

Operator Precedence

Operator Precedence

Examples:-5 -45/4*3(int) 5 / 2.0

Answers:-9(5/4)*32.5

Page 25: Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.

Concepts In Assignment 4

Don’t Forget: READ!