1 / 18

Chapter 2

Chapter 2. Wrap Up 9/20/16 & 9/26/16. Topics. Review for Exam I More Style Conventions Debugging using eclipse The Java API. Exam I. Next week More details on my website. Review material on my website. Previous Style Conventions. Class names begin with upper case

jmccranie
Download Presentation

Chapter 2

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. Chapter 2 Wrap Up 9/20/16 & 9/26/16

  2. Topics • Review for Exam I • More Style Conventions • Debugging using eclipse • The Java API

  3. Exam I • Next week • More details on my website. • Review material on my website

  4. Previous Style Conventions • Class names begin with upper case • Method names and variable names begin with lower case • Braces • eclipse puts open brace on the line with the heading • Line up indentation of the closing brace with whatever it is ending. • End brace commented(optional) • Lines within braces indented • Skip lines for readability (white space)

  5. More Conventions from 2.10 • Each statement on its own line. • A blank line can separate groups of statements, but related statements usually have no blank lines between them. • Arithmetic operators and = separate by one space. No space precedes an ending semicolon or ( ). • Variable/parameter names are descriptive, use at least two words. • exceptions for loop indices (i, j), or math items like point coordinates (x, y).

  6. More Conventions from 2.10 • Variables usually defined early (not within code), and initialized to be safe (if practical). • Lines of code are typically less than 100 characters wide. • Look at GramsToOuncesStyle2.java

  7. What is wrong with this style? import java.util.Scanner; public class GramsToOuncesStyle2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); final double CONVfactor = 28.35; double g; System.out.println("Grams?"); g = scan.nextDouble(); double o=g/CONVfactor; System.out.println(g+" grams is "+ o+" ounces"); scan.close(); }}

  8. Questions • In the following program give an example of something that is not properly named. • Give an example of bad indentation. • What is wrong with the println statement?

  9. Copy This Program and Fix the Style publicclass ShippingCalculator { publicstaticvoid main (String [] args) { intshipWeightPounds = 10; intshipCostCents; finalintFLAT_FEE_CENTS = 75 ;finalintcentsPerPound=25 ; shipCostCents = FLAT_FEE_CENTS + shipWeightPounds * centsPerPound; System.out.println("Weight(lb): " + shipWeightPounds + ", Flat fee(cents): " + FLAT_FEE_CENTS + ", Cents per pound: " + centsPerPound + ", Shipping cost(cents): " + shipCostCents); return; } }

  10. Debugging 10/11/16 & 10/12/16

  11. Debugging in Eclipse 2.8 • Syntax Errors • Caught by the Editor. • Logic Errors • Bad Results • Can use diagnostic prints to examine variables • In eclipse you can exam the contents of variable

  12. Debugging • Do • Predict a cause of the problem. • Conduct a test to validate the cause. • Repeat • Don't • Make random changes • Stare at the program • A “walk-through” can be good.

  13. Debugging in Eclipse GramstoOunces Example, 42.0 g→1.48 oz • Set a break point in the program near a possible bug. • Right click in margin left of code • Can right click again to untoggle • Run the program with “Debug as” • Click yes when dialog box appears • Gets you into Debug mode • Use box in upper right to examine variables.

  14. Debugging in Eclipse • Use buttons to execute one statement at a time. • Step-into • Step-over for java methods • Click “Java” on top tool bar to get back to normal mode.

  15. import java.util.Scanner; //Program to convert grams to ounces publicclass GramsToOunces{ publicstaticvoid main(String[] args) { Scanner scan = new Scanner(System.in); finaldoubleCONV_FACTOR = 28.35; doublegrams = 0.0; System.out.println("Grams?"); grams = scan.nextDouble(); doubleounces = grams / CONV_FACTOR; System.out.println(grams + " grams is " + ounces + " ounces"); scan.close(); }

  16. Questions • How do you toggle a breakpoint in eclipse? • How do you get into Debug mode? • Why would you want to examine a variable's contents? • What is the difference between step-into and step-over?

  17. Java API 2.7 • Google “Java API” • Oracle owns the language. • Use API to look up the String class. • Find the toUpperCase() method.

  18. Lab • Copy the program in AllCaps.java into eclipse. • Predict the bug in the program. • Write down the prediction for class today. • Set a break point in the above program to test the prediction. • Examine variable contents • Fix the bug in the program. • Show me the prediction and how you used a break point.

More Related