Download Ch02ZybWrapUp

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
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

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
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();
}}
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
10/11/16 & 10/12/16
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

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.
Debugging in Eclipse
GramstoOunces Example, 42.0 g→1.48 oz
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.
import java.util.Scanner;
//Program to convert grams to ounces
public class GramsToOunces{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
final double CONV_FACTOR = 28.35;
double grams = 0.0;
System.out.println("Grams?");
grams = scan.nextDouble();
double ounces = grams / CONV_FACTOR;
System.out.println(grams + " grams is " +
ounces + " ounces");
scan.close();
}
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 the String class.

Find the toUpperCase() method.
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.