Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )

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.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
CMT Programming Software Applications
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
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 Screen Output Section 2.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
2.2 Information on Program Appearance and Printing.
CIS 270—Application Development II Chapter 28—Formatted Output.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
 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.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
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.
Chapter 2: Java Fundamentals
Welcome Smith Door AP Computer Science. Computer Science at West Robotics Programming (RobotC) (One Semester) VEX Robotics/Engineering Computer Science.
Chapter 2 – Continued Basic Elements of Java. Chapter Objectives Type Conversion String Class Commonly Used String Methods Parsing Numeric Strings Commonly.
Chapter 2: Java Fundamentals Type conversion,String.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
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.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Formatting Output. Formatting features of printf System.out.printf can perform the following Rounding floating-point values Aligning properly a column.
By Mr. Muhammad Pervez Akhtar
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
CS 1704 Introduction to Data Structures and Software Engineering.
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.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
28 Formatted Output.
More lO.
Formatted Output (printf)
TMF1414 Introduction to Programming
Chapter 2 Screen Output Section 2.1
Console Input and Output
Intro to Java.
OUTPUT STATEMENTS GC 201.
Formatting Output.
System.out.println for console output
API, Wrapper classes, printf, and methods
Chapter 3: Introduction to Objects and Input/Output
CS110D Programming Language I
Programming Assignment #1 12-Month Calendar—
Introduction to Java Applications
The keyboard is the standard input device.
CS 1054 Introduction to Programming in Java
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Input and Formatted Output (printf)
Presentation transcript:

Simple Console Output

What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )

Remember our Lab 0 File: Hello.java // Hello World application public class Hello { public static void main( String args[] ) { System.out.println( “Hello world” ); }

System.out.println( “Hello World”) Typing this in our first lab exercise resulted with the words Hello World displayed on the console The words “Hello World” is a string and is used as a parameter to the method called println More on strings, parameters and methods in our lessons next time

System.out.println( “Hello World”) For now, we know that whatever we type within the quotation marks in System.out.println( … ) will be displayed on the console The console refers to our display screen void of any graphics or graphical user interface (GUI) objects (ex. Buttons) Think of the console as the black MS-DOS window or the white output display box of BlueJ

What will be displayed? File: RevisedHello.java // Revised Hello World application public class RevisedHello { public static void main( String args[] ) { System.out.println( “Hello world” ); System.out.println( “Good morning” ); System.out.println( “Java is cool” ); }

System.out.println() println() will display each string on a separate line Hello world Good Morning Java is cool

How about using print()? File: RevisedHello.java // Revised Hello World application public class RevisedHello { public static void main( String args[] ) { System.out.print( “Hello world” ); System.out.print( “Good morning” ); System.out.print( “Java is cool” ); }

System.out.print() print() will NOT print the next string on a new line Hello worldGood MorningJava is cool

System.out.print() Another example System.out.println( “Hello world” ); System.out.print( “Good morning” ); System.out.println( “Java is cool”); System.out.println( “So is our teacher” ); Hello world Good MorningJava is cool So is our teacher

Try this… File: RevisedHello.java // Revised Hello World application public class RevisedHello { public static void main( String args[] ) { System.out.print( “Hello world” + “\n” ); System.out.print( “Good morning” + “\n” ); System.out.print( “Java is cool” + “\n” ); }

Using print() with “\n” Even if print() does not display consecutive strings in separate lines, the “\n” character causes a line break The same effect of println() can be achieved

Now try this… File: AdvancedExample.java // A demonstration of printf() public class AdvancedExample { public static void main( String args[] ) { double pi = Math.PI; System.out.println( pi ); } //note: pi is a variable assigned to the value of pi. //Math.PI is a constant value from Math.java //More on this in our future lessons

Formatting numbers The code will display on the console What if we want to force pi to be displayed with up to just 3 decimal places?

System.out.printf() File: AdvancedExample.java // A demonstration of printf() public class AdvancedExample { public static void main( String args[] ) { double pi = Math.PI; System.out.printf("pi = %5.3f \n", pi); }

System.out.printf() The line System.out.printf("pi = %5.3f \n", pi); will cause pi to be displayed as 3.142

Format Specifiers Indicates how the arguments should be processed and where they should be inserted Syntax % - start of the specifier arg_index + '$' (optional) position of argument in the argument list flags (optional) provides special formatting (e.g. left align, padding with zeroes)

Format Specifiers Syntax (continued) width (optional) minimum number of spaces to print argument puts blanks in unused spaces '.' + precision (optional) number of digits after decimal Conversion Formatted into: dintegers f floating-point number sString ccharacter conversion – how the argument should be formatted WE WILL DISCUSS THIS INS DETAIL IN OURFUTURE LESSONS ON STRINGS

The same concept simplified… System.out.printf("pi = %5.3f \n", pi); The % symbol signifies the start of the specifier 5 means that the width value of the number is 5 characters The precision value 3 means that 3 decimal places are required The conversion character f means that the resulting pi is a floating-point number \n results in a line break