1 / 21

COMP 110 Worksheet review, debugger

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?

Download Presentation

COMP 110 Worksheet review, debugger

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 110Worksheet 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?

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

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

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

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

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

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

  9. Review in-class worksheet

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

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

  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

  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

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

  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!

  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

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

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

  19. UML (Universal Modeling Language) Class name Data Methods (actions)

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

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

More Related