Formatted Output / User IO

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

Introduction to Java Applications

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

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

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

Simple Java Reference

COMP 202 Java in one week

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

COMP 202. Java in one week

Object Oriented Programming. Java-Lecture 1

Fundamentals of Programming Data Types & Methods

Object-Oriented Programming in Java

Chapter 11 Introduction to Programming in C

Computational Expression

Full file at

Programming with Java

COMP 202 Java in one week

Department of Computer Science Purdue University, West Lafayette

AP Computer Science Unit 1. Writing Programs Using BlueJ

CS 106 Introduction to Computer Science I

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

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

AP Computer Science Unit 1. Programs

Introduction to Java Unit 1. Using BlueJ to Write Programs

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Lab 9: Creating a Reusable Class

CC112 Structured Programming

Reading Input from Text File

AP Computer Science Unit 1. Writing Programs Using BlueJ

Chapter 11 Introduction to Programming in C

CEN 414 Java Programming

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

Chapter 2: Review Exercise Solutions R2.1

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

Nested Loops. A loop can be nested inside another loop.

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Elementary Programming

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Question 2. [5 points] Given the following symbolic constant definition

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

Object-Based Programming. Programming with Objects

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

A+ Computer Science -

COMP102: Test 1 Model Solutions

Supplementary Test 1

Topics. The Development Process

Measurement and Geometry: Area and Volume of Geometric Figures and Objects *

Basic Programming Elements

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Full file at

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

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

WARM UP LESSONS BARE BASICS

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

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

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

2.8. Decision Making: Equality and Relational Operators

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

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

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

Introduction to Java Applications

2. Numbers In, Numbers Out

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

Use a calculator and c = 2 π r to calculate the circumference of a circle with a radius of 1.0.

Course Outline. Introduction to java

Chapter 2 Elementary Programming

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Computational Expression

PROGRAMMING IN C AND C++:

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

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

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

A+ Computer Science -

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

2.5 Another Application: Adding Integers

Computer Science 210 Data Structures Siena College Fall Topic Notes: Methods

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

Lecture 8 " INPUT " Instructor: Craig Duckett

COMP102: Test 2: Part B Model Solutions

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

Full file at

Chapter 2 ELEMENTARY PROGRAMMING

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.

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

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

Chapter 11 Introduction to Programming in C

Exercise: Using Numbers

4. Java Project Design, Input Methods

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Chapter 4: Loops and Files

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

Activity 1: Introduction

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Chapter 4: Loops and Files

Chapter 5 Lab Methods

b. Suppose you enter input from the console, when you run the program. What is the output?

We now start exploring some key elements of the Java programming language and ways of performing I/O

Transcription:

/ User IO 1 / 22 / User IO COMP SCI / SFWR ENG 2S03 Natalie Perna Dylan Aspden Department of Computing and Software McMaster University Week 4: Sept 29 - Oct 3 Natalie Perna, Dylan Aspden / User IO 1 / 22

/ User IO 2 / 22 Outline Outline 1 Resource 2 printf() Format Specifications Using printf() for Tabular Output 3 Reading from Keyboard Scanner Class Reading from Keyboard Remarks Natalie Perna, Dylan Aspden / User IO 2 / 22

/ User IO 3 / 22 Resource Resource All of the following material is adapted from: Mughal, Khalid. Java Actually : A Comprehensive Primer in Programming. Australia: Course Technology/Cenage Learning, 2008. Natalie Perna, Dylan Aspden / User IO 3 / 22

/ User IO 4 / 22 printf() printf() printf ( String format, Object... args ) format: Contains format specifications Determines how each subsequent value in the parameter args will be formatted and printed Object... args: Method accepts zero or more parameters Natalie Perna, Dylan Aspden / User IO 4 / 22

/ User IO 5 / 22 Format Specifications Format Specifications I Table 2.2 Format specifications in Java (Page 32) Parameter value Format specification Example value String printed Comment Integer "%d" 125 "125" Occupies as many character places as needed "%6d" 125 " 125" Occupies six character places and is right-justified. The printed string is padded with spaces to the left. "%02d" 3 "03" Occupies two character places and is padded with leading zeros. Natalie Perna, Dylan Aspden / User IO 5 / 22

/ User IO 6 / 22 Format Specifications Format Specifications II Parameter value Format specification Example value String printed Comment Floating point value "%f" 16.746 "16.746000" Occupies as many character places as needed, but always includes six decimal places "%.2f" 16.746 "16.75" Occupies as many character places as needed, but includes only two decimal places. "%8.2f" 16.7466 " 16.75" Occupies eight character places, including the decimal point, and uses two decimal places. Natalie Perna, Dylan Aspden / User IO 6 / 22

/ User IO 7 / 22 Format Specifications Format Specifications III Parameter value Format specification Example value String printed Comment String "%s" "Hi!" "Hi!" Occupies as many character places as are needed. "%12s" "Hi Dude!" " Hi Dude!" Occupies twelve character places and is right-justified. "%-12s" "Hi Dude!" "Hi Dude! " Occupies twelve character places and is left-justified. Linefeed "%n" (none) (none) Natalie Perna, Dylan Aspden / User IO 7 / 22

/ User IO 8 / 22 Using printf() for Tabular Output Using printf() for Tabular Output The following calls to the printf() method: System.out. printf (" Player \\ Game %6d%6d%6d%n", 1, 2, 3); System.out. printf ("% -20s%6d%6d%6d%n", "F. Reshmann ", 320, 160, 235) ; System.out. printf ("% -20s%6d%6d%6d%n", "A. King ", 1250, 1875, 2500) ; will generate this tabular printout of game results: Player \ Game 1 2 3 F. Reshmann 320 160 235 A. King 1250 1875 2500 Natalie Perna, Dylan Aspden / User IO 8 / 22

/ User IO 9 / 22 Use the System.out.printf() method to print the following values: a) A six-digit integer, including the sign, e.g. 123456 as +123456. b) The floating-point value 123456789.3837 in scientific notation, i.e. as 1.234568e+08. c) The string "We aere 100% motivated to learn Java!". d) The number 1024 as a right-justified eight-digit integer, i.e. as 00001024. Natalie Perna, Dylan Aspden / User IO 9 / 22

/ User IO 10 / 22 a) A six-digit integer, including the sign, e.g. 123456 as +123456. Natalie Perna, Dylan Aspden / User IO 10 / 22

/ User IO 10 / 22 a) A six-digit integer, including the sign, e.g. 123456 as +123456. System. out. printf (" %+6 d", 123456) ; +123456 Natalie Perna, Dylan Aspden / User IO 10 / 22

/ User IO 11 / 22 b) The floating-point value 123456789.3837 in scientific notation, i.e. as 1.234568e+08. Natalie Perna, Dylan Aspden / User IO 11 / 22

/ User IO 11 / 22 b) The floating-point value 123456789.3837 in scientific notation, i.e. as 1.234568e+08. System. out. printf ("%e", 123456789.3837) ; 1.234568 e +08 Natalie Perna, Dylan Aspden / User IO 11 / 22

/ User IO 12 / 22 c) The string "We are 100% motivated to learn Java!". Natalie Perna, Dylan Aspden / User IO 12 / 22

/ User IO 12 / 22 c) The string "We are 100% motivated to learn Java!". System. out. printf ("%s%d %%% s", "We are ", 100, to learn Java!"); " motivated We are 100% motivated to learn Java! Natalie Perna, Dylan Aspden / User IO 12 / 22

/ User IO 13 / 22 d) The number 1024 as a right-justified eight-digit integer, i.e. as 00001024. Natalie Perna, Dylan Aspden / User IO 13 / 22

/ User IO 13 / 22 d) The number 1024 as a right-justified eight-digit integer, i.e. as 00001024. System. out. printf (" %08 d", 1024) ; 00001024 Natalie Perna, Dylan Aspden / User IO 13 / 22

/ User IO 14 / 22 Reading from Keyboard Scanner Class Scanner Class Class from the Java standard library which provides the ability to read numbers and strings from the keyboard Must first tell the Java compiler that we wish to use the class Done through an import statement import java. util. Scanner ; Natalie Perna, Dylan Aspden / User IO 14 / 22

/ User IO 15 / 22 Reading from Keyboard Scanner Class Scanner Class: Methods Scanner class provides a number of methods for retrieving input from the keyboard Scanner keyboard = new Scanner ( System. in); int i = keyboard. nextint (); double d = keyboard. nextdouble (); String s = keyboard. next (); With these methods, we can take input from a keyboard, store it inside our program, and finally use it in our computations Natalie Perna, Dylan Aspden / User IO 15 / 22

/ User IO 16 / 22 Reading from Keyboard Reading from Keyboard Reading from Keyboard: Example #1 import java. util. Scanner ; public class IntegerReader { public static void main ( String [] args ) { Scanner keyboard = new Scanner ( System. in); System. out. print (" Enter an integer : "); int numberread = keyboard. nextint (); System. out. printf (" You entered : %d%n", numberread ); } } Program output : Enter an integer : 123 You entered : 123 Natalie Perna, Dylan Aspden / User IO 16 / 22

/ User IO 17 / 22 Reading from Keyboard Reading from Keyboard Reading from Keyboard: Example #2 import java. util. Scanner ; public class FloatingPointArea { public static void main ( String [] args ) { Scanner keyboard = new Scanner ( System. in); System. out. print (" Enter dimensions (l, w): "); double length = keyboard. nextdouble (); System. out. print (" "); double breadth = keyboard. nextdouble (); double area = length * breadth ; } } System. out. printf ( "A rectangle of length %.2 f cm and breadth %.2 f cm" + " has area %.2 f sq. cm. %n", length, breadth, area ); Natalie Perna, Dylan Aspden / User IO 17 / 22

/ User IO 18 / 22 Reading from Keyboard Reading from Keyboard Reading from Keyboard: Example #2 Program output : Enter dimensions (l, w): 15.5 4.25 A rectangle of length 15.50 cm and breadth 4.25 cm has area 65.88 sq. cm. Natalie Perna, Dylan Aspden / User IO 18 / 22

/ User IO 19 / 22 Reading from Keyboard Remarks Remarks Best Practice Provide meaningful prompts to make the use of the program self-explanatory. The prompt should be short, enabling a user to enter the required value on the same line. (Mughal, 2008) Natalie Perna, Dylan Aspden / User IO 19 / 22

/ User IO 20 / 22 Reading from Keyboard Exercise #1 Write a program that calculates the area a and circumference c of a circle: a = p r 2 c = 2 p r where p is a mathematical constant, and r is the radius of the circle. The square of the radius can be calculated by multiplying r with itself. Assume a value of 3.1415927 for p (π), which should be defined as a constant in your program. Natalie Perna, Dylan Aspden / User IO 20 / 22

/ User IO 21 / 22 Reading from Keyboard Exercise #2 Extend the program in Exercise #1 to read 3 radii into your program, then print out a formatted table with a header row and three columns: the radius, the area and the circumference, of each circle. Natalie Perna, Dylan Aspden / User IO 21 / 22

/ User IO 22 / 22 Reading from Keyboard Exercise #3 Extend the program in Exercise #1 to read the units measurement for the radius, e.g. cm, m, or km, as a string. You can use the nextline() / next() methods for this purpose. Now run the program to calculate the circumference of the Earth at equator, when the equatorial radius is 6378.135 km. Natalie Perna, Dylan Aspden / User IO 22 / 22