BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Similar documents
The Command Shell, Libraries and Clients, Formatted Printing. Fundamentals of Computer Science

Sta$cs and forma.ng numbers

Basic Computation. Chapter 2

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Libraries, clients, prin/

Basic Computation. Chapter 2

Basic Computation. Chapter 2

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

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

STUDENT LESSON A7 Simple I/O

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Objectives. Primitive Types, Strings, and Console I/O. Variables and Values. Outline. Naming and Declaring Variables. Variables and Values

Full file at

Java Console Input/Output The Basics. CGS 3416 Spring 2018

Console Input and Output

A+ Computer Science -

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

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

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Simple Java Input/Output

Java Console Input/Output The Basics

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Programming with Java

Introduction to Java Unit 1. Using BlueJ to Write Programs

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

Programming Language Basics

A+ Computer Science -

Formatted Output (printf) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

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

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

Basic Programming Elements

2.8. Decision Making: Equality and Relational Operators

Primitive Data, Variables, and Expressions; Simple Conditional Execution

AP Computer Science Unit 1. Writing Programs Using BlueJ

Object Oriented Programming. Java-Lecture 1

Full file at

Chapter 4: Conditionals and Recursion

Lecture Set 2: Starting Java

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

Lecture Set 2: Starting Java

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

CS 302: Introduction to Programming

Section 2.2 Your First Program in Java: Printing a Line of Text

AP Computer Science Unit 1. Writing Programs Using BlueJ

Introduction to Java Applications; Input/Output and Operators

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

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

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

Introduction to Java Applications

Lecture 8 " INPUT " Instructor: Craig Duckett

Introduction to Java & Fundamental Data Types

Section 2: Introduction to Java. Historical note

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Using Java Classes Fall 2018 Margaret Reid-Miller

Arithmetic and IO. 25 August 2017

Midterm Examination (MTA)

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

FUNDAMENTAL DATA TYPES

Topic 12 more if/else, cumulative algorithms, printf

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Chapter 5 Lab Methods

Topic 12 more if/else, cumulative algorithms, printf

2.5 Another Application: Adding Integers

Formatted Output Pearson Education, Inc. All rights reserved.

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

Computational Expression

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

5) (4 points) What is the value of the boolean variable equals after the following statement?

Section 2.2 Your First Program in Java: Printing a Line of Text

Chapter 5 Lab Methods

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

Full file at

CS111: PROGRAMMING LANGUAGE II

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Building Java Programs

printf( Please enter another number: ); scanf( %d, &num2);

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

AP Computer Science Unit 1. Programs

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

CS110: PROGRAMMING LANGUAGE I

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

1 Short Answer (10 Points Each)

4. Java language basics: Function. Minhaeng Lee

Introduction to Computer Science Unit 2. Exercises

Full file at

CEN 414 Java Programming

Object-Oriented Programming in Java

Chapter 5 Lab Methods

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Formatted Output / User IO

Primitive Data Types: Intro

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

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

Topics. The Development Process

Oct Decision Structures cont d

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

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Transcription:

BASIC INPUT/OUTPUT Fundamentals of Computer Science

Outline: Basic Input/Output Screen Output Keyboard Input

Simple Screen Output System.out.println("The count is " + count); Outputs the sting literal "the count is " Followed by the current value of the variable count. We've seen several examples of screen output already. System.out is an object that is part of Java. println() is one of the methods available to the System.out object.

Screen Output The concatenation operator (+) is useful when everything does not fit on one line. System.out.println("Lucky number = " + 13 + "Secret number = " + number); Do not break the line except before or after the concatenation operator (+).

Screen Output Alternatively, use print() System.out.print("One, two,"); System.out.print(" buckle my shoe."); System.out.println(" Three, four,"); System.out.println(" shut the door."); ending with a println().

FORMATTED PRINTING

Pretty Text Formatting printf-style formatting Common way to nicely format output Present in many programming languages Java, C++, Perl, PHP,... Use a special format language: Format string with special codes One or more variables get filled in In Java, used via: System.out.printf() output to standard out String.format() returns a formatted String 7

Floating-Point Formatting double d = 0.123456789; float f = 0.123456789f; // %f code is used with double or float variables // %f defaults to rounding to 6 decimal places System.out.printf("d is about %f\n", d); System.out.printf("f is about %f\n", f); d is about 0.123457 f is about 0.123457 \n means line feed // Number of decimal places specified by.x // Output is rounded to that number of places System.out.printf("PI is about %.1f\n", Math.PI); System.out.printf("PI is about %.2f\n", Math.PI); System.out.printf("PI is about %.3f\n", Math.PI); System.out.printf("PI is about %.4f\n", Math.PI); PI is about 3.1 PI is about 3.14 PI is about 3.142 PI is about 3.1416 // %e code outputs in scientific notation //.X specifies number of significant figures final double C = 299792458.0; System.out.printf("speed of light = %e\n", C); System.out.printf("speed of light = %.3e\n", C); C = 2.99792e+08 C = 2.998e+08 8

Integer Formatting // %d code is for integer values, int or long // Multiple % codes can be used in a single printf() long power = 1; for (int i = 0; i < 8; i++) { System.out.printf("%d = 2^%d\n", power, i); power = power * 2; } You can have multiple % codes that are filled in by a list of parameters to printf() // A number after the % indicates the minimum width // Good for making a nice looking tables of values power = 1; for (int i = 0; i < 8; i++) { System.out.printf("%5d = 2^%d\n", power, i); power = power * 2; } Minimum width of this field in the output. Java will pad with whitespace to reach this width (but can exceed this width if necessary). 1 = 2^0 2 = 2^1 4 = 2^2 8 = 2^3 16 = 2^4 32 = 2^5 64 = 2^6 128 = 2^7 1 = 2^0 2 = 2^1 4 = 2^2 8 = 2^3 16 = 2^4 32 = 2^5 64 = 2^6 128 = 2^7 9

Flags // Same table, but left justify the first field power = 1; for (int i = 0; i < 8; i++) { System.out.printf("%-5d = 2^%d\n", power, i); power = power * 2; } - flag causes this field to be left justified // Use commas when displaying numbers power = 1; for (int i = 0; i < 17; i++) { System.out.printf("%,5d = 2^%d\n", power, i); power = power * 2; }, flag causes commas between groups of 3 digits 1 = 2^0 2 = 2^1 4 = 2^2 8 = 2^3 16 = 2^4 32 = 2^5 64 = 2^6 128 = 2^7 1 = 2^0 2 = 2^1 4 = 2^2 8 = 2^3 16 = 2^4 32 = 2^5 64 = 2^6 128 = 2^7 256 = 2^8 512 = 2^9 1,024 = 2^10 2,048 = 2^11 4,096 = 2^12 8,192 = 2^13 16,384 = 2^14 32,768 = 2^15 65,536 = 2^16 10

Text Formatting // Characters can be output with %c, strings using %s String name = "Bill"; char grade = 'B'; System.out.printf("%s got a %c in the class.\n", name, grade); Bill got a B in the class. // This prints the same thing without using printf System.out.println(name + " got a " + grade + " in the class."); An equivalent way to print the same thing out using good old println(). 11

Creating Formatted Strings Formatted String creation You don't always want to immediately print formatted text to standard output Save in a String variable for later use // Formatted Strings can be created using format() String lines = ""; for (int i = 0; i < 4; i++) lines += String.format("Random number %d = %.2f\n", i, Math.random()); System.out.print(lines); Random number 0 = 0.54 Random number 1 = 0.50 Random number 2 = 0.39 Random number 3 = 0.64 12

The Format Specifier Minimum number of character used, but if number is longer it won't get cut off % [flags][width][.precision]type Special formatting options like inserting commas, making left justified, etc. Sets the number of decimal places, don't forget the. Type is the only required part of specifier. "d" for an integer, "f" for a floating-point number %[flags][width][.precision]type System.out.printf("%,6.1f", 42.0); 13

printf Gone Bad Format string specifies: Number of variables to fill in Type of those variables With great power comes great responsibility Format must agree with #/types of arguments Runtime error otherwise Compiler / Eclipse won't protect you // Runtime error %f expects a floating-point argument System.out.printf("crash %f\n", 1); // Runtime error, %d expects an integer argument System.out.printf("crash %d\n", 1.23); // Runtime error, not enough arguments System.out.printf("crash %d %d\n", 2); 14

printf Puzzler Code System.out.printf("%f", 4242.00); System.out.printf("%d", 4242); System.out.printf("%.2f", 4242.0); System.out.printf("%.3e", (double) 4242); System.out.printf("%,d", 4242); Letter E A B C D Letter Output A 4242 B 4242.00 C 4.242e+03 D 4,242 E 4242.000000 Code # System.out.printf("%d%d", 42, 42); System.out.printf("%d+%d", 42, 42); System.out.printf("%d %d", 42); System.out.printf("%8d", 42); System.out.printf("%-8d", 42); System.out.printf("%d", 42.0); 2 1 5 3 4 5 # Output 1 42+42 2 4242 3 42 4 42 5 runtime error 15

Keyboard Input Java has reasonable facilities for handling keyboard input. These facilities are provided by the Scanner class in the java.util package. A package is a library of classes.

Simple Input Sometimes the data needed for a computation are obtained from the user at run time. Keyboard input requires import java.util.scanner at the beginning of the file.

Simple Input Data can be entered from the keyboard using Scanner keyboard = new Scanner(System.in); followed, for example, by eggsperbasket = keyboard.nextint(); which reads one int value from the keyboard and assigns it to eggsperbasket.

Using the Scanner Class Near the beginning of your program, insert import java.util.scanner; Create an object of the Scanner class Scanner keyboard = new Scanner (System.in) Read data (an int or a double, for example) int n1 = keyboard.nextint(); double d1 = keyboard,nextdouble(); Close the Scanner keyboard.close();

Some Scanner Class Methods

Some Scanner Class Methods Figure 2.7b

nextline()method Caution The nextline() method reads The remainder of the current line, Even if it is empty. Example given following declaration. int n; String s1, s2; n = keyboard.nextint(); s1 = keyboard.nextline(); s2 = keyboard.nextline(); Assume input shown 42 n is set to 42 but s1 is set to the empty string. and don't you forget it.

Outline: Basic Input/Output Screen Output Keyboard Input