Begin Java having used Alice Pepper - Some slides from Alice in Action with Java.

Slides:



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

Begin Java Pepper. Objectives What is a program? Learn the basics of your programming tool: BlueJ Write a first Java program and see it run Make some.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter2: Introduction to Java Applications 1.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Alice in Action with Java Chapter 7 From Alice to Java.
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Chapter 3b Standard Input and Output Sample Development.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
COMP Computer Basics Yi Hong May 13, 2015.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Introducing Java.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Java Applications & Program Design
COMP 110 Spring Announcements Computers in class on Friday: Lab Office Hours: Monday 12-2 New students see me after class Administrative Changes.
COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
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.
Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.
Creating your first C++ program
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.
Input, Output, and Processing
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.
Board Activity Find your seat on the seating chart Login – Remember your login is your first initial your last name and the last three numbers of your.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
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.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
By Mr. Muhammad Pervez Akhtar
Creating a Java Application and Applet
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Introducing Java Chapter 3 Review. Why Program in Java? Java, is an object-oriented programming language. OOP languages evolved out of the need to better.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Intro to Programming STARS College of Communication and Information Florida State University Written by: Hannah Brock Alissa Ovalle Nicolaus Lopez Martin.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Foundations of Programming: Java
CompSci 230 S Programming Techniques
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Chapter 3 GC 101 Java Fundamentals.
Maha AlSaif Maryam AlQattan
Writing Methods.
Chapter 3 Classes and Objects
MSIS 655 Advanced Business Applications Programming
Introduction CSC 111.
Anatomy of a Java Program
Programming Fundamentals (750113) Ch1. Problem Solving
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Introduction to Java Applications
Programming Fundamentals (750113) Ch1. Problem Solving
Presentation transcript:

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 AliceJava 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

A very simple Alice program

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 mark the end Open braces mark the beginning

A First Program – What Does It Do? Prints the message This is my first Java program. Ends the line System.out.println ("This is my first Java program.");

Same steps to create a program AliceJava StorywriteDesign Code Compile PlayRun Test

Make the Program Run Compile (translate to java byte code) Run (interpreter for that OS reads java byte code and translates for machine) Source x.java Compile (javac x.java) Object x.class Execute (java x)

Development Environment Blue Jay Lets you enter code just like Alice did Download at: Instructions on syllabus

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.”

A program that does something Convert Dollars to Euros Where to start?

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

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)

Nouns  objects ? = make a Class

Verbs -> Operations

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

Design by test data Possible inputs and outputs: Input Dollar Input euros per dollar Rate Output Euros

Design by class diagram DollarsToEuroConverter main Scanner nextDouble Keyboard Input

Design by flowchart Ask for dollars start Read Dollars Ask for rate Read rate Compute euros Print euros end

Let’s start coding – setup #1 Create a new project in BlueJ Create a new class Write the standard new World starter: public class DollarsToEuroConverter { public static void main(String[ ] args) { }

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 you */ Public class DollarsToEurosConverter

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 you */ Import java.util.scanner Public class DollarsToEurosConverter

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?”); }

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);

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

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 variable eurosPerDollar = kbd.nextDouble(); -> gets the input from the user and puts it into eurosPerDollar

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

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

Check test data Possible inputs and outputs: Input Dollar Input euros per dollar Rate Output Euros

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

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

Some Java Statements (continued)

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);

Your toolset Basic setup including comments and importing packages Print to screen with formatting Read from screen Create variables to hold decimal values Calculate (*,/,+,-,^)