CS2 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

CS2

Description:

'println' vs. 'print' in Java: println includes 'carriage return' print finishes, staying ... Use of ' ' will often appear in print and println parameter lists. ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 11
Provided by: ccGa
Category:
Tags: cs2

less

Transcript and Presenter's Notes

Title: CS2


1
CS2
  • Module 11
  • Category Elements of Java
  • Topic Printing
  • Objectives
  • Basic information to print

2
CS 2
  • Introduction to
  • Object Oriented Programming
  • Module 11
  • Elements of Java
  • Basic Printing

3
Displaying information
  • Graphical User Interfaces
  • Printing with print and println
  • Simple
  • Useful for debugging

4
Printing to Screen
  • CS1
  • (display ltargumentgt)
  • Java
  • System.out.println(ltargumentgt)
  • System.out.println( ) // prints blank line
  • System.out.println(5) // prints 5
  • System.out.println(Hello World)
  • System.out.println("Hello " "World")
  • println vs. print in Java
  • println includes carriage return
  • print finishes, staying on same line

A single string!
Java actually converts the 5 to a string and
then prints it!!!
5
Printing to Screen
  • String concatenation in Java is accomplished
    using ""
  • Use of "" will often appear in print and println
    parameter lists.
  • System.out.println("Average is " avgScore)
  • System.out.print("This is another handy "
  • "use of the concatenation operator "
  • "when the line is just too long, "
  • "you can break "
  • "it up this way")

6
Strange Printing Tricks
  • If you want to print
  • The answer is 8
  • Using
  • System.out.println("The answer is " 3 5)
  • You will be in for a surprise!
  • The answer is 35
  • Why?
  • How do you fix it?

7
Printing Tricks
  • Special symbols are indicated by preceding them
    with a \ (backslash).
  • \n Go to a new line
  • \t Go to the next tab stop
  • \\ Print a backslash
  • \" Print a double quote symbol
  • System.out.println("This is a double quote"
  • " symbol \". What do you think of that!!!")

8
Printing (contd)
  • When starting Java, there are at least three
    streams created for you
  • System.in // for getting input
  • System.out // for output
  • System.err // for bad news output
  • Why 2 output streams
  • may redirect output stream to a file
  • may let error stream continue to go to the screen
    (rather than the file) to alert the user of a
    problem.

9
Printing (contd)
System.out.println("This line is
printed") System.err.println ("This is the
error stream's output") These are both
instances of the PrintStream class. There are
other methods that you might find useful in these
classes (for example) System.out.flush()
10
A word about debugging...
  • Let Java help you debug your code
  • At appropriate points add a System.out.println
    statement letting you know
  • Where you are
  • What is the value of variables of interest
  • Useful to print before loop, inside loop, and
    after loop
  • Useful to print when entering a method
  • Make System.out.println( ) your new best friend!
Write a Comment
User Comments (0)
About PowerShow.com