CS 1054 Introduction to Programming in Java

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Formatting Output For a pleasant interaction with the user.
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.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
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.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Introduction to Computer Programming Looping Around Loops I: Counting Loops.
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.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
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.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
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.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
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 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.
Introducing Python CS 4320, SPRING Format: Field widths and Alignment The string representation of a value can be padded out to a specific width.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Today’s topic:  Arithmetic expressions. Arithmetic expressions  binary (two arguments) arithmetic operators  +, -, *, /, % (mod or modulus)  unary.
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.
Java Fundamentals 5. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
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.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
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.
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
String and Scanner CS 21a: Introduction to Computing I First Semester,
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
String Formatting Preparing strings for output. Printing println( arg ) prints its one argument on a line println can be used to print any single value,
Chapter 2 print / println String Literals Escape Characters Variables / data types.
CS 1704 Introduction to Data Structures and Software Engineering.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Chapter 2 1.What is the difference between print / println 2.What are String Literals 3.What are the Escape Characters for backslash, double quotataions,
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.
More lO.
Formatted Output (printf)
Chapter 2 Screen Output Section 2.1
Console Input and Output
OUTPUT STATEMENTS GC 201.
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.
Formatting Output.
System.out.println for console output
API, Wrapper classes, printf, and methods
Lecture 13 Input/Output Files.
Chapter 3: Introduction to Objects and Input/Output
Programming Assignment #1 12-Month Calendar—
Introduction to Java Applications
The keyboard is the standard input device.
Introduction to Primitives
Introduction to Primitives
Presentation transcript:

CS 1054 Introduction to Programming in Java Console Output in Java Notes Supplement CS 1054 Introduction to Programming in Java Spring 2008

Introduction to Object-Oriented Programming print() and println() So far, we’ve used System.out.print and System.out.println for output With a single argument (string or numeric value) println() if we require a new line at the end of the output print() to stay on the current output line Introduction to Object-Oriented Programming

Printing values around strings Suppose we wanted to print the values of two variables, numCones (int) and unitPrice (double), in a neat message e.g., sold 5 cones at 2.50 each. Using print and println, System.out.print("sold " ); System.out.print( numCones ); System.out.print( " cones at " ); System.out.print( unitPrice ); System.out.println(" each." ); Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming String concatenation Can use the + operator on strings If both arguments are strings, it is simply concatenated or “put together”, e.g., "Basket" + "ball" equals "Basketball" "Hi, " + name equals "Hi, Bob" assuming name has the value "Bob" If one argument is a number, that argument is converted to a string "Hi, " + 5 equals "Hi, " + "5" = "Hi, 5" "Price: " + unitPrice equals "Price: 2.50" assuming price has the value 2.50 Introduction to Object-Oriented Programming

Using concatenation with print and println Improved version System.out.print("sold " + numCones ); System.out.println( " cones at " + unitPrice + " each." ); Can actually be compressed into one println statement Note that the print or println method still has one argument even in these cases (a string) Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming String formatting Notice that particularly with floating point numbers, we have no control over how many spaces are occupied by a number when printed e.g., 5 cones sold at 2.4999998 each What would be preferable is to be able to indicate up to how many decimal places are allowed Can be done in Java using the printf() method Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming The printf() method Even better version: System.out.printf( "sold %d cones at %.2f each.\n", numCones, unitPrice ); prints: 5 cones sold at 2.50 each More control: System.out.printf( "sold %3d cones at %6.2f each.\n", numCones, unitPrice ); prints: ˍˍ5 cones sold at ˍˍ2.50 each \n means a newline, same effect as println() Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming System.out.printf() // prints PI with 3 decimal places // 6 spaces total: ˍ3.142 including the . public class PrintingPI { public static void main( String args[] ) double pi = Math.PI; System.out.printf("pi = %6.3f \n", pi); } Format specifier Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Format specifier System.out.printf("pi = %6.3f \n", pi); The % symbol signifies the start of the specifier 6 means that the width of the number is 6 characters (including the . ; pad with leading spaces if needed) The precision value 3 means that 3 decimal places are required The conversion character f means that the corresponding argument is a floating-point number \n results in a line break Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Conversion options %10.3f : use f for float and double, first number is the width, second number is the number of decimal places %5d : use d for integer types, the number indicates the width %10s : use s for strings, the number indicates the width %d %f %s with no numbers indicated means you are leaving the formatting to Java defaults Introduction to Object-Oriented Programming