44
Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java Chapter 7 From Alice to Java

Embed Size (px)

Citation preview

Page 1: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java

Chapter 7From Alice to Java

Page 2: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 2

The Algorithm

• Algorithm: sequence of steps that solve a problem

• Algorithm for converting dollars to euros– 1. Display "How many dollars do you want to convert?"– 2. Read dollars– 3. Display "What is the euros-per-dollar exchange

rate?"– 4. Read eurosPerDollar– 5. Compute euros = dollars * eurosPerDollar– 6. Display dollars and euros, plus descriptive labels

Page 3: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 3

Writing a First Java Program

• Integrated development environment (IDE)– Program editor– Compiler– Run-time environment– Debugger

• Eclipse– Free IDE for various languages developed by IBM– Java programs in this text are built using Eclipse

Page 4: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 4

Starting Eclipse

• Double-click eclipse.exe or eclipse shortcut– Select a folder to hold your Java programs

• Items in the first time welcome screen – An overview of Eclipse– Tutorials for using Eclipse– Some code samples– A summary of what’s new in the current edition

• The Eclipse IDE is divided into several regions

Page 5: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 5

Starting Eclipse (continued)

Page 6: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 6

Writing a Java Program Using Eclipse

• Creating a project– Select File->New->Project from the menu bar

• New Project dialog box opens

– Make sure that Java Project is selected – Click the Next button

• New Java Project dialog box opens

– Enter a descriptive name– Click the Finish button

• After clicking the Finish, the main window reappears – The new project appears in the package explorer area

Page 7: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 7

Writing a Java Program Using Eclipse (continued)

Page 8: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 8

Writing a Java Program Using Eclipse (continued)

Page 9: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 9

Writing a Java Program Using Eclipse (continued)

• All Java programs are classes

• Creating the DollarsToEurosConverter class– Select the project in the package explorer area– Select File->New->Class

• The New Java Class dialog box opens

– Type DollarsToEurosConverter as the name– Ensure Public static void main()is checked – Click the Finish button

• After the class has been created, enter program code– Lines in main() correspond to steps in the algorithm

Page 10: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 10

Writing a Java Program Using Eclipse (continued)

Page 11: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 11

Writing a Java Program Using Eclipse (continued)

Page 12: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 12

Compiling a Java Program Using Eclipse

• Java compiler: checks a program for syntax errors

• Invoke the compiler by saving your program

• How the Java compiler reports errors– Error symbol displayed next to problem statement– Error is listed in the problems & output area

• Example of an error: omit semicolon after statement – The compiler marks the line as an error – Error details are shown in the problems & output area

• All errors must be fixed before you can run a program

Page 13: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 13

Compiling a Java Program Using Eclipse (continued)

Page 14: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 14

Running a Java Program Using Eclipse

• Running your program the first time– Choose the Run->Run... menu choice

• The Run dialog box opens

– Ensure Java Application is selected – Click New button below Configurations column

• A new Java Application run configuration appears

– Ensure project is listed in the box below Project– Use Search to identify class containing main()– Click the Apply button and then click Run

• Initiate future executions by simply clicking Run

Page 15: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 15

Running a Java Program Using Eclipse (continued)

Page 16: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 16

Running a Java Program Using Eclipse (continued)

Page 17: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 17

Running a Java Program Using Eclipse (continued)

Page 18: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 18

Testing a Java Program Using Eclipse

• Functional testing– Running a program multiple times, using various values– Example: use various dollar values and exchange rates

• Sanity checking: testing with easily verified values

• Logic error: problem with the program structure

• User testing– Utilizing another person to uncover hidden flaws– Example: roommate reveals euros formatting error

• Solution: use printf()to round off values of euros

Page 19: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 19

Testing a Java Program Using Eclipse (continued)

Page 20: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 20

Java Basics

• Strategy: compare the first program to the algorithm

• References– Algorithm developed in Section 7.1– DollarsToEurosConverter.java in Figure 7-20

Page 21: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 21

Java Basics (continued)

Page 22: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 22

Comments

• Comments: explanatory remarks

• Comments are ignored by the Java compiler

• Three different kinds of comments– Inline: begins comment with // and ends at line’s end– Block (C-style): begins with /* and ends with */– Javadoc: begins with /** and ends with */

• Opening comments of converter are Javadoc type– Every file should have an opening comment

• In general, add comments to improve readability

Page 23: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 23

Import Statements and Packages

• Package: group of related, predefined classes– Example: Scanner class is in java.util package

• Access packaged classes with an import statement– Place import statements before class declaration– Example: import java.util.Scanner;

• Wild-card import statement– Used to import all of the classes in a package– Example: import java.util.*;

• java.lang contains commonly used classes– Automatically imported into a program

Page 24: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 24

The Simplest Java Program

• Consists of a class and an empty main()method

• General pattern used to define a class:public class NameOfTheClass { }

– The word public makes the class visible to users– The word class indicates a new type name follows

• Every Java program needs a main()method

• The main()method is defined within the class body• main()is like myFirstMethod()in Alice

Page 25: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 25

The Simplest Java Program (continued)

Page 26: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 26

The Simplest Java Program (continued)

• Understanding the structure of the main()method– public allows method to be invoked outside class– static indicates that main()belongs to the class– void indicates that main()does not return a value– main is the name of the method– String[]args provides a way to pass values – Statements placed within body are delimited by { and }

• Adding Java statements using Eclipse – Type or paste statements into the editing area– Differs from drag-and-drop style used in Alice

Page 27: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 27

Some Java Statements

• Technique for writing a program– Go through an algorithm step by step– Translate each step into an equivalent Java statement

• Goal: apply technique to dollars-to-euros algorithm

• Step 1– Display "How many dollars do you want to convert?”– Use System.out.print(String query)

• Step 2 – Read dollars– Use a Scanner object to retrieve value from keyboard

Page 28: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 28

Some Java Statements (continued)

• Step 3– Display "What is the euros-per-dollar exchange rate?”– Use System.out.print(String query)

• Step 4– Read eurosPerDollar– Reuse the Scanner object from Step 2

• Step 5– Compute euros = dollars * eurosPerDollar– Assign the value in the expression to euros variable– double euros = dollars * eurosPerDollar;

Page 29: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 29

Some Java Statements (continued)

• Step 6– Display dollars and euros, plus descriptive labels– Use System.out.println(String output)

• Concatenation operator (+) combines String values• The printf()statement

– Controls the format of printed values– Must have at least one argument (format-string)– Arguments after the format-string need a placeholder– Example: "%.2f dollars => %.2f euros“

• Placeholder %.2f provides precision and type information

Page 30: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 30

Some Java Statements (continued)

Page 31: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 31

A Second Java Program

• Problem: how to determine relative value

• Scenario 1– Regular size cereal costs $2.90 per 12 ounces– Economy size cereal costs $4.00 per 15 ounces

• Scenario 2– 60-gigabyte MP3 player costs $150– 80-gigabyte model costs $190

• Solution: compare items using unit prices

• Goal: program should find the unit price of an item

Page 32: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 32

Designing the UnitPricer Program

• Step 1: elements of user story built around unit price– Query: “What is the price of the first item?”– Read the first price from the keyboard– Query “How many units are in the first item?”– Read the number of units in the first item– Perform the first four actions for the second item– Compute and display the unit prices of the two items– Use of a generic item broadens program’s application

• Step 2: extract the objects from the noun phrases

• Step 3: extract the methods from the verb phrases

Page 33: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 33

Designing the UnitPricer Program (continued)

Page 34: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 34

Designing the UnitPricer Program (continued)

Page 35: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 35

Designing the UnitPricer Program (continued)

• Step 4: develop the UnitPricer algorithm

• The algorithm is the blueprint for the program

• Generalization: broadens application of a program

• Generalization in the UnitPricer algorithm – The use of “item” in place of cereal box and MP3 player– “Item” is a generic term embracing a variety of objects

Page 36: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 36

Designing the UnitPricer Program (continued)

Page 37: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 37

Writing the UnitPricer Program

• A summary of the steps– Create a new Java project in Eclipse– Create the UnitPricer class– Implement the algorithm in main()

• Figure 7-24 presents the final version

Page 38: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 38

Writing the UnitPricer Program (continued)

Page 39: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 39

Testing the UnitPricer Program

• Conduct testing using easy-to-verify values

• The printf() message revisited– Begin format-string with %n to advance cursor one line– %w.pf placeholder used to specify width and precision

• Format-string: "%nItem 1 unit price: $%7.2f“– %n advances the cursor to the next line– %7.2f: 7 spaces, two decimal places for a real number

Page 40: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 40

Testing the UnitPricer Program (continued)

Page 41: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 41

Testing the UnitPricer Program (continued)

Page 42: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 42

Solving the Problems

• Solve the MP3 player problem – Reminder: the 60-gigabyte model cost $150 – Reminder: the 80-gigabyte model cost $190

• MP3 player solution: 80-GB model is a better value

• Solve the corn flakes problem– Reminder: the 12-ounce “regular” size cost $2.90– Reminder: the 15-ounce “economy” size cost $4.00

• Corn flakes solution: regular size box is a better value

Page 43: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 43

Solving the Problems (continued)

Page 44: Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java 44

Solving the Problems (continued)