Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COMP 110 Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014."— Presentation transcript:

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

2 Announcements Lab 4 due Wednesday, 2pm ◦ Remember to also print out your code and hand it in Extra office hours before Wednesday’s class? 2

3 Questions? Any further questions on Lab 3? How is Lab 4 going? 3

4 Today in COMP 110 Some comments about the type boolean and boolean expressions Go over in-class worksheet Debugger Revisiting classes 4

5 boolean type Can be either true or false Choose variable names that sound true when the value is true boolean isAlive = true; boolean isPositive = false; boolean systemsAreOK = true; if (isAlive)... if (isPositive)... if (systemsAreOK)... 5

6 boolean type Can give boolean variable the value of a boolean expression by using an assignment statement int number = -5; boolean isPositive = (number > 0); 6

7 boolean type Once 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

8 boolean type Can use more complicated expressions also boolean systemsAreOK = (temperature <= 100) && (thrust >= 12000) && (cabinPressure > 30); 8

9 Review in-class worksheet 9

10 && 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

11 &&, ||: short-circuit evaluation Evaluate 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

12 &, |: complete evaluation Evaluate all subexpressions, always What 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

13 &, |: 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

14 Finding errors Trace 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 (4 th edition), p. 218 (5 th edition) Use a debugger 14

15 Debugger in jGRASP Read 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

16 Classes and Objects Java 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

17 Class A 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.

18 Objects, Instantiation 18 Object Name: patsCar amount of fuel: 10 gallons speed: 55 miles per hour license plate: “135 XJK” Object Name: suesCar amount of fuel: 14 gallons speed: 0 miles per hour license plate: “SUES CAR” Object Name: ronsCar amount of fuel: 2 gallons speed: 75 miles per hour license plate: “351 WLF” Instantiations, or instances, of the class Automobile

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

20 Something we’ve seen before Automobile suesCar = new Automobile(); Scanner keyboard = new Scanner(System.in); 20 Create an object Return memory address of object Assign memory address of object to variable

21 Wednesday Lab 4 due, 2pm Go over program 2 21


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

Similar presentations


Ads by Google