Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.

Slides:



Advertisements
Similar presentations
Computer Programming Lab(7).
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-3: Interactive Programs w/
Introduction to Computer Programming Looping Around Loops I: Counting Loops.
Introduction to Computer Programming Decisions If/Else Booleans.
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.
Chapter 2: Java Fundamentals Input and Output statements.
Introduction to Computer Programming Decisions, Decisions: Boolean Expressions and Selection.
Introduction to Computer Programming Loops N Decisions If/Else Counting Loops.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
Java Variables and Expressions CSC160 Professor Pepper (presentation adapted from Dr. Siegfried)
Introduction to Computer Programming Looping Around Loops II : Conditional Loops.
Introduction to Computer Programming CSC171 – 001 Getting Started – Chapters 1 & 2 Professor Pepper (presentation adapted from Dr. Siegfried)
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
Computer Programming Lab(4).
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Computer Programming Lab(5).
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
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.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to programming in java
CompSci 230 S Programming Techniques
User Input ICS2O.
Building Java Programs
Chapter 2 Clarifications
CSC111 Quick Revision.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Building Java Programs
Repetition.
Topic 11 Scanner object, conditional execution
Something about Java Introduction to Problem Solving and Programming 1.
The for-loop and Nested loops
Introduction to Classes and Methods
A+ Computer Science INPUT.
Building Java Programs
Introduction to Computer Programming
Building Java Programs
Building Java Programs
Chapter 2: Java Fundamentals
Building Java Programs
Building Java Programs
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
Random Numbers while loop
Building Java Programs
Building Java Programs
Optional Topic: User Input with Scanner
Presentation transcript:

Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried

The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come from the keyboard. To read in a value, we need to use an object belonging to a class called Scanner: Scanner keyb = new Scanner(System.in);

Reading from the keyboard Once we declare keyb as Scanner, we can read integer values by writing: variable = keyb.nextInt();

Writing the input statements in Average3b We can read in a value by writing: System.out.println ("What is the first value\t?"); int value1 = keyb.nextInt(); System.out.println ("What is the second value\t?"); int value2 = keyb.nextInt(); System.out.println ("What is the third value\t?"); int value3 = keyb.nextInt(); 2.Add the values 3. Divide the sum by 3 4.Print the result

Writing the assignments statements in Average3b System.out.println ("What is the first value\t?"); int value1 = keyb.nextInt(); System.out.println ("What is the second value\t?"); int value2 = keyb.nextInt(); System.out.println ("What is the third value\t?"); int value3 = keyb.nextInt(); sum = value1 + value2 + value3; 3. Divide the sum by 3 4.Print the result Adding up the three values

Writing the assignments statements in Average3b System.out.println ("What is the first value\t?"); int value1 = keyb.nextInt(); System.out.println ("What is the second value\t?"); int value2 = keyb.nextInt(); System.out.println ("What is the third value\t?"); int value3 = keyb.nextInt(); sum = value1 + value2 + value3; average = sum / 3; 4.Print the result Calculating the average

Writing the output statement in Average3b System.out.println ("What is the first value\t?"); int value1 = keyb.nextInt(); System.out.println ("What is the second value\t?"); int value2 = keyb.nextInt(); System.out.println ("What is the third value\t?"); int value3 = keyb.nextInt(); sum = value1 + value2 + value3; average = sum / 3; System.out.println("The average is " + average);

import java.util.Scanner; public class Average3b { public static void main(String[] args) { int sum, average; Scanner keyb = new Scanner(System.in); System.out.println ("What is the first value\t?"); int value1 = keyb.nextInt(); System.out.println ("What is the second value\t?"); int value2 = keyb.nextInt();

System.out.println ("What is the third value\t?"); int value3 = keyb.nextInt(); sum = value1 + value2 + value3; average = sum / 3; System.out.println("The average is " + average); }

Another example – calculating a payroll We are going to write a program which calculates the gross pay for someone earning an hourly wage. We need two pieces of information: –the hourly rate of pay –the number of hours worked. We are expected to produce one output: the gross pay, which we can find by calculating: –Gross pay = Rate of pay * Hours Worked

Examples Pay RateHoursGross $6.7510$67.50 $ $10040$4000

Our Design for payroll 1.Get the inputs 2.Calculate the gross pay 3.Print the gross pay 1.1 Get the rate 1.2 Get the hours We can substitute:

Developing The Payroll Program 1.1 Get the rate 1.2 Get the hours 2.Calculate the gross pay 3.Print the gross pay 1.1.1Prompt the user for the rate 1.1.2Read the rate 1.2.1Prompt the user for the hours 1.2.2Read the hours We can substitute

Coding the payroll program Before we code the payroll program, we recognize that the values ( rate, hours and gross ) may not necessarily be integers. We will declare these to be double, which means that they can have (but do not have to have) fractional parts. In Java, we usually declare our variables where they first appear in the program.

Developing The Payroll Program (continued) 1.1.1Prompt the user for the rate 1.1.2Read the rate 1.2.1Prompt the user for the hours 1.2.2Read the hours 2.Calculate the gross pay 3.Print the gross pay System.out.println("What is your hourly pay rate?"); double rate = keyb.nextDouble();

Developing The Payroll Program (continued) System.out.println ("What is your hourly pay rate?"); double rate = keyb.nextDouble(); 1.2.1Prompt the user for the hours 1.2.2Read the hours 2.Calculate the gross pay 3.Print the gross pay System.out.println("How many hours did you work?"); double hours = keyb.nextDouble();

Developing The Payroll Program (continued) System.out.println ("What is your hourly pay rate?"); double rate = keyb.nextDouble(); System.out.println ("How many hours did you work?"); double hours = keyb.nextDouble(); 2.Calculate the gross pay 3.Print the gross pay double gross = rate * hours;

Developing The Payroll Program (continued) System.out.println ("What is your hourly pay rate?"); double rate = keyb.nextDouble(); System.out.println ("How many hours did you work?"); double hours = keyb.nextDouble(); double gross = rate * hours; 3.Print the gross pay System.out.println("Your gross pay is $ " + gross);

import java.util.Scanner; public class Payroll { public static void main(String[] args) { Scanner keyb = new Scanner(System.in); System.out.println ("What is your hourly pay rate?"); double rate = keyb.nextDouble(); System.out.println ("How many hours did you work?"); double hours = keyb.nextDouble(); double gross = rate * hours; System.out.println("Your gross pay is $“ + gross); }

import java.util.Scanner; public class Payroll { /** This program calculates the gross pay for an * hourly worker * Inputs - hourly rate and hours worked * Output - Gross pay */ public static void main(String[] args) { Scanner keyb = new Scanner(System.in); // Get the hourly rate System.out.println ("What is your hourly pay rate?"); double rate = keyb.nextDouble();

// Get the hours worked System.out.println ("How many hours did you work?"); double hours = keyb.nextDouble(); // Calculate and display the gross pay double gross = rate * hours; System.out.println("Your gross pay is $" + gross); }

Using Stepwise Refinement to Design a Program You should noticed that when we write a program, we start by describing the steps that our program must perform and we subsequently refine this into a long series of more detailed steps until we are writing individual steps. This is called stepwise refinement. Stepwise refinement is one of the most basic methods for developing a program.

Example – A program to convert pounds to kilograms Our program will convert a weight expressed in pounds into kilograms. –Our input is the weight in pounds. –Our output is the weight in kilograms –We also know that Kilograms = Pounds / 2.2

Examples for pounds to kilograms Weight in pounds (int)Weight in kilograms

Pounds to Kilograms Program (continued) Our program must: 1.Get the weight in pounds 2.Calculate the weight in kilograms 3.Print the weight in kilograms

Pounds to Kilograms Program (continued) Our program must: 1.Get the weight in pounds 2.Calculate the weight in kilograms 3.Print the weight in kilograms 1.1 Prompt the user for the weight in pounds 1.2Read the pounds

Pounds to Kilograms Program (continued) Our program must: 1.1 Prompt the user for the weight in pounds 1.2 Read the pounds 2. Calculate the weight in kilograms 3. Print the weight in kilograms System.out.println ("What is the weight in pounds?"); double lbs = keyb.nextInt();

Pounds to Kilograms Program (continued) System.out.println ("What is the weight in pounds?"); double lbs = keyb.nextInt(); 2. Calculate the weight in kilograms 3. Print the weight in kilograms double kg = lbs / 2.2;

Pounds to Kilograms Program (continued) System.out.println ("What is the weight in pounds?"); double lbs = keyb.nextInt(); double kg = lbs / 2.2; 3. Print the weight in kilograms System.out.println("The weight is " + kg + " kilograms");

import java.util.Scanner; public class ConvertPounds { // Convert pounds to kilograms // Input - weight in pounds // Output - weight in kilograms public static void main(String[] args) { Scanner keyb = new Scanner(System.in); // Get the weight in pounds System.out.println ("What is the weight in pounds?"); double lbs = keyb.nextInt(); // Calculate and display the weight in // kilograms double kg = lbs / 2.2; System.out.println("The weight is " + kg + " kilograms"); }

Another Example – The Area of A Rectangle Our program will calculate the area of a rectangle. –Our input is the length and width. –Our output is the area. –We also know that Area = Length * Width 0 = 0 * = 20 * = 100 * 3

Our Program’s Steps 1.Find the length and width 2.Calculate the area 3.Print the area

Our Program’s Steps (continued) 1.Find the length and width 2.Calculate the area 3.Print the area 1.1 Find the length 1.2 Find the width

Our Program’s Steps (continued) 1.1 Find the length 1.2 Find the width 2. Calculate the area 3. Print the area Prompt the user for the length Read the length Prompt the user for the width Read the width

Our Program’s Steps (continued) Prompt the user for the length Read the length Prompt the user for the width Read the width 2. Calculate the area 3. Print the area System.out.println("Enter the length?"); double length = keyb.nextDouble(); System.out.println("Enter the width?"); double width = keyb.nextDouble();

Our Program’s Steps (continued) System.out.println("Enter the length?"); double length = keyb.nextDouble(); System.out.println("Enter the width?"); double width = keyb.nextDouble(); 2. Calculate the area 3. Print the area double area = length * width;

Our Program’s Steps (continued) System.out.println("Enter the length?"); double length = keyb.nextDouble(); System.out.println("Enter the width?"); double width = keyb.nextDouble(); double area = length * width; 3. Print the area System.out.println("The area is " + area);

import java.util.Scanner; public class CalculateArea { // Calculates the area of a rectangle // Inputs - The length and width of the rectangle // Output - The area of the rectangle public static void main(String[] args) { Scanner keyb = new Scanner(System.in); // Print an explanatory message for the user System.out.println ("Given the width and length of a rectangle"); System.out.println ("this program calculates its area." );

// Get the inputs System.out.println("Enter the length?"); double length = keyb.nextDouble(); System.out.println("Enter the width?"); double width = keyb.nextDouble(); // Calculate and display the area double area = length * width; System.out.println("The area is " + area); }

More on Scanner You can read: –nextInt() –nextLong() –nextByte() –nextDouble() –next() – up to next whitespace (delimiter) –nextLine() – up to “\n” –useDelimiter() Throw in nextLine() to get down a line

TryScanner Tell the user to “Type an integer and then a word, and press Enter” Print it back to them with “You typed and.” Then, ask for a whole line and print it back. See that you need to be careful with the Enter keystroke. (Capture it with keyb.nextLine.)

Scanner Play solution import java.util.Scanner; public class ScannerPlay { public static void main(String[] args) { Scanner keyb = new Scanner(System.in); System.out.println ("Type an integer and then a word, and press Enter"); int number1 = keyb.nextInt(); String word1 = keyb.next(); System.out.println("You typed " + number1 + " and " + word1 + "."); System.out.println("Type something else and Enter"); keyb.nextLine(); // skip a line String line1 = keyb.nextLine(); System.out.println("You typed " + line1); }