Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes

Similar presentations


Presentation on theme: "Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes"— Presentation transcript:

1 Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes
Use your Purple Paper!

2 What did we do in our programs from Chapter 1?
System.out.println ( “ “); Lets try System.out.print The print method is similar to the println method, except that it does not advance to the next line Therefore anything printed after a print statement will appear on the same line

3 // Countdown.java // // Demonstrates the difference between print and println. public class Countdown { public static void main (String[] args) { System.out.print ("Three... "); System.out.print ("Two... "); System.out.print ("One... "); System.out.print ("Zero... "); System.out.println ("Liftoff!"); // appears on first output line System.out.println ("Houston, we have a problem."); } What is the output for this program?

4 Output… Three…Two…One…Zero…Liftoff! Houston, we have a problem.

5 2.2 – Character Strings A string literal is always placed inside double quotation marks. The string concatenation operator (+) is used to join one string to the end of another It can also be used to place a number in a string

6 System.out.println (“A dozen is equal to 12");

7 What’s the different between these two?
System.out.println (“A dozen is equal to “ ); System.out.println ("A dozen is equal to “ + (10 + 2)); The first line is concatenated. The second line is added.

8 2.2 - Escape Sequences What if we wanted to print a double quote character? System.out.println (“She said “hello” to me.”); That line would confuse the compiler because it would interpret the second quote as the end of the string We need to use an escape sequence. An escape sequence begins with a backslash character (\), which indicates that the character(s) that follow should be treated in a special way

9 2.2 - Escape Sequences Some Java escape sequences:
\b \t \n \r \" \' \\ Meaning backspace tab newline carriage return double quote single quote backslash See Roses.java (page 67)

10 public static void main (String[] args) {
public class Roses { public static void main (String[] args) { System.out.println ("Roses are red,\n\tViolets are blue,\n" + "Sugar is sweet,\n\tBut I have \"commitment issues\",\n\t" + "So I'd rather just be friends\n\tAt this point in our " + "relationship."); } What is the output?

11 Now you try… Create a file called EscapeSequences in the Notes Project.

12 Assignment Textbook Assignment M.C. (1, 3) T/F (1 – 3) S.A. (2 – 5)


Download ppt "Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes"

Similar presentations


Ads by Google