1 / 22

Chapter 2 topics

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.” );.

olwen
Download Presentation

Chapter 2 topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 topics

  2. 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.”);

  3. // ************************************************************************ // 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 line Will print this statement and stay on the same line I’m on the line with the statement above.

  4. 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”;

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

  6. Strings & Numbers Need 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.

  7. 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.

  8. 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

  9. 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

  10. 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

  11. 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 1015 40 will be the sum of 36 + 4 This will add 36 + 4 because of the ( ) 40 >

  12. 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

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

  14. 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.

  15. 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

  16. 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 time int number = 37; double money = 28.42; boolean done = true;

  17. 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

  18. 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

  19. data type variable name 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

  20. Variables • A variable can be given an initial value in the declaration. Called initializing. 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."); } } • When a variable is referenced in a program, its current value is used • See PianoKeys.java

  21. Assignment • An assignment statement changes the value of a variable • The assignment operator is the = sign int total = 55; total = 47; • The expression on the right is evaluated and the result is stored in the variable on the left • The value that was in totalinitially 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)

  22. //********************************************************************//******************************************************************** // 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."); } • }

More Related