COMP 110 Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014.

Post on 18-Jan-2016

215 views 0 download

Tags:

Transcript of COMP 110 Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014.

COMP 110COMP 110Worksheet review, debuggerWorksheet review, debugger

Luv KohliSeptember 29, 2008

MWF 2-2:50 pmSitterson 014

AnnouncementsAnnouncementsLab 4 due Wednesday, 2pm

◦Remember to also print out your code and hand it in

Extra office hours before Wednesday’s class?

2

Questions?Questions?Any further questions on Lab 3?

How is Lab 4 going?

3

Today in COMP 110Today in COMP 110Some comments about the type boolean

and boolean expressions

Go over in-class worksheet

Debugger

Revisiting classes

4

booleanboolean type typeCan be either true or falseChoose variable names that sound true

when the value is trueboolean isAlive = true;

boolean isPositive = false;

boolean systemsAreOK = true;

if (isAlive) ...

if (isPositive) ...

if (systemsAreOK) ...

5

booleanboolean type typeCan give boolean variable the value of a

boolean expression by using an assignment statement

int number = -5;

boolean isPositive = (number > 0);

6

booleanboolean type typeOnce boolean variable has a value, you

can use it just like any other boolean expression

int number = -5;

boolean isPositive = (number > 0);

if (isPositive)

System.out.println(“The number is positive”);

else

System.out.println(“The number is not positive”);

7

booleanboolean type typeCan use more complicated expressions

also

boolean systemsAreOK = (temperature <= 100) &&

(thrust >= 12000) && (cabinPressure > 30);

8

Review in-class worksheetReview in-class worksheet

9

&& vs. &, || vs. |&& vs. &, || vs. |In the if statement below, is the boolean

expression true or false?

int temperature = 100;

int rainFall = 40;

int humidity = 70;

if ((temperature > 95) || (rainFall > 20) || (humidity >= 60))

10

&&, ||: short-circuit evaluation&&, ||: short-circuit evaluationEvaluate the first subexpression

◦ If that is enough information, do not evaluate subsequent subexpressions

if ((temperature > 95) || (rainFall > 20) || (humidity >= 60))

if ((assignmentsDone > 0) &&

((totalScore / assignmentsDone) > 60))

11

&, |: complete evaluation&, |: complete evaluationEvaluate all subexpressions, alwaysWhat is wrong with this if statement?

if ((assignmentsDone > 0) &

((totalScore / assignmentsDone) > 60))

Possible divide by 0 error!◦assignmentsDone could be 0

Usually do not want or need to use complete evaluation

12

&, |: bitwise operators&, |: bitwise operators&, | are not valid as boolean logical

operators in many C-based languages

Usually, they are bitwise operators◦ In Java, can be used as boolean logical

operators or bitwise operators

More advanced, will not discuss bitwise operators now

13

Finding errorsFinding errorsTrace your variables

◦ Put output statements in your code to see what values are stored in your variables System.out.println(variable); Check whether the values are correct and what you expect

◦ Remove these extra output statements after the program runs correctly

◦ Read example in the book, p. 188 (4th edition), p. 218 (5th edition)

Use a debugger

14

Debugger in jGRASPDebugger in jGRASPRead sections 2.10, 7.1-7.5 of jGRASP

tutorial (linked off of course schedule)

Try using debugger on your programs

It will not help if your program does not compile!

15

Classes and ObjectsClasses and ObjectsJava programs (and programs in other

object-oriented programming languages) consist of objects of various class types

Objects can represent objects in the real world◦Automobiles, houses, employee records

Or abstract concepts◦Colors, shapes, words

16

ClassClassA class is the definition of a kind of object

◦A blueprint for constructing specific objects

17

Class Name: Automobile

Data: amount of fuel speed license plate

Methods (actions): accelerate: How: Press on gas pedal. decelerate: How: Press on brake pedal.

Objects, InstantiationObjects, Instantiation

18

Object Name: patsCar

amount of fuel: 10 gallonsspeed: 55 miles per hourlicense plate: “135 XJK”

Object Name: suesCar

amount of fuel: 14 gallonsspeed: 0 miles per hourlicense plate: “SUES CAR”

Object Name: ronsCar

amount of fuel: 2 gallonsspeed: 75 miles per hourlicense plate: “351 WLF”

Instantiations, or instances, of the class Automobile

UML (Universal Modeling Language)UML (Universal Modeling Language)

19

Automobile

- fuel: double- speed: double- license: String

+ accelerate(double pedalPressure): void+ decelerate(double pedalPressure): void

Class name

Data

Methods (actions)

Something we’ve seen beforeSomething we’ve seen beforeAutomobile suesCar = new Automobile();

Scanner keyboard = new Scanner(System.in);

20

Create an objectReturn memory address of object

Assign memory address of object to variable

WednesdayWednesdayLab 4 due, 2pm

Go over program 2

2121