Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions...

18
Chapter 2 Wrap Up 2/18/16 & 2/22/16

description

Exam I Next week  Th, 2/25/16  M, 2/29/16 Over Chapter 1, , 4.1 and 4.3, eclipse and linux Review material on my website

Transcript of Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions...

Page 1: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Chapter 2

Wrap Up2/18/16 & 2/22/16

Page 2: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Topics

Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API

Page 3: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Exam I

Next week Th, 2/25/16 M, 2/29/16

Over Chapter 1, 2.1-2.6, 4.1 and 4.3, eclipse and linux Review material on my website

Page 4: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Class DecimalFormat

Class DecimalFormat enables control of output In java.text package. Specify a string pattern to describe desired format

double val = 1.23;DecimalFormat formatter =

new DecimalFormat("0.000");System.out.println(formatter.format(val));

Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Page 5: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Class DecimalFormat

0 – digit, leading and trailing zeros# -- digit, no leading and trailing zeros% -- percentage$ -- at beginning displays dollar sign

Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Page 6: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Class DecimalFormatFigure 4-7 Examples of

patterns used with class DecimalFormat

Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Page 7: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Previous Style ConventionsClass names begin with upper caseMethod names and variable names begin with lower

caseBraces

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 indentedSkip lines for readability (white space)

Page 8: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

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

Page 9: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

More Conventions from 2.10

Constants use upper case and underscores (and at least two words)

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 GramsToOuncesStyle.java

Page 10: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

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?

Page 11: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Copy This Program and Fix the Style

public class ShippingCalculator { public static void main (String [] args) { int shipWeightPounds = 10; int shipCostCents; final int FLAT_FEE_CENTS = 75 ;final int centsPerPound=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; }}

Page 12: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

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

Page 13: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Debugging in Eclipse

OuncesToGrams Example, 1.5 oz → 42.52 g

1)Set a break point in the program near a possible bug. Right click in margin left of code Can right click again to untoggle

2)Run the program with “Debug as” Click yes when dialog box appears Gets you into Debug mode

3)Use box in upper right to examine variables.

Page 14: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Debugging in Eclipse

4)Use buttons to execute one statement at a time. Step-into Step-over for java methods

5)Click “Java” on top tool bar to get back to normal mode.

Page 15: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

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?

Page 16: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Java API 2.7

Google “Java API” Oracle owns the language. Use API to look up a class.

e.g. Scanner Can see what methods it has.

Page 17: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Lab

Copy the program on slide #12 into eclipse. Fix style problems in the program. Set a break point in the above program. Use the debugger

to examine the shipCostCents variable's contents while the program is executing.

Get into a web browser and find the Java API.

– Find the method in the String class that lets you know the length of a String.

Page 18: Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging…

Exam Next Week

Don't forget your notes! Good Luck!