BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

15
BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION

Transcript of BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

Page 1: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

BUILDING JAVA PROGRAMSCHAPTER 2VARIABLES AND STRING CONCATENATION

Page 2: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

22

VARIABLES

Page 3: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

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:<type> <name>; // The name is an identifier.

int x;

double myGPA;

x

myGPA

Page 4: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

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:<name> = <expression>;

int myFirstVariable;

myFirstVariable = 307;

myFirstVariable = -3 - 4;

myFirstVariable

307myFirstVariable

-7myFirstVariable

Page 5: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

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; // ???

3X

5X

Page 6: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

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;

3.429totalMinutes

3minutes

25seconds

Page 7: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

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

Page 8: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

88

EXPANDED PRECEDENCE TABLE

Description Operators

unary operators +, -, ++, --

multiplicative operators *, /, %

additive operators +, -assignment operators =, +=, -=, *=, /=, %=

This table is on page 86 of your book

Page 9: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

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

Page 10: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

1010

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 " + 3 + " years straight!";

System.out.println(myMessage);Hello and welcome to my party!It's been going on for 3 years straight!

Page 11: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

1111

EXAMPLE

"2 + 2" + 3 + 4

"2 + 23" + 4

"2 + 234"

Page 12: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

1212

EXAMPLE

3 + 4 + " 2 + 2"

7 + " 2 + 2"

"7 2 + 2"

Page 13: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

1313

EXAMPLE

2 + " times " + 3 + " = " + 2 * 3

2 + " times " + 3 + " = " + 6

"2 times " + 3 + " = " + 6

"2 times 3" + " = " + 6

"2 times 3 = " + 6

"2 times 3 = 6"

Page 14: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

1414

IN YOUR NOTEBOOK…

2 + " plus " + 3 + " = " + 2 + 3

"2 plus " + 3 + " = " + 2 + 3

"2 plus 3 = 23"

How do you fix it?

2 + " plus " + 3 + " = " + (2 + 3)

"2 plus 3 = 5"

Page 15: BUILDING JAVA PROGRAMS CHAPTER 2 VARIABLES AND STRING CONCATENATION.

1515

PROGRAMMING PRACTICE 2

Create a Java class called Area that has the following methods:

• Square

• Rectangle

• Triangle

• Circle

• SphereEach 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!