begin java having used alice
DESCRIPTION
Begin Java having used Alice. Pepper - Some slides from Alice in Action with Java. Objectives. Write some first Java programs Learn the basics of the BlueJ Integrated Development Environment (IDE) Begin making the transition from Alice to Java. Alice vs Java. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/1.jpg)
Begin Java having used Alice
Pepper
- Some slides from Alice in Action with Java
![Page 2: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/2.jpg)
Objectives
• Write some first Java programs
• Learn the basics of the BlueJ Integrated Development Environment (IDE)
• Begin making the transition from Alice to Java
![Page 3: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/3.jpg)
Alice vs Java
Alice Java
•3D graphics let you visualize programming concepts•Drag-and-drop coding reduces syntax errors
•Create new objects - •Run Java programs across various platforms•Build applets to make Web pages interactive
Much of Alice is written in Java
![Page 4: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/4.jpg)
A very simple Alice program
![Page 5: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/5.jpg)
A Very Simple Java Program
public class JavaWorld { public static void main(String[] args) { System.out.println
("This is my first Java program."); }}
Class header
Method header
statements
Close braces markthe end
Open braces markthe beginning
![Page 6: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/6.jpg)
A First Program – What Does It Do?
Prints the messageThis is my first Java program.
Ends the line
System.out.println("This is my first Java program.");
![Page 7: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/7.jpg)
Same steps to create a program
Alice Java
Storywrite Design
Code Code
Compile
Play Run
Test Test
![Page 8: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/8.jpg)
Make the Program Run
• Compile (translate to java byte code)
• Run (interpreter for that OS reads java byte code and translates for machine)
Sourcex.java
Compile(javac x.java)
Objectx.class
Execute(java x)
![Page 9: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/9.jpg)
Development Environment
Blue Jay
Lets you enter code just like Alice did
Download at:
http://www.bluej.org/download/download.html
Instructions on syllabus
![Page 10: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/10.jpg)
Exercise
• Add the line “Welcome to Java”
• When you run the program, you will see:
This is my first Java Program. Welcome to Java.
• Extra: – Info: print() does not go to the next line– Try splitting Welcome to Java into two
statements: “Welcome” and then “ to Java.”
![Page 11: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/11.jpg)
A program that does something
Convert Dollars to Euros
Where to start?
![Page 12: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/12.jpg)
Design possibilities
• By words : Nouns and Verbs algorithm
• By test data : List some possible inputs and outputs before coding
• By class diagram : Design object pictures
• By flow chart : chart the sequential steps and decisions
![Page 13: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/13.jpg)
Designing by words
• 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)
![Page 14: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/14.jpg)
Nouns objects
? = make a Class
![Page 15: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/15.jpg)
Verbs -> Operations
![Page 16: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/16.jpg)
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 17: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/17.jpg)
Design by test data• Possible inputs and outputs:
Input Dollar
Input euros per dollar Rate
Output Euros
10 100 1000
10.55 100 1055
10 .055 .55
0 100 0
100 0 0
![Page 18: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/18.jpg)
Design by class diagram
DollarsToEuroConverter
main
Scanner
nextDouble
Keyboard Input
![Page 19: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/19.jpg)
Design by flowchart
Ask for dollars
start
Read Dollars
Ask for rate
Read rate
Compute euros
Print euros
end
![Page 20: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/20.jpg)
Let’s start coding – setup #1
Create a new project in BlueJCreate a new classWrite the standard new World starter: public class DollarsToEuroConverter { public static void main(String[ ] args) { }}
![Page 21: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/21.jpg)
Comments – setup #2• 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 */• Add comments to your class now at the top:
/** DollarsToEurosConverter.java converts dollars to euros.
@author you
*/
Public class DollarsToEurosConverter
![Page 22: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/22.jpg)
Import Packages – setup #3• Like importing other character blueprints– actually
groups of characters• Package Example: Scanner class is in java.util
package• Bring it in with an import statement
– Example: import java.util.Scanner; - let’s us talk with the user – like world’s “ask user for” functions
• Do it now in your program between comment and class start:/** DollarsToEurosConverter.java converts dollars to euros. @author you*/Import java.util.scannerPublic class DollarsToEurosConverter
![Page 23: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/23.jpg)
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) – Like SAY
• Ask now in your program, and then run it
public class DollarsToEuroConverter { public static void main(String[ ] args) { System.out.print("How many dollars do you want to convert?”); }}
![Page 24: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/24.jpg)
Step 2 – Read dollars from screen
• Step 2 – Read dollars– This means we have to read from the screen. We
need an object that knows how to do this. Scanner. – Once we create him, we can ask him what the user
typed on the screen. We can ask him as many times as we like once he is created.
– Create a scanner in your program now
Scanner kbd = new Scanner (System.in);
![Page 25: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/25.jpg)
Step 2 read the dollars cont.
• Now we can ask scanner for the dollars, but we will need to save those dollars into a variable:
double dollars; -> creates a variable
dollars = kbd.nextDouble(); -> gets the input from the user and puts it into dollars
![Page 26: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/26.jpg)
Step 3 & 4 – same thing for rate
• Step 3– Display "What is the euros-per-dollar exchange rate?”– Use System.out.print(String query)
System.out.print(“What is the Euros per dollar exchange rate?”);
• Step 4– Read eurosPerDollar– Reuse the Scanner object from Step 2
double eurosPerDollar; -> creates a variableeurosPerDollar = kbd.nextDouble(); -> gets the
input from the user and puts it into eurosPerDollar
![Page 27: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/27.jpg)
Step 5 - compute
• Step 5– Compute euros = dollars * eurosPerDollar– Assign the value in the expression to euros
variable
double euros;
euros = dollars * eurosPerDollar;
![Page 28: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/28.jpg)
Step 6 – tell result
– Display dollars and euros, plus descriptive labels– Use System.out.println(String output)– Concatenation operator (+) combines String values
Type:
System.out.println(dollars + “dollars => “ + euros + “euros”);
• RUN YOUR Program
![Page 29: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/29.jpg)
Check test data• Possible inputs and outputs:
Input Dollar
Input euros per dollar Rate
Output Euros
10 100 1000
10.55 100 1055
10 .055 .55
0 100 0
100 0 0
![Page 30: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/30.jpg)
Testing a Java Program Using BlueJ
• 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 31: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/31.jpg)
Print statement – a bit more
• 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 32: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/32.jpg)
Some Java Statements (continued)
![Page 33: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/33.jpg)
Step 6 – a bit better
Type the statement to print the result to the screen in place of println:
System.out.printf(
“%.2f dollars => %.2f euros”,dollars, euros);
![Page 34: Begin Java having used Alice](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56814093550346895dac25c4/html5/thumbnails/34.jpg)
Your toolset
• Basic setup including comments and importing packages
• Print to screen with formatting
• Read from screen
• Create variables to hold decimal values
• Calculate (*,/,+,-,^)