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.

Slides:



Advertisements
Similar presentations
This is Java Jeopardy.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CMT Programming Software Applications
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
String Escape Sequences
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.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Mathematical Calculations in Java Mrs. G. Chapman.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
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.
Mathematical Calculations in Java Mrs. C. Furman.
Chapter 2 Data and Expressions Java Software Solutions Foundations of Program Design 1.
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
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.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
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.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
OUTPUT STATEMENTS GC 201.
Escape Sequences What if we wanted to print the quote character?
Data and Expressions Part One
Building Java Programs
Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Instructor: Alexander Stoytchev
Building Java Programs
Presentation transcript:

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 String literals & Concatenation8. 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 assignment4 Arithmetic Expressions2, 3, 4, 5, 6 Order of precedence2. Objects & Classes

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

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

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

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

Strings & Numbers Need to be careful to get correct output from numbers. System.out.println(“We want to add “ ); 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 and 15 are printed as if they are String literals.

Strings & Numbers The plus sign is also used as the addition operator. System.out.println( “ is ”); The numbers will be added together because they are at the beginning before the String. So it will perform addition. Output: 40 is

Strings and Numbers You can use parentheses to force addition with the + sign. System.out.println(“ = “ + (36+5)); Parentheses have highest precedence so it performs that operation first. Output: = 40

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

Check Printing String literals with concatenation What will the following print? System.out.println(“This will print “ ); System.out.println(“This will print “ + ( )); System.out.println( “ 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

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

Escape sequences Page 60 Used with Strings to produce a newline, slash or quotation marks. \t tab will tab 5 spaces \n newlinewill go to a new line \\ will print \ will print one slash \” will print double quotes

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

TYPESIZERANGE intwhole numbers doublefractional numbers ± E-324; ± E+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.

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

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;

data type with operations when you use 2 integers in an operation the result is an integer 20/6 + 3 = 20/6 = = 6 When you use a double as one of the operands then the result is a double 20/ = 20/6.0 = = 6.333

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 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 typevariable name

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

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 = because total is declared as an int;  See Geometry.java (page 70)Geometry.java  The expression on the right is evaluated and the result is stored in the variable on the left

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