Alice in Action with Java Chapter 7 From

  • Slides: 56
Download presentation
Alice in Action with Java Chapter 7 From Alice to Java Alice in Action

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

Objectives • Write some first Java programs • Learn the basics of the Eclipse

Objectives • Write some first Java programs • Learn the basics of the Eclipse Integrated Development Environment (IDE) • Begin making the transition from Alice to Java Alice in Action with Java 2

From Alice to Java • The four steps of computer programming – – 1.

From Alice to Java • The four steps of computer programming – – 1. Designing the program 2. Writing the program 3. Running the program 4. Testing the program • Program development using Alice – – Step 1: Write user stories and construct storyboards Step 2: Add objects to world, animate with messages Step 3: Press the Play button Step 4: Scrutinize a number of executions Alice in Action with Java 3

From Alice to Java (continued) • Benefits of program development using Alice – 3

From Alice to Java (continued) • Benefits of program development using Alice – 3 D graphics let you visualize programming concepts – Drag-and-drop coding reduces syntax errors • Benefits of program development using Java – Build reusable classes – Run Java programs across various platforms – Build applets to make Web pages interactive Alice in Action with Java 4

Designing a First Java Program • Use many techniques learned in Alice • First

Designing a First Java Program • Use many techniques learned in Alice • First step: write a user story to help setup structure • User story for dollars-to-euros currency conversion – – – Query user for dollar amount to convert to euros Read the dollar amount from the user Query user for euros-per-dollar exchange rate Read euros-per-dollar exchange rate Compute corresponding number of euros Display dollar and (computed euros values) Alice in Action with Java 5

The Object List • Review: class is blueprint for an object • Procedure for

The Object List • Review: class is blueprint for an object • Procedure for isolating objects in the user story – Identify the noun phrases (primary object indicators) – Create a table listing nouns, values stored, and names • The name should also be a noun (or noun phrase) • The name should be descriptive – If no Java type matches the value, define a class • Naming convention – Class: capitalize each word in the name – Variable or method: capitalize each word except first Alice in Action with Java 6

The Object List (continued) Alice in Action with Java 7

The Object List (continued) Alice in Action with Java 7

The Operations List • Operations correspond to verb phrases in user story • Procedure

The Operations List • Operations correspond to verb phrases in user story • Procedure for isolating verbs in the user story – Identify the verb phrases (primary operation indicators) – Create a table listing verbs and matching operations • There are five verb phrases in the user story Alice in Action with Java 8

The Operations List (continued) Alice in Action with Java 9

The Operations List (continued) Alice in Action with Java 9

The Algorithm • Algorithm: sequence of steps that solve a problem • Algorithm for

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 euros. Per. Dollar – 5. Compute euros = dollars * euros. Per. Dollar – 6. Display dollars and euros, plus descriptive labels Alice in Action with Java 10

Writing a First Java Program • Integrated development environment (IDE) – – Program editor

Writing a First Java Program • Integrated development environment (IDE) – – Program editor Compiler Run-time environment Debugger • Some features of the Alice IDE – World window, object tree, editing area, Play button • Eclipse – Free IDE for various languages developed by IBM – Java programs in this text are built using Eclipse Alice in Action with Java 11

Starting Eclipse • Double-click eclipse. exe or eclipse shortcut – Select a folder to

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 Alice in Action with Java 12

Starting Eclipse (continued) Alice in Action with Java 13

Starting Eclipse (continued) Alice in Action with Java 13

Writing a Java Program Using Eclipse • Creating a project – Select File->New->Project from

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 Alice in Action with Java 14

Writing a Java Program Using Eclipse (continued) Alice in Action with Java 15

Writing a Java Program Using Eclipse (continued) Alice in Action with Java 15

Writing a Java Program Using Eclipse (continued) Alice in Action with Java 16

Writing a Java Program Using Eclipse (continued) Alice in Action with Java 16

Writing a Java Program Using Eclipse (continued) • All Java programs are classes •

Writing a Java Program Using Eclipse (continued) • All Java programs are classes • Creating the Dollars. To. Euros. Converter class – Select the project in the package explorer area – Select File->New->Class • The New Java Class dialog box opens – Type Dollars. To. Euros. Converter 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 Alice in Action with Java 17

Writing a Java Program Using Eclipse (continued) Alice in Action with Java 18

Writing a Java Program Using Eclipse (continued) Alice in Action with Java 18

Writing a Java Program Using Eclipse (continued) Alice in Action with Java 19

Writing a Java Program Using Eclipse (continued) Alice in Action with Java 19

Compiling a Java Program Using Eclipse • Java compiler: checks a program for syntax

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 Alice in Action with Java 20

Compiling a Java Program Using Eclipse (continued) Alice in Action with Java 21

Compiling a Java Program Using Eclipse (continued) Alice in Action with Java 21

Running a Java Program Using Eclipse • Running your program the first time –

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 Alice in Action with Java 22

Running a Java Program Using Eclipse (continued) Alice in Action with Java 23

Running a Java Program Using Eclipse (continued) Alice in Action with Java 23

Running a Java Program Using Eclipse (continued) Alice in Action with Java 24

Running a Java Program Using Eclipse (continued) Alice in Action with Java 24

Running a Java Program Using Eclipse (continued) Alice in Action with Java 25

Running a Java Program Using Eclipse (continued) Alice in Action with Java 25

Testing a Java Program Using Eclipse • Functional testing – Running a program multiple

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 Alice in Action with Java 26

Testing a Java Program Using Eclipse (continued) Alice in Action with Java 27

Testing a Java Program Using Eclipse (continued) Alice in Action with Java 27

Java Basics • Strategy: compare the first program to the algorithm • References –

Java Basics • Strategy: compare the first program to the algorithm • References – Algorithm developed in Section 7. 1 – Dollars. To. Euros. Converter. java in Figure 7 -20 Alice in Action with Java 28

Java Basics (continued) Alice in Action with Java 29

Java Basics (continued) Alice in Action with Java 29

Comments • Comments: explanatory remarks • Comments are ignored by the Java compiler •

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 Alice in Action with Java 30

Import Statements and Packages • Package: group of related, predefined classes – Example: Scanner

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 Alice in Action with Java 31

The Simplest Java Program • Consists of a class and an empty main()method •

The Simplest Java Program • Consists of a class and an empty main()method • General pattern used to define a class: public class Name. Of. The. Class { } – 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 my. First. Method()in Alice in Action with Java 32

The Simplest Java Program (continued) Alice in Action with Java 33

The Simplest Java Program (continued) Alice in Action with Java 33

The Simplest Java Program (continued) • Understanding the structure of the main()method – –

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 in Action with Java 34

Some Java Statements • Technique for writing a program – Go through an algorithm

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 Alice in Action with Java 35

Some Java Statements (continued) • Step 3 – Display "What is the euros-per-dollar exchange

Some Java Statements (continued) • Step 3 – Display "What is the euros-per-dollar exchange rate? ” – Use System. out. print(String query) • Step 4 – Read euros. Per. Dollar – Reuse the Scanner object from Step 2 • Step 5 – Compute euros = dollars * euros. Per. Dollar – Assign the value in the expression to euros variable – double euros = dollars * euros. Per. Dollar; Alice in Action with Java 36

Some Java Statements (continued) • Step 6 – Display dollars and euros, plus descriptive

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: "%. 2 f dollars => %. 2 f euros“ • Placeholder %. 2 f provides precision and type information Alice in Action with Java 37

Some Java Statements (continued) Alice in Action with Java 38

Some Java Statements (continued) Alice in Action with Java 38

A Second Java Program • Problem: how to determine relative value • Scenario 1

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 MP 3 player costs $150 – 80 -gigabyte model costs $190 • Solution: compare items using unit prices • Goal: program should find the unit price of an item Alice in Action with Java 39

Designing the Unit. Pricer Program • Step 1: elements of user story built around

Designing the Unit. Pricer 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 Alice in Action with Java 40

Designing the Unit. Pricer Program (continued) Alice in Action with Java 41

Designing the Unit. Pricer Program (continued) Alice in Action with Java 41

Designing the Unit. Pricer Program (continued) Alice in Action with Java 42

Designing the Unit. Pricer Program (continued) Alice in Action with Java 42

Designing the Unit. Pricer Program (continued) • Step 4: develop the Unit. Pricer algorithm

Designing the Unit. Pricer Program (continued) • Step 4: develop the Unit. Pricer algorithm • The algorithm is the blueprint for the program • Generalization: broadens application of a program • Generalization in the Unit. Pricer algorithm – The use of “item” in place of cereal box and MP 3 player – “Item” is a generic term embracing a variety of objects Alice in Action with Java 43

Designing the Unit. Pricer Program (continued) Alice in Action with Java 44

Designing the Unit. Pricer Program (continued) Alice in Action with Java 44

Writing the Unit. Pricer Program • A summary of the steps – Create a

Writing the Unit. Pricer Program • A summary of the steps – Create a new Java project in Eclipse – Create the Unit. Pricer class – Implement the algorithm in main() • Figure 7 -24 presents the final version Alice in Action with Java 45

Writing the Unit. Pricer Program (continued) Alice in Action with Java 46

Writing the Unit. Pricer Program (continued) Alice in Action with Java 46

Testing the Unit. Pricer Program • Conduct testing using easy-to-verify values • The printf()

Testing the Unit. Pricer 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: "%n. Item 1 unit price: $%7. 2 f“ – %n advances the cursor to the next line – %7. 2 f: 7 spaces, two decimal places for a real number Alice in Action with Java 47

Testing the Unit. Pricer Program (continued) Alice in Action with Java 48

Testing the Unit. Pricer Program (continued) Alice in Action with Java 48

Testing the Unit. Pricer Program (continued) Alice in Action with Java 49

Testing the Unit. Pricer Program (continued) Alice in Action with Java 49

Solving the Problems • Solve the MP 3 player problem – Reminder: the 60

Solving the Problems • Solve the MP 3 player problem – Reminder: the 60 -gigabyte model cost $150 – Reminder: the 80 -gigabyte model cost $190 • MP 3 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 Alice in Action with Java 50

Solving the Problems (continued) Alice in Action with Java 51

Solving the Problems (continued) Alice in Action with Java 51

Solving the Problems (continued) Alice in Action with Java 52

Solving the Problems (continued) Alice in Action with Java 52

The Software Engineering Process • • Six steps are similar for Java and Alice

The Software Engineering Process • • Six steps are similar for Java and Alice development Software design: Steps 1 -4 Software implementation and testing: Steps 5 and 6 Software engineering: – Includes software design, implementation, and testing Alice in Action with Java 53

The Software Engineering Process (continued) Alice in Action with Java 54

The Software Engineering Process (continued) Alice in Action with Java 54

Summary • Steps in program development: designing, writing, running, and testing • Java: object-oriented

Summary • Steps in program development: designing, writing, running, and testing • Java: object-oriented language used to develop crossplatform applications • Algorithm: sequence of steps that solves a problem • IDE: program development tool that bundles an editor, compiler, run-time environment, and debugger • Eclipse: free IDE for Java developed by IBM Alice in Action with Java 55

Summary (continued) • Package: group of related, predefined classes • import statement: exposes a

Summary (continued) • Package: group of related, predefined classes • import statement: exposes a program to classes in a package • Display values by sending print(), println(), or printf()to an instance of System. out • Connect a program to the keyboard using a Scanner object • Software engineering: methodology for designing, implementing, and testing software Alice in Action with Java 56