Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
CS102--Object Oriented Programming Lecture 2: – the String class – Console Input/Output – Flow of control Copyright © 2008 Xiaoyan Li.
Chapter 2 Screen Output Section 2.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Java Applications & Program Design
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
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.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Programming
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Lecture 4 – Scanner & Style
More lO.
Input/Output.
Console Input and Output
Formatting Output & Enumerated Types & Wrapper Classes
Chapter 3 Java Input/Output.
Chapter 2 Screen Output Section 2.1
Console Input and Output
Comp 249 Programming Methodology
Comp 249 Programming Methodology
Program Style Console Input and Output
System.out.println for console output
Comp 249 Programming Methodology
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Comp 249 Programming Methodology
Chapter 3 Input/Output.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of Absolute Java 3 rd Edition by Savitch; which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is published by Pearson Education / Addison-Wesley. Copyright © 2007 Pearson Addison-Wesley Copyright © Aiman Hanna All rights reserved

System.out.println for console output Every invocation of println ends a line of output Every invocation of println ends a line of output System.out.println("The account has a balance of " + balance); 2-2

println Versus print Another method that can be invoked by the System.out object is print Another method that can be invoked by the System.out object is print The print method is like println, except that it does not end a line The print method is like println, except that it does not end a line 2-3 OutputFormatting1.java (MS-Word file) OutputFormatting1.java (MS-Word file) OutputFormatting1.javaMS-Word file OutputFormatting1.javaMS-Word file

Formatting Output with printf Starting with version 5.0, Java includes a method named printf that can be used to produce output in a specific format Starting with version 5.0, Java includes a method named printf that can be used to produce output in a specific format System.out.printf can have any number of arguments System.out.printf can have any number of arguments The first argument is always a format string that contains one or more format specifiers for the remaining arguments The first argument is always a format string that contains one or more format specifiers for the remaining arguments 2-4 OutputFormatting2.java (MS-Word file) OutputFormatting2.java (MS-Word file) OutputFormatting2.javaMS-Word file OutputFormatting2.javaMS-Word file

printf Format Specifier The code The code double price = 19.8; System.out.printf("Price is: %10.2f", price); System.out.println(" each"); will output the line will output the line Price is: each Price is: each 2-5

Right and Left Justification in printf Specifier such as %8.2 will left align the output Specifier such as %8.2 will left align the output If right alignment is needed, then a “ - ” sign is placed after the % of the specifier. If right alignment is needed, then a “ - ” sign is placed after the % of the specifier. System.out.printf("Price is: %-10.2f", price); OutputFormatting3.java (MS-Word file) OutputFormatting3.java (MS-Word file) OutputFormatting3.javaMS-Word file OutputFormatting3.javaMS-Word file 2-6

Multiple arguments with printf It is also possible to format different types with printf It is also possible to format different types with printf %n is used for new line %n is used for new line % is used to escape a specifier % is used to escape a specifier 2-7 OutputFormatting4.java (MS-Word file) OutputFormatting4.java (MS-Word file) OutputFormatting4.javaMS-Word file OutputFormatting4.javaMS-Word file

Format Specifiers for System.out.printf 2-8

Importing Packages and Classes Libraries in Java are called packages Libraries in Java are called packages A package is a collection of classes that is stored in a manner that makes it easily accessible to any program A package is a collection of classes that is stored in a manner that makes it easily accessible to any program In order to use a class that belongs to a package, the class must be brought into a program using an import statement In order to use a class that belongs to a package, the class must be brought into a program using an import statement Classes found in the package java.lang are imported automatically into every Java program Classes found in the package java.lang are imported automatically into every Java program import java.text.NumberFormat; // import the NumberFormat class only // import the NumberFormat class only import java.text.*; //import all the classes in package java.text //import all the classes in package java.text 2-9

Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing simple keyboard input named the Scanner class Starting with version 5.0, Java includes a class for doing simple keyboard input named the Scanner class In order to use the Scanner class, a program must include the following line near the start of the file: In order to use the Scanner class, a program must include the following line near the start of the file: import java.util.Scanner 2-10 InputScanner2.java (MS-Word file) InputScanner2.java (MS-Word file) InputScanner2.javaMS-Word file InputScanner2.javaMS-Word file InputScanner1.java (MS-Word file) InputScanner1.java (MS-Word file) InputScanner1.javaMS-Word file InputScanner1.javaMS-Word file

Console Input Using the Scanner Class The method next reads one string of non- whitespace characters delimited by whitespace The method next reads one string of non- whitespace characters delimited by whitespace The method nextLine reads an entire line of keyboard input The method nextLine reads an entire line of keyboard input 2-11 InputScanner3.java (MS-Word file) InputScanner3.java (MS-Word file) InputScanner3.javaMS-Word file InputScanner3.javaMS-Word file

Pitfall: Dealing with the Line Terminator, '\n' The method nextLine of the class Scanner reads the remainder of a line of text starting wherever the last keyboard reading left off The method nextLine of the class Scanner reads the remainder of a line of text starting wherever the last keyboard reading left off This can cause problems when combining it with different methods for reading from the keyboard such as nextInt, or next This can cause problems when combining it with different methods for reading from the keyboard such as nextInt, or next Given the code, Given the code, Scanner keyboard = new Scanner(System.in); int n = keyboard.nextInt(); String s1 = keyboard.nextLine(); String s2 = keyboard.nextLine(); and the input, and the input,2 Heads are better than 1 head. what are the values of n, s1, and s2 ? what are the values of n, s1, and s2 ? 2-12

Pitfall: Dealing with the Line Terminator, '\n' Given the code and input on the previous slide Given the code and input on the previous slide n will be equal to "2", s1 will be equal to "", and s2 will be equal to "heads are better than" If the following results were desired instead If the following results were desired instead n equal to "2", s1 equal to "heads are better than", and s2 equal to "1 head" then an extra invocation of nextLine would be needed to get rid of the end of line character ( '\n' ) then an extra invocation of nextLine would be needed to get rid of the end of line character ( '\n' ) 2-13 InputScanner4.java (MS-Word file) InputScanner4.java (MS-Word file) InputScanner4.javaMS-Word file InputScanner4.javaMS-Word file

Methods in the Class Scanner (Part 1 of 3) 2-14

Methods in the Class Scanner (Part 2 of 3) 2-15

Methods in the Class Scanner (Part 3 of 3) 2-16

Other Input Delimiters The delimiters that separate keyboard input can be changed when using the Scanner class The delimiters that separate keyboard input can be changed when using the Scanner class For example, the following code could be used to create a Scanner object and change the delimiter from white-space to "##" For example, the following code could be used to create a Scanner object and change the delimiter from white-space to "##" Scanner keyboard2 = new Scanner(System.in); Keyboard2.useDelimiter("##"); After invocation of the useDelimiter method, "##" and not white-space will be the only input delimiter for the input object keyboard2 After invocation of the useDelimiter method, "##" and not white-space will be the only input delimiter for the input object keyboard InputScanner5.java (MS-Word file) InputScanner5.java (MS-Word file) InputScanner5.javaMS-Word file InputScanner5.javaMS-Word file

Changing the Input Delimiter (Part 1 of 3) 2-18

Changing the Input Delimiter (Part 2 of 3) 2-19

Changing the Input Delimiter (Part 3 of 3) 2-20

Money Formats Using the NumberFormat class enables a program to output amounts of money using the appropriate format Using the NumberFormat class enables a program to output amounts of money using the appropriate format The NumberFormat class must first be imported in order to use it The NumberFormat class must first be imported in order to use it import java.text.NumberFormat An object of NumberFormat must then be created using the getCurrencyInstance() method An object of NumberFormat must then be created using the getCurrencyInstance() method The format method takes a floating-point number as an argument and returns a String value representation of the number in the local currency The format method takes a floating-point number as an argument and returns a String value representation of the number in the local currency 2-21

Money Formats 2-22 import java.text.NumberFormat; public class CurrencyFormatDemo { public static void main(String[] args) public static void main(String[] args) { System.out.println("Default location:"); System.out.println("Default location:"); NumberFormat moneyFormater = NumberFormat moneyFormater = NumberFormat.getCurrencyInstance(); NumberFormat.getCurrencyInstance(); System.out.println(moneyFormater.format(19.8)); System.out.println(moneyFormater.format(19.8)); System.out.println(moneyFormater.format( )); System.out.println(moneyFormater.format( )); System.out.println(moneyFormater.format( )); System.out.println(moneyFormater.format( )); System.out.println(moneyFormater.format(19)); System.out.println(moneyFormater.format(19)); System.out.println(); System.out.println(); }}

Money Formats Output of the previous program Output of the previous program Default location: $19.80$19.81$19.90$

Specifying Locale Invoking the getCurrencyInstance() method without any arguments produces an object that will format numbers according to the default location Invoking the getCurrencyInstance() method without any arguments produces an object that will format numbers according to the default location In contrast, the location can be explicitly specified by providing a location from the Locale class as an argument to the getCurrencyInstance() method In contrast, the location can be explicitly specified by providing a location from the Locale class as an argument to the getCurrencyInstance() method When doing so, the Locale class must first be imported When doing so, the Locale class must first be imported import java.util.Locale; 2-24

import java.text.NumberFormat; import java.util.Locale; public class CurrencyFormatDemo { public static void main(String[] args) public static void main(String[] args) { System.out.println("US as location:"); System.out.println("US as location:"); NumberFormat moneyFormater2 = NumberFormat moneyFormater2 = NumberFormat.getCurrencyInstance(Locale.US); NumberFormat.getCurrencyInstance(Locale.US); System.out.println(moneyFormater2.format(19.8)); System.out.println(moneyFormater2.format(19.8)); System.out.println(moneyFormater2.format( )); System.out.println(moneyFormater2.format( )); System.out.println(moneyFormater2.format( )); System.out.println(moneyFormater2.format( )); System.out.println(moneyFormater2.format(19)); System.out.println(moneyFormater2.format(19)); }} Specifying Locale 2-25

Specifying Locale Output of the previous program Output of the previous program US as location: $19.80$19.81$19.90$

Locale Constants for Currencies of Different Countries 2-27

The DecimalFormat Class Using the DecimalFormat class enables a program to format numbers in a variety of ways Using the DecimalFormat class enables a program to format numbers in a variety of ways The DecimalFormat class must first be imported The DecimalFormat class must first be imported A DecimalFormat object is associated with a pattern when it is created using the new command A DecimalFormat object is associated with a pattern when it is created using the new command The object can then be used with the method format to create strings that satisfy the format The object can then be used with the method format to create strings that satisfy the format An object of the class DecimalFormat has a number of different methods that can be used to produce numeral strings in various formats An object of the class DecimalFormat has a number of different methods that can be used to produce numeral strings in various formats 2-28

The DecimalFormat Class (Part 1 of 3) 2-29

The DecimalFormat Class (Part 2 of 3) 2-30

The DecimalFormat Class (Part 3 of 3) 2-31