/Users/lyon/j4p/src/bookExamples/ch03Ops/Operations.java
|
1 package bookExamples.ch03Ops;
2
3
4 public class Operations {
5 public static void main(String args[]) {
6 parenthesesDemo();
7 memberSelectionDemo();
8 separationDemo();
9 postIncrementDemo();
10 postDecrementDemo();
11 preIncrementDemo();
12 preDecrementDemo();
13 plusDemo();
14 minusDemo();
15 multiplicationDemo();
16 divisionDemo();
17 modulusDemo();
18 additionDemo();
19 subtractionDemo();
20 equalsDemo();
21 plusEqualsDemo();
22 minusEqualsDemo();
23 multiplyEqualsDemo();
24 divideEqualsDemo();
25 modulusEqualsDemo();
26 }
27
28 public static void parenthesesDemo() {
29 int x = 4, y = 2, z = 1;
30 hr();
31 print("x = 4, y = 2, and z = 1"
32 + "\nso when you calculate "
33 + "x * (y + z)"
34 + "the result is "
35 + (x * (y + z)));
36 print("x = 4, y = 2, and z = 1"
37 + "\nso when you calculate"
38 + " (x * y) + z the result is "
39 + ((x * y) + z));
40 }
41
42 public static void memberSelectionDemo() {
43
44 int x = 0;
45 hr();
46 print("if we call Math.cos(x) when x = 0 "
47 + "using the member"
48 + "selection\nwe can "
49 + "invoke method "
50 + "cos of the Math class, "
51 + "the result is "
52 + Math.cos(x));
53 }
54
55 public static void separationDemo() {
56 int x = 4, y = 2;
57 hr();
58 print("if we call Math.pow(x,y) "
59 + "when x = 4 "
60 + "and y = 2 using the "
61 + "separation"
62 + "\nwe can invoke method "
63 + "pow of the Math class,"
64 + "the result is " + Math.pow(x, y));
65 }
66
67 public static void plusDemo() {
68 int x = 4;
69 hr();
70 print("x = 4 but +x = "
71 + (+x));
72 }
73
74 public static void minusDemo() {
75 int x = 4;
76 hr();
77 print("x = 4 but -x = "
78 + (-x));
79 }
80
81 public static void postIncrementDemo() {
82 int x = 0;
83 hr();
84 print("x = 0 but x++ = "
85 + x++);
86 print("after x++, x = "
87 + x);
88 }
89
90 public static void postDecrementDemo() {
91 int x = 0;
92 hr();
93 print("x = 0 but x-- = "
94 + x--);
95 print("after x--, x = "
96 + x);
97 }
98
99 public static void preIncrementDemo() {
100 int x = 0;
101 hr();
102 print("x = 0 but ++x = "
103 + ++x);
104 print("after ++x, x = "
105 + x);
106 }
107
108 public static void preDecrementDemo() {
109 int x = 0;
110 hr();
111 print("x = 0 but --x = "
112 + --x);
113 print("after --x, x = "
114 + x);
115 }
116
117 public static void additionDemo() {
118 int x = 4, y = 2;
119 hr();
120 print("x = 4 and y = 2"
121 + "\nso x + y = "
122 + (x + y));
123 }
124
125 public static void subtractionDemo() {
126 int x = 4, y = 2;
127 hr();
128 print("x = 4 and y = 2"
129 + "\nso x - y = "
130 + (x - y));
131 }
132
133 public static void multiplicationDemo() {
134 int x = 4, y = 2;
135 hr();
136 print("x = 4 and y = 2\nso x * y = "
137 + (x * y));
138 }
139
140 public static void divisionDemo() {
141 int x = 4, y = 2;
142 hr();
143 print("x = 4 and y = 2\nso x / y = "
144 + (x / y));
145 }
146
147 public static void modulusDemo() {
148 int x = 4, y = 2;
149 hr();
150 print("x = 4 and y = 2"
151 + "\nso x % y = " + (x % y));
152 }
153
154 public static void equalsDemo() {
155 int x = 4, y = 2;
156 hr();
157 print("x = 4, y = 2\nso x = y "
158 + "makes x equal to "
159 + (x = y));
160 }
161
162 public static void plusEqualsDemo() {
163 int x = 4, y = 2;
164 hr();
165 print("x = 4 and y = 2\nso x += y makes"
166 + "x equal to " + (x += y));
167 }
168
169 public static void minusEqualsDemo() {
170 int x = 4, y = 2;
171 hr();
172 print("x = 4 and y = 2"
173 + "\nso x -= y "
174 + "makes x equal to "
175 + (x -= y));
176 }
177
178 public static void multiplyEqualsDemo() {
179 int x = 4, y = 2;
180 hr();
181 print("x = 4 and y = 2"
182 + "\nso x *= y makes "
183 + "x equal to "
184 + (x *= y));
185 }
186
187 public static void divideEqualsDemo() {
188 int x = 4, y = 2;
189 hr();
190 print("x = 4 and y = 2"
191 + "\nso x /= y makes x equal to "
192 + (x /= y));
193 }
194
195 public static void modulusEqualsDemo() {
196 int x = 4, y = 2;
197 hr();
198 print("x = 4 and y = 2\n"
199 + "so x %= y makes x equal to "
200 + (x %= y));
201 }
202
203 public static void hr() {
204 print("____________________________");
205 }
206
207 public static void print(Object o) {
208 System.out.println(o);
209 }
210 }
211