210 likes | 324 Views
COMP 110 Worksheet review, debugger. Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014. Announcements. Lab 4 due Wednesday, 2pm Remember to also print out your code and hand it in Extra office hours before Wednesday’s class?. Questions?. Any further questions on Lab 3?
E N D
COMP 110Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014
Announcements • Lab 4 due Wednesday, 2pm • Remember to also print out your code and hand it in • Extra office hours before Wednesday’s class?
Questions? • Any further questions on Lab 3? • How is Lab 4 going?
Today in COMP 110 • Some comments about the type boolean and boolean expressions • Go over in-class worksheet • Debugger • Revisiting classes
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) ...
boolean type • Can give boolean variable the value of a boolean expression by using an assignment statement int number = -5; boolean isPositive = (number > 0);
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”);
boolean type • Can use more complicated expressions also boolean systemsAreOK = (temperature <= 100) && (thrust >= 12000) && (cabinPressure > 30);
&& 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))
&&, ||: 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))
&, |: 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
&, |: 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
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 (4th edition), p. 218 (5th edition) • Use a debugger
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!
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
Class • A class is the definition of a kind of object • A blueprint for constructing specific objects 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, Instantiation 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
UML (Universal Modeling Language) Class name Data Methods (actions)
Something we’ve seen before Automobile suesCar = new Automobile(); Scanner keyboard = new Scanner(System.in); Assign memory address of object to variable Return memory address of object Create an object
Wednesday • Lab 4 due, 2pm • Go over program 2 21