Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.

Similar presentations


Presentation on theme: "Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs."— Presentation transcript:

1 Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs

2 Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: System.out.println("This is a string literal."); Examples: "123 Main Street" "X"

3 The println Method In the Lincoln program, we invoked the println method to print a character string The System.out object represents a destination (the monitor screen) to which we can send output System.out.println ("Whatever you are, be a good one."); object method name information provided to the method (parameters)

4 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

5 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."); }

6 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" It can also be used to append a number to a string

7 String Concatenation The + operator is also used for arithmetic addition The function that it performs depends on the type of the information on which it operates If both operands are strings, or if one is a string and one is a number, it performs string concatenation

8 String Concatenation If both operands are numeric, it adds them The + operator is evaluated left to right, but parentheses can be used to force the order System.out.println ("24 and 45 concatenated: " + 24 + 45); System.out.println ("24 and 45 added: " + (24 + 45));

9 Exercise What output is produced by the following program statements? A. System.out.println("6" +"7" +8 +9); B. System.out.println(6 +7 +"8" +"9"); C. System.out.println("6" +"7" +(8 +9));

10 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.");

11 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.");

12 Escape Sequences Some Java escape sequences: Escape Sequence \b \t \n \" \' \\ Meaning backspace tab newline double quote single quote backslash

13 Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Roses.java Author: Lewis/Loftus // // Demonstrates the use of escape sequences. //******************************************************************** public class Roses { //----------------------------------------------------------------- // Prints a poem (of sorts) on multiple lines. //----------------------------------------------------------------- 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."); }

14 Variables

15 A variable is a name for a location in memory. Variables in a program are used to store data such as numbers and letters. A variable must be declared by specifying the variable's name and the type of information that it will hold int total; data type variable name int count, temp, result; Multiple variables can be created in one declaration

16 Naming Conventions Style for names of variables –Start with a lower-case letter, then capitalize the first letter of any subsequent word in the name. For example, int radius; int currentSpeed; –The names should be nouns or noun phrases. –One-character variable names should be avoided except for temporary and loop variables.

17 Variable Initialization A variable can be given an initial value in the declaration When a variable is referenced in a program, its current value is used int sum = 0; int base = 32, max = 149;

18 Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; The value that was in total is overwritten You can only assign a value to a variable that is consistent with the variable's declared type The expression on the right is evaluated and the result is stored in the variable on the left

19 Constants A constant is an identifier that is similar to a variable except that it holds the same value during its entire existence Naming convention: upper case for constants In Java, we use the final modifier to declare a constant final int MIN_HEIGHT = 69;

20 Constants The compiler will issue an error if you try to change the value of a constant – the only valid place to change their value is in the initial assignment Constants give meaning to otherwise unclear literal values. For example, MAX_NUM_STUDENTS Second, they facilitate program maintenance. If a constant is used in multiple places, its value need only be updated in one place

21 Primitive Data Types

22 Primitive Data There are eight primitive data types in Java Four of them represent integers: –byte, short, int, long Two of them represent floating point numbers: –float, double One character type: char One boolean type: boolean

23 Numeric Primitive Data The difference between the various numeric primitive types is their size, and therefore the values they can store: Type byte short int long Min Value -128 -32,768 -2,147,483,648 < -9 x 10 18 Max Value 127 32,767 2,147,483,647 > 9 x 10 18 Common used type

24 Floating Point Types Type float double Min Value +/- 3.4 x 10 38 with 7 significant digits +/- 1.7 x 10 308 with 15 significant digits Max Value For example, double score; score = 78.4; Common used type

25 Characters A char variable stores a single character Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n' Example declarations: char topGrade = 'A'; char terminator = ';', separator = ' ';

26 Character Sets A character set is an ordered list of characters, with each character corresponding to a unique number A char variable in Java can store any character from the Unicode character set The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters

27 Characters The ASCII character set is older and smaller than Unicode, but is still quite popular The ASCII characters are a subset of the Unicode character set, including: uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, \, … backspace, tab,...

28 Boolean A boolean value represents a true or false condition The reserved words true and false are the only valid values for a boolean type boolean done = false; A boolean variable can also be used to represent any two states, such as a light bulb being on or off

29 Exercises Declare an integer variable cardsInHand and initialize it to 13. Declare a double variable temperature and initialize it to 98.6.

30 Expressions

31 An expression is a combination of one or more operators and operands Arithmetic expressions compute numeric results and make use of the arithmetic operators: Addition+ Subtraction- Multiplication* Division / Remainder%

32 Division and Remainder If both operands to the division operator ( / ) are integers, the result is an integer (the fractional part is discarded) – integer division results in truncation, not rounding The remainder operator (%) returns the remainder after dividing the second operand into the first 14 / 3 equals 8 / 12 equals 4 0 14 % 3 equals 8 % 12 equals 2 8

33 Operator Precedence Operators can be combined into complex expressions result = total + count / max - offset; Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation Arithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation order

34 Operator Precedence What is the order of evaluation in the following expressions? a / (b + c) - d % e 2341 a / (b * (c + (d - e))) 4123

35 Exercises Given the declarations below, find the result of each expression (in a Java program). int a = 3, b = 10, c = 7; a / b b / a b% a

36 Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated Then the result is stored in the variable on the left hand side answer = sum / 4 + MAX * lowest; 1432

37 Exercises Given two integer variables matricAge and gradAge, write a statement that gives gradAge a value that is 4 more than the value of matricAge.

38 Assignment Operators Often we perform an operation on a variable, and then store the result back into that variable. Java provides assignment operators to simplify that process For example, the statement num += count; is equivalent to num = num + count;

39 Assignment Operators There are many assignment operators in Java, including the following: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y

40 Assignment Operators The right hand side of an assignment operator can be a complex expression For example, result /= (total-MIN) % num; is equivalent to result = result / ((total-MIN) % num);

41 Interactive Programs

42 Programs generally need input on which to operate - The Scanner class provides convenient methods for reading input values of various types A Scanner object can be set up to read input from various sources, including the user typing values on the keyboard Keyboard input is represented by the System.in object

43 Reading Input The following line creates a Scanner object that reads from the keyboard: Scanner scan = new Scanner (System.in); The new operator creates the Scanner object Once created, the Scanner object can be used to invoke various input methods, such as: answer = scan.nextLine();

44 Reading Input The nextLine method reads all of the input until the newline character (\n, end of the line) is found The Scanner class is part of the java.util class library, and must be imported into a program to be used In the beginning of your program, import java.util.Scanner;

45 Input Tokens Unless specified otherwise, white space is used to separate the elements (called tokens) of the input - White space includes space characters, tabs, new line characters The next method of the Scanner class reads the next input token and returns it as a string Methods such as nextInt and nextDouble read data of particular types

46 Copyright © 2012 Pearson Education, Inc. //******************************************************************** // GasMileage.java Author: Lewis/Loftus // // Demonstrates the use of the Scanner class to read numeric data. //******************************************************************** import java.util.Scanner; public class GasMileage { //----------------------------------------------------------------- // Calculates fuel efficiency based on values entered by the // user. //----------------------------------------------------------------- public static void main (String[] args) { int miles; double gallons, mpg; Scanner scan = new Scanner (System.in); continue

47 Copyright © 2012 Pearson Education, Inc. continue System.out.print ("Enter the number of miles: "); miles = scan.nextInt(); System.out.print ("Enter the gallons of fuel used: "); gallons = scan.nextDouble(); mpg = miles / gallons; System.out.println ("Miles Per Gallon: " + mpg); }

48 Readings and Assignments Reading: Chapter 2.1 -2.4, 2.6 Lab Assignment: Java Lab 2 Self-Assessment Exercises: –Self-Review Questions SR2.4, 2.5, 2.6, 2.25, 2.27, 2.30 –After Chapter Exercises EX2.2, 2.5, 2.8, 2.9, 2.11

49 Grading Guidelines for Java Labs and Lab Exams 80% - Functionality: Runs correctly, generating correct outputs. A program that does not compile will result in a zero. 10% - Style: Use consistent indentation to emphasize block structure; variables have meaningful names. 10% - Comments: Comment major code segments adequately


Download ppt "Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs."

Similar presentations


Ads by Google