TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ Name:
Date:
Period:
Arithmetic Operators What is an Arithmetic Operator? First off, operators are simply symbols used to manipulate values within computer programs. Most languages use similar operators, e.g., + , , / , * , etc. to allow programmers to perform various computations; many such operators are strikingly similar to those commonly used in mathematical equations . In Computer Science, operators are used to form expressions which are evaluated by a computer. Anything that acts upon one or more values is called an operator; values being acted upon are called operands. Java’s arithmetic operators — operators that act upon numerical operands, i.e., literals or variables, and return a single numerical value — are listed in Figure 1.
+
Additive (also used for String concatenation)
*
Multiplication
-
Subtraction
/
Division
%
Modulus a.k.a. Remainder Figure 1
As is true with most technical disciplines, half the battle is in mastering the terminology . Refer to Figure 2, an example which diagrams the operator and the operands within a Java expression.
Figure 2 Page 1 of 3
TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ Examples and Practice The best thing about Netflix? Binge-watching your favorite shows - without commercials! Netflix has definitely changed the way many of us watch TV, and for some, just how much of it we watch. To start, you need to know that the average TV show season is 13 episodes long; each show, on average, lasts 47 minutes . Study the following code snippets and complete the Java expressions as necessary to answer each question. For each, two variables have already been declared for you (these may or may not be needed in writing your expression). Note: The first and third questions have already been completed; refer to these as models. 1. How many minutes worth of commercials does the average hour-long program have?
int showsPerSeason = 13, minsPerShow = 47; int commercialMins = 60 - minsPerShow;
2. How many minutes of content d oes a season of your favorite show consist of?
int showsPerSeason = 13, minsPerShow = 47; int seasonMins =
3. Referring to the previous question, approximately how many hours d oes the season last?
int showsPerSeason = 13, minsPerShow = 47; double seasonHrs = (double) (showsPerSeason * minsPerShow) / 60;
In the example above, notice the (double) ; this is needed to make sure that the result of (showsPerSeason * minsPerShow) is treated as a number with a fractional, i.e., decimal, value. Page 2 of 3
TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________
4. Using the previous question as your model, approximately how many days does the season last?
int showsPerSeason = 13, minsPerShow = 47;
5. You watched seven episodes of your favorite show on Saturday and another six on Sunday; how many minutes of commercials did you spare yourself by watching on Netflix?
int showsPerSeason = 13, minsPerShow = 47;
6. In your own words, describe the difference between operators and operands .
7. Match the following mathematical equations with their Java equivalents by writing the corresponding letter in the space provided.
4 ÷ 2 = 2
_____
a. int x = 4 / 2;
4 × 2 = 8
_____
b. double x = 4 * 1.67
4 ÷ 3 = 1.33
_____
c. double x = 4 / 3;
4 × 1.67 = 6.68
_____
d. int x = 4 * 2;
Page 3 of 3