I
treat arithmetic operations in the following order:
- Multiplication (*) and division
(/)
- Remainder (%)
- Addition (+) and
subtraction (-)
For example: 2*3+5=11
But if you want to calculate 3+5 first then multiply 2? What should
you do?
Using grouping operators
- parentheses ()
It is like 2*(3+5)=16.
But remember , inside parentheses () I
still follow the above order.
For example: 2*(3*2+4)=20,
2*(3*(2+4))=36.
Let us pretend you are a satellite dish installer, working for Directv or dish
network. You have 118 receivers in hands. There are 30 houses
applying to install dish in town. 20 house have 3 bedrooms. 10
houses have 4 bedroom. Each bedroom needs one receiver. You want
me to calculate how many receivers are left after you installed
all 30 houses. OkieDokie Sir. As before, I will type code, compile and run.
class dishInstaller
{
public static void main(String[] args)
{
int NumberOf_3_Bedroom_House=20;
int NumberOf_4_Bedroom_House=10;
int TotalReceivers=100;
int NumberOfLeftReceiver=100%(20*3+10+4);
System.out.println("Number of left dish receivers="+NumberOfLeftReceiver);
}
}
Save it as dishInstaller.java. Compile: javac
dishInstaller.java. Then run it : java
dishInstaller. I get
: Number of left dish
receivers=16.
HomeWork
Correct? OK. You are qualified for DirecTV or dish network now.
Go there and apply for a job.
Let's continue with that satellite dish installer project. This
time suppose you have 126 receivers. After you installed all those
houses? How many receivers left?
If DirecTV ask you to install more 4-bedroom houses by using the
left receivers, how many more houses you could install?
if dish network ask you to install 3-bedroom houses by using the
left receivers,
how many more houses you could install?
(Solution is here) |