OUTPUT STATEMENTS GC 201.

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
CMT Programming Software Applications
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
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(,,,..., );
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
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.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Java Fundamentals
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Java Fundamentals 5. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
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( … )
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Introduction to Programming
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Java Fundamentals 4.
BASIC ELEMENTS OF A COMPUTER PROGRAM
CPS120: Introduction to Computer Science
Yanal Alahmad Java Workshop Yanal Alahmad
TMF1414 Introduction to Programming
Java Programming: From Problem Analysis to Program Design, 4e
SELECTION STATEMENTS (1)
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Escape Sequences What if we wanted to print the quote character?
IDENTIFIERS CSC 111.
INPUT & OUTPUT scanf & printf.
SELECTION STATEMENTS (2)
Chapter 9 - Formatted Input/Output
Chapter 3: Introduction to Objects and Input/Output
Chapter 2: Java Fundamentals
elementary programming
Introduction to Java Applications
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
CS 1054 Introduction to Programming in Java
Presentation transcript:

OUTPUT STATEMENTS GC 201

Outline 1. System.out.print 2. System.out.println 3. Escape Sequences 4. System.out.printf 5. Format Specifiers

3. OUTPUT STATEMENTS SYNTAX System.out.print (expression); Example System.out.print (29/4); Output 7_ 1 Note the cursor position (_): it is on the same line as the output. Note that 29/4 is not enclosed between quotes  it is evaluated. SYNTAX System.out.println (expression); Example System.out.println (29/4); Output 7 _ 1 2 Note the cursor position (_): it is on the next line to the output. SYNTAX System.out.println (); Skips a line. Output _ 1

3. OUTPUT STATEMENTS The output of Program 1 is as follows: PROGRAM 1 // import necessary libraries public class userOutput1 { public static void main (String[] args) // Declaration section: to declare needed variables // Input section: to enter values of used variables // Processing section: processing statements // Output section: display program output System.out.print (“Hello there”); //output line 1 System.out.println (“My name is Fatma”); //output line 1 System.out.println (“I am studying Java”); //output line 2 } // end main } // end class 1 2 3 4 5 6 7 8 9 10 11 12 13 14 The output of Program 1 is as follows: Hello thereMy name is Fatma I am studying Java _ 1 2 3

3. OUTPUT STATEMENTS Escape sequences allow you to control the output. All escape sequences start with a backslash \ character. The following table shows some of the most commonly used escape sequences: Syntax Escape Sequence Description \n New line Cursor moves to the beginning of the next line. \r Return Cursor moves to the start of the current line. \t Tab Cursor moves to the next tab stop. \b Backspace Cursor moves one space to the left. \\ Backslash Backslash is printed. \’ Single quote Single quotation is printed. \” Double quotes Double quote is printed

3. OUTPUT STATEMENTS The output of Program 2 is as follows: PROGRAM 2 // import necessary libraries public class userOutput2 { public static void main (String[] args) // Declaration section: to declare needed variables // Input section: to enter values of used variables // Processing section: processing statements // Output section: display program output System.out.print (“Hello there \t”); //output line 1 System.out.print (“My name is Fatma\n”); //output line 1 System.out.println (“I am studying \”Java\””);//output line 2 } // end main } // end class 1 2 3 4 5 6 7 8 9 10 11 12 13 14 The output of Program 2 is as follows: Hello there My name is Fatma I am studying “Java” _ 1 2 3

3. PRINTF STATEMENT SYNTAX System.out.printf (formatString); Example 1 System.out.printf (“Hello there!!”); Output Hello there!!_ 1 SYNTAX System.out.printf (formatString, argumentList); The argumentList is a list of one or more arguments: constant values, variables, or expressions. If the argumentList has more than one argument, then the arguments are separated by commas.

FORMATTING INTEGER NUMBERS 3. PRINTF STATEMENT FORMATTING INTEGER NUMBERS Example 1 int x = 120; System.out.printf (“The value of x = %d”, x); “The value of x = %d” is the formatString. x is the argumentList. %d is called a format specifier. There is one-to-one correspondence between the format specifier and the arguments in the argumentList. The format specifier used depends on the type of the argument. %d is used for the variables of type int. Output The value of x = 120_ 1 Example 2 int cm = 25; int mm = 2500; System.out.printf (“There are %d mm in %d cm”, mm, cm); The formatString is “There are %d mm in %d cm”. The argumentList is mm, cm Output There are 2500 mm in 25 cm_ 1

FORMATTING FLOATING-POINT NUMBERS 3. PRINTF STATEMENT FORMATTING FLOATING-POINT NUMBERS The default output of floating-point numbers is: up to 6 decimal places for float values, and up to 15 decimal places for double values. The %f is the format specifier used for floating-point numbers. %.2f specifies the number of digits printed after the decimal point (2 in this example). Example 1 static final float PI = 3.14159f; double radius = 7.534; System.out.printf (“PI = %.3f and radius = %.1f ”, PI, radius); Output PI = 3.142 and radius = 7.5_ 1 Note that the numbers are approximated. Example 2 double area = 227.534; System.out.printf (“Area = %8.2f”, area); Output Area = ~~227.53_ 1

FORMATTING CHARACTER VARIABLES 3. PRINTF STATEMENT FORMATTING CHARACTER VARIABLES The %c is the format specifier used for char values. Example char option = ‘Y’; System.out.printf (“Option = %c”, option); Output Option = Y_ 1 The following are other format specifiers: Specifier Description %d The result is formatted as a (decimal) integer %f The result is formatted as a decimal floating-number %c The result is a Unicode character %s The result is a string %e The result is formatted as a scientific notation %n Line separator %% Prints %

3. PRINTF STATEMENT NOTES By default, the output is right-justified for all primitive data types. Example 1 String name = “aly”; System.out.printf (“First name = %6s%n”, name); Output First name = ~~~aly _ 1 2 To print it left-justified, precede the width with a – sign: Example 2 String name = “aly”; System.out.printf (“First name = %-6s%n”, name); Output First name = aly~~~ _ 1 2 Example 3 int year = 2015; System.out.printf (“Academic year = %-6d-%6d”, year, ++year); Output Academic year = 2015~~-~~2016_ 1

Self-Check Exercises (1) What is the output of the following program: public class selfCheck1 { public static void main (String[] args) int num = 52033; double x = 9234.8667; String str = “Computer Science”; System.out.printf (“%-5d%-10.2f%-20s ***%n”, num, x, str); System.out.printf (“%-25s%-6d%-12.3f ***%n”, str, num, x); System.out.printf (“%-13.4f%-7d%-22s ***%n”, x, num, str); } // end main } // end class 1 2 3 4 5 6 7 8 9 10 11 12 W3.3 Output Statements

Self-Check Exercises (2) A milk carton can hold 3.78 liters of milk. Each morning, a dairy farm ships cartons of milk to a local grocery store. The cost of producing one liter of milk is $0.38, and the profit of each carton of milk is $0.27. Write a program that does the following: Prompts the user to enter the total amount of milk produced in the morning. Outputs the number of milk cartons needed to hold milk (Round your answer to the nearest integer). Outputs the cost of producing milk. Output the profit for producing milk. W3.3 Output Statements