Chapter 2 Screen Output Section 2.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.

Slides:



Advertisements
Similar presentations
Formatting Output For a pleasant interaction with the user.
Advertisements

Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Alice in Action with Java Chapter 7 From Alice to Java.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS102--Object Oriented Programming Lecture 2: – the String class – Console Input/Output – Flow of control Copyright © 2008 Xiaoyan Li.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
 Pearson Education, Inc. All rights reserved Formatted Output.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
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
Formatting Screen Output How do I make my numbers look pretty?
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 10 Introduction to File I/O Section 10.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
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.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
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.
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
More lO.
Input/Output.
Console Input and Output
Formatting Output & Enumerated Types & Wrapper Classes
Chapter 2 Screen Output Section 2.1
Console Input and Output
OUTPUT STATEMENTS GC 201.
System.out.println for console output
Chapter 4 Constructors Section 4.4
CS 1054 Introduction to Programming in Java
Presentation transcript:

Chapter 2 Screen Output Section 2.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage

System.out.println for console output System.out is an object that is part of the Java language println is a method invoked by the System.out object that can be used for console output – The data to be output is given as an argument in parentheses – A plus sign is used to connect more than one item – Every invocation of println ends a line of output System.out.println("The answer is " + 42); 2-2 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

println Versus 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 – With println, the next output goes on a new line – With print, the next output goes on the same line 2-3 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

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 The Java method printf is similar to the print method – Like print, printf does not advance the output to the next line 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 – All the arguments except the first are values to be output to the screen 2-4 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

printf Format Specifier The code double price = 19.8; System.out.print("$"); System.out.printf("%6.2f", price); System.out.println(" each"); will output the line $ each The format string "%6.2f" indicates the following: – End any text to be output and start the format specifier ( % ) – Display up to 6 right-justified characters, pad fewer than six characters on the left with blank spaces (i.e., field width is 6 ) – Display exactly 2 digits after the decimal point (.2 ) – Display a floating point number, and end the format specifier (i.e., the conversion character is f ) 2-5 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Right and Left Justification in printf The code double value = ; System.out.printf("Start%8.2fEnd", value); System.out.println(); System.out.printf("Start%-8.2fEnd", value); System.out.println(); will output the following Start 12.12End The format string "Start%8.2fEnd" produces output that is right justified with three blank spaces before the The format string "Start%-8.2fEnd" produces output that is left justified with three blank spaces after the Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Multiple arguments with printf The following code contains a printf statement having three arguments – The code double price = 19.8; String name = "magic apple"; System.out.printf("$%6.2f for each %s.", price, name); System.out.println(); System.out.println("Wow"); will output $ for each magic apple. Wow – Note that the first argument is a format string containing two format specifiers ( %6.2f and %s ) – These format specifiers match up with the two arguments that follow ( price and name ) 2-7 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Line Breaks with printf Line breaks can be included in a format string using %n The code double price = 19.8; String name = "magic apple"; System.outprintf("$%6.2f for each %s.%n", price, name); System.out.println("Wow"); will output $ for each magic apple. Wow 2-8 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Format Specifiers for System.out.printf 2-9 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

2-10 The printf Method (Part 1 of 3)

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.2-11 The printf Method (Part 2 of 3)

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.2-12 The printf Method (Part 3 of 3)

Formatting Money Amounts with printf A good format specifier for outputting an amount of money stored as a double type is %.2f It says to include exactly two digits after the decimal point and to use the smallest field width that the value will fit into: double price = 19.99; System.out.printf("The price is $%.2f each.") produces the output: The price is $19.99 each Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Legacy Code Code that is "old fashioned" but too expensive to replace is called legacy code Sometimes legacy code is translated into a more modern language The Java method printf is just like a C language function of the same name This was done intentionally to make it easier to translate C code into Java 2-14 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Money Formats 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 import java.text.NumberFormat – 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 2-15 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Money Formats import java.text.NumberFormat; public class CurrencyFormatDemo { public static void main(String[] args) { System.out.println("Default location:"); NumberFormat moneyFormater = NumberFormat.getCurrencyInstance(); System.out.println(moneyFormater.format(19.8)); System.out.println(moneyFormater.format( )); System.out.println(moneyFormater.format( )); System.out.println(moneyFormater.format(19)); System.out.println(); } 2-16 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Money Formats Output of the previous program Default location: $19.80 $19.81 $19.90 $ Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Specifying Locale 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 – When doing so, the Locale class must first be imported import java.util.Locale; 2-18 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Specifiying Locale import java.text.NumberFormat; import java.util.Locale; public class CurrencyFormatDemo { public static void main(String[] args) { System.out.println(“France as location:"); NumberFormat moneyFormater2 = NumberFormat.getCurrencyInstance(Locale.FRANCE); System.out.println(moneyFormater2.format(19.8)); System.out.println(moneyFormater2.format( )); System.out.println(moneyFormater2.format( )); System.out.println(moneyFormater2.format(19)); } 2-19 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Specifying Locale Output of the previous program France as location: 19,80 € 19,81 € 19,90 € 19,00 € 2-20 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Locale Constants for Currencies of Different Countries 2-21 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Importing Packages and Classes 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 – 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 import java.text.NumberFormat; // import theNumberFormat class only import java.text.*; //import all the classes in package java.text 2-22 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The DecimalFormat Class Using the DecimalFormat class enables a program to format numbers in a variety of ways – The DecimalFormat class must first be imported – 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 – An object of the class DecimalFormat has a number of different methods that can be used to produce numeral strings in various formats 2-23 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The DecimalFormat Class (Part 1 of 3) 2-24 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

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

The DecimalFormat Class (Part 3 of 3) 2-26 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.