22
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println 10. Testing of output is restricted to System.out.print and System.out.println String literals & Concatenation 8. String concatenation + is part of the AP Java subset Escape sequences: produce backslash, double quotes, new line 9. The escape sequences inside strings \\, \", \n are part of the AP Java subset. Data types 1, 7. primitive types int, double, and boolean Variables and assignment 4

Chapter 2 topics

  • Upload
    olwen

  • View
    43

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 2 topics . p rintln & print. System.out.println ( “ Will print this statement and then return to a new line” ); System.out.print ( “Will print this statement and stay on the same line” ); System.out.println ( “ I’m on the line with the statement above.” );. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 2 topics

Chapter 2 topics Concept # on Java Subset Required for AP Exam

print and println 10. Testing of output is restricted to System.out.print and System.out.println

String literals & Concatenation 8. String concatenation + is part of the AP Java subset

Escape sequences: produce backslash, double quotes, new line

9. The escape sequences inside strings \\, \", \n are part of the AP Java subset.

Data types 1, 7. primitive types int, double, and boolean

Variables and assignment 4

Arithmetic Expressions 2, 3, 4, 5, 6

Order of precedence 2.

Objects & Classes

Page 2: Chapter 2 topics

println & print

System.out.println(“ Will print this statement and then return to a new line”);

System.out.print( “Will print this statement and stay on the same line” ); System.out.println(“ I’m on the line with the statement above.”);

Page 3: Chapter 2 topics

// ************************************************************************// PrintPrintln.java Author: Gail Chapman // This program demonstrates Println and Print // ***********************************************************************

public class PrintPrintln{ public static void main(String[]args) { System.out.println("Will print this statement and then return to a new line"); System.out.print( "Will print this statement and stay on the same line" ); System.out.println(" I’m on the line with the statement above.");

}}

Output:

Will print this statement and then return to a new lineWill print this statement and stay on the same line I’m on the line with the statement above.

Page 4: Chapter 2 topics

String literals & Concatenation

String refers to a string of characters that make up words.

String is a class. It is not a primitive data type. Every String literal is an object.

String literals are words in quotation marks. “This is a String literal”;

Page 5: Chapter 2 topics

Concatenation

The concatenation operator used with Strings is the plus sign +

The + sign is also used for arithmetic operations.

The + sign joins: Strings and other Strings (“This is a String ” + “ and another String”);

Page 6: Chapter 2 topics

Strings & NumbersNeed to be careful to get correct output from numbers.

System.out.println(“We want to add “ + 10 + 15); What is the output:

The compiler starts at the left encounters the String and reads everything after it as a String. It will perform String concatenation.

Output: We want to add 1015 10 and 15 are printed as if they are String literals.

Page 7: Chapter 2 topics

Strings & Numbers

The plus sign is also used as the addition operator.

System.out.println(36 + 4 + “ is 36 + 4”); The numbers will be added together because they are at the beginning before the String. So it will perform addition. Output: 40 is 36 + 4.

Page 8: Chapter 2 topics

Strings and Numbers

You can use parentheses to force addition with the + sign.

System.out.println(“36 + 5 = “ + (36+5)); Parentheses have highest precedence so it performs that operation first.

Output: 36 + 5 = 40

Page 9: Chapter 2 topics

What will print? System.out.println("12" + 3 + 4);

System.out.println("12" + (3 + 4));

System.out.println(12 + 3 + 4);

System.out.println(12 + "3" + 4);

System.out.println(12 + 3 + "4");

1234

127

19

1234

154

Page 10: Chapter 2 topics

Check Printing String literals with concatenation

What will the following print? System.out.println(“This will print “ + 3 + 2 + 0);

System.out.println(“This will print “ + (3 + 2 + 0));

System.out.println(3 + 2 + 0 + “ will print”);

System.out.println(“This will print “ + 2 * 6);

This will print 320

This will print 5

5 will print

This will print 12

Page 11: Chapter 2 topics

public class ConcatenationRules{ public static void main(String[]args) { System.out.println("This will not add the numbers 10 + 15 together " + 10 + 15 ); System.out.println(36 + 4 + " will be the sum of 36 + 4"); System.out.println("This will add 36 + 4 because of the ( ) " + (36+4)); }}

Output:

This will not add the numbers 10 + 15 together 101540 will be the sum of 36 + 4This will add 36 + 4 because of the ( ) 40>

Page 12: Chapter 2 topics

Escape sequences Page 60

• Used with Strings to produce a newline, slash or quotation marks.

\t tab will tab 5 spaces \n newline will go to a new line\\ will print \ will print one slash\” will print double quotes

Page 13: Chapter 2 topics

Escape Sequences //escape characters

public class Escape{ public static void main(String[]args) { System.out.println("A slash n \nis used to go to a new line\n"); System.out.println("A slash quotation mark \"will\" print a double quote"); System.out.println("double slash \\ will print one backslash."); System.out.println("A slash t twill tab.\n\tName\t\tAge\t\tDOB"); System.out.println("A slash one quotation \'will\' produce single quote."); }}

Page 14: Chapter 2 topics

TYPE SIZE RANGE

int whole numbers 2147483648

2147473647

double fractional numbers

±4.94065645841246544E-324;

±1.79769313486231570E+308

boolean {true, false} Holds two values: {true, false}

PRIMITIVE DATA TYPES:

Information in program is stored in variables. Data type is the type of data stored in the variable. There are 8 Primitive data types in Java.

Page 15: Chapter 2 topics

Memory Storage

• In order to reserve space in memory for the variable, the computer has to know what type of data it will be.

• Declare = to tell what type of data the variable will hold and its name

• Initialize = assign the variable a value using the = sign

Page 16: Chapter 2 topics

Declare is to tell what type of data the variable will hold and its name• the computer needs to know how much memory to store for that variable • int number;double money; boolean done;

You cannot use a variable until you declare the type

Initalize is to assign the variable a value using the = sign number = 4;money = 7.50;done = false ;

Can Initialize & Declare at same timeint number = 37;double money = 28.42; boolean done = true;

Page 17: Chapter 2 topics

data type with operations

• when you use 2 integers in an operation the result is an integer

• 20/6 + 3 = 20/6 = 3 + 3 = 6

• When you use a double as one of the operands then the result is a double

• 20/6.0 + 3 = 20/6.0 = 3.333 + 3 = 6.333

Page 18: Chapter 2 topics

Using variables to store the answer

Storing that information in a variable• integers can be stored in an int or a double

int a = 20;int b = 6 int c = 3; int d; double d; 20/6 + 3 = 6.0;

d = a / b + c ; 20/6 + 3 = 6

Page 19: Chapter 2 topics

19

Variables• A variable is a name for a location in memory

• A variable must be declared by specifying the variable's name and the type of information that it will hold

int total;

int count, temp, result;

Multiple variables can be created in one declaration

data type variable name

Page 20: Chapter 2 topics

20

Variables• A variable can be given an initial value in the declaration. Called

initializing.

When a variable is referenced in a program, its current value is used

See PianoKeys.java

int sum = 0;int base = 32, max = 149;double amount = 47.52; boolean done = false;

public class PianoKeys{ //----------------------------------------------------------------- // Prints the number of keys on a piano. //----------------------------------------------------------------- public static void main (String[] args) { int keys = 88;

System.out.println ("A piano has " + keys + " keys."); }}

Page 21: Chapter 2 topics

21

Assignment• An assignment statement changes the value of a variable• The assignment operator is the = sign

int total = 55;total = 47;

The value that was in total initially is overwritten You can assign only a value to a variable that is consistent with the

variable's declared type could not say total = 47.50 because total is declared as an int;

See Geometry.java (page 70)

The expression on the right is evaluated and the result is stored in the variable on the left

Page 22: Chapter 2 topics

//********************************************************************// Geometry.java Author: Lewis/Loftus/Cocking//// Demonstrates the use of an assignment statement to change the// value stored in a variable.//********************************************************************

public class Geometry{ //----------------------------------------------------------------- // Prints the number of sides of several geometric shapes. //----------------------------------------------------------------- public static void main (String[] args) { int sides = 7; // declaration with initialization System.out.println ("A heptagon has " + sides + " sides.");

sides = 10; // assignment statement System.out.println ("A decagon has " + sides + " sides.");

sides = 12; System.out.println ("A dodecagon has " + sides + " sides."); }• }