BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

Slides:



Advertisements
Similar presentations
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
Advertisements

COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
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,
Perimeter and Area o Perimeter is the distance around the outside of a flat object. o Area is the amount of surface space that a flat object has.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Chapter 2 Practice.
Areas and Perimeter of Rectangles, Square, Triangles and Circles
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Chapter 3.6 Variation. Direct Variation When one quantity is a constant multiple of another quantity, the two quantities are said to vary directly. For.
1 Variables. 2 Receipt example What's bad about the following code? public class Receipt { public static void main(String[] args) { // Calculate total.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Data And Variables Chapter Names vs. Values Michael Jordan name (the letter sequence used to refer to something) value (the thing itself)
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Topic 4 Expressions and variables Based on slides bu Marty Stepp and Stuart Reges from "Once a person has understood.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
$100 $200 $300 $400 $100 $200 $300 $400 $300 $200 $100 Writing variable equations Find variables in addition equations Find variables in subtraction.
A Sample Program #include using namespace std; int main(void) { cout
Variables and Expressions #4. A variable is a letter or symbol that represents a quantity that can change. A constant is a quantity that does not change.
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
2D Shapes.
CS 106A, Lecture 4 Introduction to Java
Lecture 4: Expressions and Variables
Building Java Programs
Lecture 2: Operations and Data Types
Primitive Data, Variables, Loops (Maybe)
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
Building Java Programs Chapter 2
What shape am I? I am a plane shape. I have 4 sides.
Lecture 3: Expressions and Variables
Building Java Programs
Building Java Programs
Building Java Programs
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
Building Java Programs
Data Types and Expressions
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Data Types and Expressions
Building Java Programs
Building Java Programs
Lecture 4: Expressions and Variables
Building Java Programs
Building Java Programs
Compound Area Compound area is where a shape can be made up of other shapes. The area of a compound shape can be found by calculating the area of the shapes.
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Completing the Square.
Building Java Programs
Building Java Programs
Data Types and Expressions
Building Java Programs
Data Types and Expressions
2D shapes.
Presentation transcript:

BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION

22 VARIABLES

33 VARIABLE DECLARATION Declaring a variable tells Java to set aside a place to store a value. Variables must be declared before they can be used. Syntax: ;// The name is an identifier. int x; double myGPA; x myGPA

44 VARIABLE ASSIGNMENT Assigning a value to a variable updates its contents, dumping out whatever used to be there. The value can be an expression: the variable stores the result. Syntax: = ; int myFirstVariable; myFirstVariable = 307; myFirstVariable = ; myFirstVariable 307 myFirstVariable -7 myFirstVariable

55 VARIABLE ASSIGNMENT Assignment uses = It is not an algebraic equation. = means: "store the value at right in variable at left“ The right side expression is evaluated first, and then its result is stored in the variable at left. What happens here? int x = 3; x = x + 2; // ??? 3X3X 5X5X

66 EXPRESSIONS WITH VARIABLES Variables can stand in for values in expressions. double cookiesPerMinute = 3.5; int cookiesInJar = 12; double totalMinutes = cookiesInJar / cookiesPerMinute; int minutes = (int) totalMinutes; int seconds = ((int) (totalMinutes * 60)) % 60; totalMinutes 3 minutes 25 seconds

77 SHORTHAND ARITHMETIC OPERATORS Some operations are so commonly done that shorthand operators are included in Java. total = total + 33;=>total += 33; price = price * taxRate;=>price *= taxRate; numAwards = numAwards + 1;=> ++numAwards; or numAwards++; cookiesInJar = cookiesInJar – 1;=> --cookiesInJar; or cookiesInJar--;

88 EXPANDED PRECEDENCE TABLE DescriptionOperators unary operators +, -, ++, -- multiplicative operators *, /, % additive operators +, - assignment operators=, +=, -=, *=, /=, %= This table is on page 86 of your book

99 PROGRAMMING PRACTICE Write a program that will calculate the percent you earned on your Monday quiz. Declare variables for percent, points earned, points possible

10 STRING CONCATENATION Like “adding” strings together, it uses the + or += operators. String myMessage = "Hello" + " and welcome"; myMessage += " to my party!"; myMessage += "\nIt's been going on for " " years straight!"; System.out.println(myMessage); Hello and welcome to my party! It's been going on for 3 years straight!

11 EXAMPLE "2 + 2" "2 + 23" + 4 " "

12 EXAMPLE " 2 + 2" 7 + " 2 + 2" " "

13 EXAMPLE 2 + " times " " = " + 2 * " times " " = " + 6 "2 times " " = " + 6 "2 times 3" + " = " + 6 "2 times 3 = " + 6 "2 times 3 = 6"

14 IN YOUR NOTEBOOK… 2 + " plus " " = " "2 plus " " = " … "2 plus 3 = 23" How do you fix it? 2 + " plus " " = " + (2 + 3) … "2 plus 3 = 5"

15 PROGRAMMING PRACTICE 2 Create a Java class called Area that has the following methods: Square Rectangle Triangle Circle Sphere Each method should declare 1-2 variables (one for base, height, length, etc.), initialize the variables to 1, and print the area or surface area of the shape. Have the main method call each shape method to test that your program works!