Download Note - Arithmetic Expressions

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ICS3U1 – Unit 1
Name: _______________
Arithmetic Expressions
Arithmetic Operators
Each data type has operators that we can use to manipulate values.
An operator is a symbol that denotes some sort of operation. For instance, we all know that +
denotes the addition operation in mathematics. The + operator requires two operands – that
is, two numbers on which to perform the operation.
Java has several built-in operators to work with numeric data types (int and double).
Our basic arithmetic operators are as follows:
Operator
+
*
/
()
Meaning
Denotes addition between two
numbers.
Denotes subtraction between
two numbers.
Denotes multiplication between
two numbers
Denotes division between two
numbers.
Used to affect the order of
operations (BEDMAS)
All of the above operators work as you would expect, but division has some catches:
1) If you divide two int values, Java will always round the answer down (24 / 5 will yield 4).
2) If you want a double as your answer to a division statement, at least one of your operands
must be a double!
Command
System.out.println (13 / 2);
System.out.println (13.0 / 2);
System.out.println (13 / 2.0);
System.out.println (13.0 / 2.0);
Output
6
6.5
6.5
6.5
Modulo Operator
Java also has an operator used for determining remainders. The “%” operator computes the
remainder when its first integer argument is divided by its second.
For example, 19 % 4 returns 3 because the number 4 goes in to 19 four times, leaving a
remainder of 3.
ICS3U1 – Unit 1
Name: _______________
Assigning Expressions to Variables
We’ve seen how to declare and initialize variables. Often times what we want to store in a
variable is dependent on other variables. This can be obtain by writing an expression.
For example, we might want to store the area of a circle in a variable:
// Create a variable to store the radius of the circle.
double radius = 1.5;
// Calculate the area of the circle using the well-known formula.
double area = 3.14 * radius * radius;
Expressions are used so often that the developers of Java built shortcuts for some common
Operations.
Operator
Expression
Meaning
+=
-=
*=
/=
x += y
x -= y
x *= y
x /= y
x=x+y
x=x–y
x=x*y
x=x/y
The above arithmetic operators are binary operators as they require two operands.
Example: The following code demonstrates the above operations.
ICS3U1 – Unit 1
Name: _______________
Prefix and Postfix Operators
Java also has a shortcut for incrementing and decrementing an int by one (this will be useful
later!).
The ++ and -- operators come in two flavours:
Operator
Expression
Meaning
++
x++
++x
x=x+1
x=x+1
--
x---x
x=x-1
x=x-1
You should all be thinking “Wait a minute, what’s the difference between x++ and ++x?”.
In the case of x++, the operator ++ is a postfix operator. This means that the value of x is
incremented at the termination of that line of code.
The prefix operator looks like “++x”, in which case the value of x is incremented immediately
(before execution of the line).
Example: The following code demonstrates the prefix and postfix operators.
Arithmetic Expressions – Problem Set
1. Calculate and output the integers from 10 to 0, using the -- operator. Save your program as
Countdown.java.
2. Calculate and output all the following pattern using either the += or *= operators (or a
combination of the two):
1
2
Save your program as Dublin.java.
4
8
16
32
ICS3U1 – Unit 1
Name: _______________
3. Calculate and output the following pattern (using each arithmetic operator at least once):
1
5
6
3
9
10
6
1
Save your program as Pattern1.java.
4. Using 2 variables and showing only 2 numbers in the entire program (in the two assignment
statements), calculate and output the following pattern:
1
2
4
7
11
16
Hint: Determine the pattern in the numbers first. This will help you determine the pattern of
operations required.
Save your program as Pattern2.java.
5. Write a program that declares and initialize an integer with a number between 1 and 1000.
Your program will then output the coins used to make change for the given number. For
example, if your number is 99, then your program should output 3 quarters, 2 dimes, and 4
pennies. Note: Use the % operator. Your program should still work with other numbers.
Save your program as MakingChange.java.
6. Bonus: This problem was asked of me during an interview with Research in Motion, the
makers of the Blackberry smartphone. It’s tricky but within your reach!
Let x and y be int variables. Show how to swap the values of x and y without using a third
variable.
Save your program as Swap.java.