Variables and Arithmetic

advertisement

More With Variables

Assignments

Variables can be given any value that matches their type.

The Java statement that gives a variable its value

is called an assignment statement.

After a variable is given a value, the variable is

said to be initialized.

These aren’t equations! Read “=” as “gets” deposit = 4; my_num = 3.4; name = “CSC 130”;

Arithmetic

 You can use the assignment operator (=) and math operators (*,/,+,-) to do arithmetic.

 Remember your order of operations! (PEMDAS)

 The thing on the left gets changed.

 value = 4+3*10;

 interest = 3.0/2.0;

 due = value+interest;

The right-hand sides are expressions , just like in math.

Arithmetic Operators

 The following table summarizes the arithmetic operators available in Java.

This is an integer division where the fractional part is truncated.

Constants

 We can change the value of a variable. If we want the value to remain the same, we use a constant .

final double PI = 3.14159; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060;

The reserved word final is used to declare constants.

These are constants, also called named constant

.

These are called literal constant.

Variable Scope

 Block of code: Denoted by { }

Scope: The area of code in which a variable is available for use

A variable’s scope is the block in which it is declared

The rules

1.

2.

3.

4.

5.

You must declare and initialize a variable before using it on the right of a rule.

Only the variable to the left of the = gets changed

There can be one and only one variable on the left.

You cannot put data into a variable that does not match the variables’ type.

You can only have one variable with any given name in a particular block.

This Ain’t Math Class

 Assignment statements are NOT math equations!

 count = count + 1;

 These are commands!

 value = 2;

 interest = value;

 value = value + 3;

 What’s the value of interest?



This Ain’t Math Class

 There’s no such thing as: z

3 x

4( y

12)

Two Types of Division

Double Division: no rounding

 3.0 / 6.0

 6.0 / 3.0

 x/1.5

 At least one number must have a decimal

 Integer Division: round DOWN

 3/6

 6/3

 x/y, if both x and y are ints

 Both numbers are integers

Division Practice

(P.S. This Ain’t Math Class!)

 int x = 6/4;

 int y = 4/6;

 double z = 4/6;

 double a = 6/4;

 double b = 6/12.0;

 int c = 6.0/12;

Modulo Operator

 Modular Arithmetic: Remainder from division

 Works with integers only

Operator is % (NOT PERCENT!)

6 % 4 is read “six mod four”

 3 % 6 =

 7 % 2 =

 7 % 14 =

 14 % 7 =

Brainstorm

What useful thing does % 10 do?

 3 % 10 =

 51 % 10 =

 40 % 10 =

 678 % 10 =

What useful thing does /10 do (integer division?)

 3/10 =

 51/10 =

 40/10=

 678/10 =

What useful thing does % 2 do?

Trick #1 - Casting

 To change a variable’s type, you can cast from one type to another

 int x = 4;

 double y = 10/(double)x;

 This is illegal:

 int x = 5.5;

 This is legal:

 int x = (int)5.5;

Trick # 2

 Increment Operator

 x =x + 1;

 x++;

 Decrement Operator

 x = x – 1;

 x--;

 And others:

 x += 2;

 amount *= 1.05;

Trick #3

 Random numbers – generate with the

Math object.

 Random double between x and y: double rand = Math.random()*(y-x)+x;

 Random int between x and y (inclusive): int rand = (int)(Math.random()*(y-x+1)+x);

Be careful with parentheses!

Printing

 You can print to the console window:

 System.out.println(“Hello!”);

 String word = “Hey”;

 System.out.println(word);

 System.out.println(45+5);

 int y = 45+5;

 System.out.println(y);

 System.out.println(“y”);

 System.out.println(“The answer is “+y);

 System.out.print(“Stay on this line “);

 System.out.print(“and keep printing.”);

Practice

 Average three numbers

 Fahrenheit to Celsius

 C = (5/9)(F-32)

 Given a big number, (> 1000), print its last three digits in reverse order (Ex: if input is

1234, prints out 432)

Download