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

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Chapter 8 Improving the User Interface
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Alice in Action with Java
The Web Warrior Guide to Web Design Technologies
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 1: Introduction
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Datalogi A 1: 8/9. Book: Cay Horstmann: Big Java or Java Consepts.
Begin Java having used Alice Pepper - Some slides from Alice in Action with Java.
Introduction to Computing and Programming
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Chapter 2 Screen Output Section 2.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Java Applications & Program Design
A First Program Using C#
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Microsoft Visual Basic 2005: Reloaded Second Edition
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Alice in Action with Java Chapter 7 From Alice to Java.
Alice in Action with Java Chapter 7 From Alice to Java.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
1 Chapter 2 First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
The Java Programming Language
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Alice in Action with Java Chapter 9 Methods. Alice in Action with Java2 Non- void vs. void Methods Alice messages –Methods: messages sent where a statement.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
By Mr. Muhammad Pervez Akhtar
Creating a Java Application and Applet
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Introduction to Java Applications
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 2 Introduction to Java Applications
Console Input and Output
MSIS 655 Advanced Business Applications Programming
Anatomy of a Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Introduction to Java Applications
Computer Programming-1 CSC 111
Presentation transcript:

Alice in Action with Java Chapter 7 From Alice to Java

Alice in Action with Java2 Objectives Write some first Java programs Learn the basics of the development process for Java programs Begin making the transition from Alice to Java

Alice in Action with Java3 From Alice to Java Benefits of program development using Alice –3D 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 Java4 Program Development The four steps of computer programming –1. Designing the program –2. Writing the program –3. Running the program –4. Testing the program

Alice in Action with Java5 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 Java6 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 Java7 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 Java8 The Object List (continued)

Alice in Action with Java9 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 Java10 The Operations List (continued)

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

Alice in Action with Java12 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 We are going to learn to develop programs using command line tools BlueJ is a simple-to-use IDE for Java

Alice in Action with Java13 Java Programming Process Use a text editor to create a file containing the program code –vim MyFirstProgram.java Compile the program –javac MyFirstProgram Run the program –java MyFirstProgram

Alice in Action with Java14 Java Basics Strategy: compare the first program to the algorithm References –Algorithm developed in Section 7.1 –DollarsToEurosConverter.java in Figure 7-20

Alice in Action with Java15 DollarsToEurosConverter import java.util.Scanner; /** DollarsToEurosConverter converts dollars to Joel Adams */ public class DollarsToEurosConverter { public static void main( String [] args) { System.out.print( "How many dollars? "); Scanner keyboard = new Scanner( System.in); double dollars = keyboard.nextDouble(); System.out.print( "How many euros per dollar? "); double eurosPerDollar = keyboard.nextDouble(); double euros = dollars * eurosPerDollar; System.out.printf( "%.2f dollars => %.2f euros\n", dollars, euros); }

Alice in Action with Java16 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 Java17 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 class should have an opening comment –Place it just above the " public class … " In general, add comments to improve readability

Alice in Action with Java18 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 –However, you cannot replace main with another method

Alice in Action with Java19 The Simplest Java Program

Alice in Action with Java20 The main Method 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 }

Alice in Action with Java21 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 Java22 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;

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

Alice in Action with Java24 Use printf for formatting numbers

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

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

Alice in Action with Java27 Designing the UnitPricer Program (continued)

Alice in Action with Java28 Designing the UnitPricer Program (continued)

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

Alice in Action with Java30 Designing the UnitPricer Program (continued)

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

Alice in Action with Java32 Writing the UnitPricer Program (continued)

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

Alice in Action with Java34 Testing the UnitPricer Program (continued)

Alice in Action with Java35 Testing the UnitPricer Program (continued)

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

Alice in Action with Java37 Solving the Problems (continued)

Alice in Action with Java38 Solving the Problems (continued)

Alice in Action with Java39 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 Java40 The Software Engineering Process (continued)

Alice in Action with Java41 Summary Steps in program development: designing, writing, running, and testing Java: object-oriented language used to develop cross-platform 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 Java42 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