1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Basic Syntax: Data & Expressions
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
String Escape Sequences
© 2004 Pearson Addison-Wesley. All rights reserved1-1 Intermediate Java Programming Lory Al Moakar.
2.2 Information on Program Appearance and Printing.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Program Statements Primitive Data Types and Strings.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
Chapter 2: Java Fundamentals
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Chapter 2 Data and Expressions Part One. © 2004 Pearson Addison-Wesley. All rights reserved2-2/29 Data and Expressions Let's explore some other fundamental.
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.
Chapter 2 Data and Expressions Java Software Solutions Foundations of Program Design 1.
Lab 01-2 Objectives:  Writing a Java program.  How to send output to the command line console.  Learn about escape sequences.  Learn how to compile,
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2 print / println String Literals Escape Characters Variables / data types.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
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,
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Object Oriented Programming Idea Computer program may be seen as comprising a collection of objects Object Fundamental entity in a JAVA program Used to.
Chapter 02 Data and Expressions.
Intermediate Java Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
CSC 1051 – Data Structures and Algorithms I
Chapter 2 Data and Expressions
Introduction to Objects
Escape Sequences What if we wanted to print the quote character?
Data and Expressions Part One
MSIS 655 Advanced Business Applications Programming
String Concatenation Objectives
Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes
The keyboard is the standard input device.
CSC 1051 – Data Structures and Algorithms I
Instructor: Alexander Stoytchev
Java Programming Presented by Dr. K. SATISH KUMAR,
Introduction to Objects
Presentation transcript:

1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.

2 Objectives You will be able to:  Use the Java String class  and do String concatenation  Define and use variables in a Java program  Understand and use the Java primitive data types  Write expressions in a Java program  Write an interactive Java program  One that gets input from a user.

Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: System.out.println("Hello, World!"); Examples: "123 Main Street" "X" "" A String Literal 3

The println Method In the Lincoln program, we invoked the println method to display a character string on the screen. System.out.println ("Whatever you are, be a good one."); System.out is an object that has the ability to display characters on the screen using its println method The println method takes a single parameter A character string to be displayed on the screen The value that we supply for the parameter is called the argument. 4

5 System.out.println ("Whatever you are, be a good one."); object method name information provided to the method (argument) Summary

The print Method The System.out object provides another service as well. 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. 6

Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Countdown.java Author: Lewis/Loftus // // Demonstrates the difference between print and println. //******************************************************************** public class Countdown { // // Prints two lines of output representing a rocket 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!"); System.out.println ("Houston, we have a problem."); } Countdown.java 7

8 Running Countdown.java Program output

String Concatenation A string literal cannot be broken across two lines in a program. The string concatenation operator (+) is used to append one string to the end of another. "Peanut butter " + "and jelly" 9

The + Operator The + operator is also used for arithmetic addition. The function that it performs depends on the type of the information on which it operates. "Operator overloading" 10

The + Operator If both operands are strings, the + operator performs string concatenation. println ("Peanut butter " + "and jelly"); outputs Peanut butter and jelly 11

The + Operator If both operands are numeric, the + operator adds them. println( ) outputs 69 12

The + Operator If one operand is a string and the other is a number, the + operator also performs string concatenation, using the string representation of the number. println ( "The number following forty-four is " + 45); outputs The number following forty-four is 45 println( 45 + " is the number following 44"); outputs 45 is the number following 44 13

The + Operator When there are multiple + operators in an expression, the expression is evaluated left to right System.out.println ("24 and 45 concatenated: " ); outputs 24 and 45 concatenated:

The + Operator When there are multiple + operators in an expression, the expression is evaluated left to right but parentheses can be used to force the order. Subexpressions inside parentheses are evaluated first. System.out.println ("24 and 45 added: " + ( )); outputs 24 and 45 added: In PowerPoint, click on the slide to display successive lines.

Exercise What output is produced by the following program statements? A. System.out.println("6" + "7" ); B. System.out.println( "8" + "9"); C. System.out.println("6" + "7" + (8 + 9)); Try it! 16 In PowerPoint, click on the slide to display successive lines.

Plustest.java in Notepad++ 17

Running Plustest 18 "6" +"7" "8" +"9" "6" +"7" + (8 + 9) Can you explain why is result is what it is? In PowerPoint, click on the slide to show the strings that produced each result.

Escape Sequences What if we wanted to print the quote character? The following line would confuse the compiler because it would interpret the second quote as the end of the string System.out.println ("I said "Hello" to you."); 19 In PowerPoint, click on the slide to display successive lines.

Escape Sequences An escape sequence is a series of characters that represents a special character. An escape sequence begins with a backslash character ( \ ) System.out.println ("I said \"Hello\" to you."); \" is an escape sequence that puts a quotation mark into a String literal. The println above would display on the screen I said "Hello" to you. 20 In PowerPoint, click on the slide to display successive lines.

Escape Sequences Some Java escape sequences: Escape Sequence \b \t \n \" \' \\ Meaning backspace tab newline double quote single quote backslash 21 End of Section In PowerPoint, click on the slide to display successive lines.