Ch02ZybWrapUp

Download Report

Transcript Ch02ZybWrapUp

Chapter 2
Wrap Up
2/18/16 & 2/22/16
Topics

Review for Exam I

DecimalFormat

More Style Conventions

Debugging using eclipse

The Java API
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
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
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
Class DecimalFormat

Figure 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
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)
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).
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
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?
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;
}
}
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
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.

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.
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?
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.
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.
Exam Next Week

Don't forget your notes!

Good Luck!