Input. Scanner keyboard = new Scanner(System.in); String name;

Similar documents
A+ Computer Science -

Computational Expression

A+ Computer Science -

Programming with Java

Fundamentals of Programming Data Types & Methods

Basic Computation. Chapter 2

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Reading Input from Text File

Lecture 8 " INPUT " Instructor: Craig Duckett

Full file at

Basic Computation. Chapter 2

Peer Instruction 1. Elementary Programming

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Basic Computation. Chapter 2

Introduction to Java Unit 1. Using BlueJ to Write Programs

AP Computer Science Unit 1. Writing Programs Using BlueJ

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

AP Computer Science Unit 1. Programs

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Computational Expression

I. Variables and Data Type week 3

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

AP Computer Science Unit 1. Writing Programs Using BlueJ

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

ing execution. That way, new results can be computed each time the Class The Scanner

Software Practice 1 Basic Grammar

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Chapter 2: Basic Elements of Java

St. Edmund Preparatory High School Brooklyn, NY

CS 302: Introduction to Programming

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Full file at

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CEN 414 Java Programming

Text User Interfaces. Keyboard IO plus

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

File I/O Array Basics For-each loop

Chapter 3 Lab Decision Structures

Basics of Java Programming

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Data Types and the while Statement

Anatomy of a Java Program: Comments

Using Java Classes Fall 2018 Margaret Reid-Miller

Chapter 2: Data and Expressions

Using Classes. GEEN163 Introduction to Computer Programming

COMP String and Console I/O. Yi Hong May 18, 2015

Objects and Classes 1: Encapsulation, Strings and Things CSC 121 Fall 2014 Howard Rosenthal

double float char In a method: final typename variablename = expression ;

Task #1 The if Statement, Comparing Strings, and Flags

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

A token is a sequence of characters not including any whitespace.

Elementary Programming

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Basic Programming Elements

CSIS 10A Assignment 3 Due: 2/21 at midnight

Ex: If you use a program to record sales, you will want to remember data:

CSS 161 Fundamentals of Compu3ng. Assignments, Expressions, Operators, Console input & output October 1, 2012

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

Chapter 2: Data and Expressions

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Topic 11 Scanner object, conditional execution

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Object Oriented Programming. Java-Lecture 1

COMP 202 Java in one week

Formatted Output / User IO

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Object-Oriented Programming in Java

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

COMP 202. Java in one week

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

Robots. Byron Weber Becker. chapter 6

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Important Java terminology

Chapter 1 Lab Algorithms, Errors, and Testing

Data Conversion & Scanner Class

AP Computer Science A

4. If the following Java statements are executed, what will be displayed?

Chapter 4: Conditionals and Recursion

Lecture Set 2: Starting Java

CSIS 10A Assignment 4 SOLUTIONS

1 Short Answer (10 Points Each)

13 th Windsor Regional Secondary School Computer Programming Competition

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Lecture Set 2: Starting Java

Chapter. Let's explore some other fundamental programming concepts

Java Input/Output. 11 April 2013 OSU CSE 1

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Transcription:

Reading Resource Input Read chapter 4 (Variables and Constants) in the textbook A Guide to Programming in Java, pages 77 to 82. Key Concepts A stream is a data channel to or from the operating system. The standard input stream in Java is System.in Use the Scanner class has methods for inputting data from the console window import java.util.*; public class InputExample1 { public static void main(string[] args) { Scanner keyboard = new Scanner(System.in); String name; // prompt the user for their name System.out.println("What is your name? "); // input a word from the keyboard name = keyboard.nextline(); // output a greeting System.out.println("Hello " + name); } // end main method } // end class Example4 Notes about importing packages The import statement makes the classes of a package accessible to an application. The following statement imports the one class (Scanner) to your application import java.util.scanner; Use an asterisk (*) so all members of the util package are accessible Import statements are placed before the class definition Lesson 3 (Input) Unit 1 1

The Scanner class To input what you type on the keyboard, your program needs to connect to the hardware through the operating system. This is done with a class (a blueprint for a program) that is included with the Java, the Scanner class. Add this line to your programs to enable keyboard input. Scanner keyboard = new Scanner(System.in); This statement instructs the computer to create an object (keyboard in this example) and initialize it with the input stream called System.in Remember that in previous examples output was displayed on the monitor, which is the output stream called System.out, with the println method. Terminology I n p u t s t r e a m : a sequence of characters received from an input device. Cla s s : a blueprint for an abject O b j e c t : a named entity that is made of related data and instructions for performing actions on that data. M e t h o d : a named set of statements that performs a simple, well-defined task. Methods in the Scanner class A method is like a subprogram inside the class with a special purpose. The Scanner class has many methods for obtaining a value from the user. Look in your textbook, A Guide to Programming in Java, on page 81 and copy the list of methods into the chart below. Method name Description Lesson 3 (Input) Unit 1 2

Here is another example program: //********************************************************* // * Program Title: Input Example 2 // * Programmer: Mr. Young // * Course: ICS 4U1 // * Date: 09/08/2014 // * Filename: InputExample2.java // * Description: demonstrates different methods of // * the Scanner class // *********************************************************** import java.util.*; public class InputExample2 { public static void main(string[] args) { Scanner keyboard = new Scanner(System.in); String name; int age; // prompt the user for their name System.out.println("What is your name? "); // input a line of text from the keyboard name = keyboard.nextline(); // prompt the user for their age System.out.println("How old are you? "); // input an integer number from the keyboard age = keyboard.nextint(); // output an extended greeting System.out.println("\n\tHello " + name + "."); System.out.println("\tI see that you are " + age + " years old.\n"); } // end main method } // end class ProgramExample5 Notes on Example # 5 You must reserve a location in memory for the data in your program. There are a few fundamental types of data that you will see in your programs. Here are some standard data types: int An integer data type is a whole number (no decimals; ex: 6, 127, or 9358) double If your program will use decimal numbers such as 3.1415 or 0.002, then you must tell the computer that the variable needs this kind of storage. Note: in some textbooks and program examples float is used instead of double. This is OK since double is just twice the memory capacity of a float variable char This designates one byte of memory for a single Unicode character. String For a whole word or a whole sentence to be stored in a single variable, use the type String. Notice that this data type begins with a capital. Lesson 3 (Input) Unit 1 3

The Scanner class: there are many methods (commands) that you can use with an object created from the Scanner class. This statement creates an object called get built with the Scanner "blueprint": Scanner get = new Scanner(System.in); Use the next() method to input one word (delimited by spaces), for example: String first_name; first_name = get.next(); Use the nextline() method to input an entire line of text (delimited by the Enter key), for example: String sentence; sentence = get.next(); Use the nextint() method to input one integer, for example: int number; number = get.nextint(); Use the nextdouble() method to input one "floating point" number, which is a number with a fractional part (a decimal), for example: double price; price = get.nextdouble(); Rules for Naming Variables in Java A variable name may be very long (up to 256 characters) but it is more practical to keep them short (example: name rather than TheLastNameOfMyClient) A variable name must begin with a letter A variable name may contain the digits 0-9. (ex: letter1, letter2, letter3) DO NOT use spaces (bad example: size in sq meters) Separate words in a variable name with the underscore _ character (example: size_in_sq_meters) Java is case sensitive so Size_In_SqMeters is different than size_in_sqmeters Do Not use key-words as variables (such as int, float, char, and so on) Examples Legal variable names Illegal variable names total Price 4names $letter Sentence My_Name File-name Total Price alongvariablename sales_tax char account#1 COUNT number A long variable name. Lesson 3 (Input) Unit 1 4

Activity # 7 Modify the example code so that the program asks for the user s full name (first name and last name) and their telephone number (Area Code, Exchange, and Number) and then displays the output similar to this sample: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Lesson 3 (Input) Unit 1 5

Activity # 8 The height of an object at any given time dropped from a starting height of 100 meters is given by the equation h=100-4.9*t 2 where t is the time in seconds. Create an ObjectHeight application that prompts the user for a time less than 4,5 seconds and then displays the height of the object at that time. The application output should look similar to: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Lesson 3 (Input) Unit 1 6