-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbernjav
More file actions
347 lines (277 loc) · 10.2 KB
/
Copy pathbernjav
File metadata and controls
347 lines (277 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package week04;
/**
* Created by shuxford on 25/12/2015.
*/
public class DoingIt {
Window myWindow;
public void doIt(Window win){
myWindow = win;
//ex13();
//ex14();
//ex22();
//ex23();
//ex24();
//ex25();
//ex26();
//ex28();
//assignmentIsAnOperator();
//arithmeticOperations();
//equalityRelationalOperators();
//equalityRelationalOperators2();
//logicalOperators();
//redundantSyntax();
//blockScopeTest();
}
//LAB EXERCISES for you to code
private void ex13(){
boolean a = true;
boolean b = false;
boolean c;
c=a;
a=b;
b=c;
myWindow.clearOut();
myWindow.writeOutLine(a);
myWindow.writeOutLine(b);
}
private void ex14(){
myWindow.clearOut();
int a, b, c;
double discriminant, numerator1, numerator2, denominator, root1, root2;
a = 3;
b = -9;
c = 2;
discriminant = (double) b * b - 4 * a * c;
denominator = (double) 1/ (2 * a);
numerator1 = -b + Math.sqrt(discriminant);
numerator2 = -b - Math.sqrt(discriminant);
root1 = numerator1 * denominator;
root2 = numerator2 * denominator;
myWindow.writeOut("The two roots of the equations are x = " + (float)root1 + " , " +(float)root2 );
}
private void ex22(){
int n;
Boolean result;
n=-1233;
result = (n % 2 == 0);
myWindow.clearOut();
myWindow.writeOutLine(result);
n=2;
result = (n % 2 == 0);
myWindow.writeOutLine(result);
n=1;
result = (n % 2 == 0);
myWindow.writeOutLine(result);
n=300;
result = (n % 2 == 0);
myWindow.writeOutLine(result);
n=-1234;
result = (n % 2 == 0);
myWindow.writeOutLine(result);
}
private void ex23(){
int n;
Boolean result;
n=-1233;
result = (n % 2 != 0 && n<0);
myWindow.clearOut();
myWindow.writeOutLine(result);
n=2;
result = (n % 2 != 0 && n<0);
myWindow.writeOutLine(result);
n=1;
result = (n % 2 != 0 && n<0);
myWindow.writeOutLine(result);
n=301;
result = (n % 2 != 0 && n<0);
myWindow.writeOutLine(result);
n=-1234;
result = (n % 2 != 0 && n<0);
myWindow.writeOutLine(result);
}
private void ex24(){
int n=1;
boolean result;
result = (n>1 && n<=100 || n%2!=0 && n>40 && n<=50);
myWindow.clearOut();
myWindow.writeOutLine(result);
n=-2;
result = (n>1 && n<=100 || n%2!=0 && n>40 && n<=50);
myWindow.writeOutLine(result);
n=47;
result = (n>1 && n<=100 || n%2!=0 && n>40 && n<=50);
myWindow.writeOutLine(result);
n=2;
result = (n>1 && n<=100 || n%2!=0 && n>40 && n<=50);
myWindow.writeOutLine(result);
n=101;
result = (n>1 && n<=100 || n%2!=0 && n>40 && n<=50);
myWindow.writeOutLine(result);
}
private void ex25(){
String input;
input = myWindow.readIn();
boolean result;
result = input.equals("Y") || input.equals("y") || input.equals("N") || input.equals("n");
myWindow.clearOut();
myWindow.writeOut(result);
}
private void ex26(){
String input;
input = myWindow.readIn();
boolean result;
result = !input.equals("done");
myWindow.clearOut();
myWindow.writeOut(result);
}
private void ex28(){
int year = 2020;
boolean division, century, yearly;
division = year % 4 == 0;
century = (year % 100 == 0) && (year % 400 == 0 );
yearly = (year % 100 != 0 );
myWindow.clearOut();
myWindow.writeOutLine(division && (century || yearly));
}
//WORKSHOP CODE for you to play with
private void assignmentIsAnOperator(){
int testInt;
myWindow.clearOut();
myWindow.writeOutLine(testInt = 123);
}
private void arithmeticOperations(){
//using literals to keep things clear but could have used variables
int i1, i2, i3; //these are reused multiple times for multiple uses in this method which is poor style in real code
myWindow.clearOut();
myWindow.writeOutLine(1.0 / 2.0);
myWindow.writeOutLine(1 / 2);
myWindow.writeOutLine((double) 1 / 2);
myWindow.writeOutLine(7 % 3);
myWindow.writeOutLine(7 % 8);
// int counter = 0;
// counter = counter + 1;
// ++counter; //pre increment
// counter++; //post increment
//
// counter = counter - 1;
// --counter; //pre decrement
// counter--; //post decrement
myWindow.writeOutLine("");
i1 = 1; i2 = 2;
myWindow.writeOutLine(i1 + ++i2);
i1 = 1; i2 = 2;
myWindow.writeOutLine(i1 + i2++);
i1 = 1; i2 = 2;
myWindow.writeOutLine(i1 + --i2);
i1 = 1; i2 = 2;
myWindow.writeOutLine(i1 + i2--);
double totalSales = 0, thisSale;
thisSale = 5.50;
totalSales += thisSale;
myWindow.writeOutLine(totalSales);
thisSale = 7.50;
totalSales += thisSale;
myWindow.writeOutLine(totalSales);
// totalSales = totalSales + thisSale;
// totalSales += thisSale;
myWindow.writeOutLine("");
double pi = Math.PI; //no parenthesis after PI ???
myWindow.writeOutLine(Math.floor(pi));
myWindow.writeOutLine(Math.pow(2.0, 20)); //1MByte
//double calculations are inaccurate
myWindow.writeOutLine(Math.sin(2.0 * pi));
myWindow.writeOutLine(Math.abs(-pi));
myWindow.writeOutLine("");
i1 = 1; i2 = 2; i3 = 3;
myWindow.writeOutLine(i1 + i2 * i3);
myWindow.writeOutLine((i1 + i2) * i3);
}
private void equalityRelationalOperators(){
double d1 = 1.23, d2 = 3.45;
double delta = 0.00000001;
String s1 = "dog", s2;
s2 = myWindow.readIn(); //assume the string dog is input
myWindow.clearOut();
//remember floating points are stored inaccurately
myWindow.writeOutLine("d1 == d2: " + (d1 == d2)); //false, maybe
myWindow.writeOutLine("d1 != d2: " + (d1 != d2)); //true, maybe
//inner parentheses essential in the next statement. Why?
myWindow.writeOutLine("d1 < d2: " + (d1 < d2)); //true, maybe
myWindow.writeOutLine("d1 <= d1: " + (d1 <= d1)); //true, maybe
//allowing for floating point inaccuracy
myWindow.writeOutLine((d2 - d1) < delta);
myWindow.writeOutLine("s1 == s2: " + (s1 == s2)); //false, huh!!!
}
private void equalityRelationalOperators2(){
String s1 = "dog", s2, s3 = "dOg", s4 = "dogs)";
s2 = myWindow.readIn(); //the string dog is input
myWindow.clearOut();
myWindow.writeOutLine("s1.equals(s2): " + s1.equals(s2)); //hooray!
myWindow.writeOutLine("!s1.equals(s2): " + !s1.equals(s2)); //
myWindow.writeOutLine("s1.equals(s3): " + s1.equals(s3)); //case sensitive
//next statement: true, case insensitive
myWindow.writeOutLine("s1.equalsIgnoreCase(s3): " + s1.equalsIgnoreCase(s3));
myWindow.writeOutLine("s1.compareTo(s4): " + s1.compareTo(s4)); // -ve
myWindow.writeOutLine("s4.compareTo(s1): " + s4.compareTo(s1)); // +ve
myWindow.writeOutLine("s1.compareTo(s2): " + s1.compareTo(s2)); // 0
myWindow.writeOutLine("s1.compareTo(s3): " + s1.compareTo(s3)); // +ve
//next statement: 0 i.e. s1 = s2
myWindow.writeOutLine("s1.compareToIgnoreCase(s3): " + s1.compareToIgnoreCase(s3));
}
private void logicalOperators(){
int low = 50, high = 100; //inclusive
int testValue1 = 99, testValue2 = 101;
boolean firstTest, secondTest;
myWindow.clearOut();
firstTest = testValue1 >= low && testValue1 <= high; //in test
secondTest = testValue1 < low || testValue1 > high; //out test
myWindow.writeOutLine("testValue1 in range: " + firstTest);
myWindow.writeOutLine("testValue1 out of range: " + secondTest);
myWindow.writeOutLine("");
firstTest = testValue2 >= low && testValue2 <= high; //in test
secondTest = testValue2 < low || testValue2 > high; //out test
myWindow.writeOutLine("testValue2 in range: " + firstTest);
myWindow.writeOutLine("testValue2 out of range: " + secondTest);
//short circuiting
int i = 0;
myWindow.writeOutLine("");
myWindow.writeOutLine(5 < 3 && (++i > 100));
myWindow.writeOutLine(i);
}
private void redundantSyntax(){
boolean myBoolean = true;
boolean result;
result = myBoolean = true;
result = myBoolean != true;
}
private void blockScopeTest(){
int outer = 1;
myWindow.clearOut();
myWindow.writeOutLine("outer: " + outer);
//myWindow.writeOutLine("inner1: " + inner1); //inner1 not in scope
//myWindow.writeOutLine("inner2: " + inner2); //inner2 not in scope
{
int inner1 = 2;
myWindow.writeOutLine("outer: " + outer);
myWindow.writeOutLine("inner1: " + inner1);
//myWindow.writeOutLine("inner2: " + inner2); //inner2 not in scope
}
{
int inner2 =3;
myWindow.writeOutLine("outer: " + outer);
//myWindow.writeOutLine("inner1: " + inner1); //inner1 not in scope
myWindow.writeOutLine("inner2: " + inner2);
}
}
// =========================================================================
// UTILITIES ===============================================================
// =========================================================================
private static boolean isNumeric(String str){
return str.matches("-?\\d+(\\.\\d+)?"); //match a number with optional '-' and decimal.
}
private static int getRandom(int n1, int n2){
int retVal = 0;
retVal = n1 + (int) Math.floor(Math.random() * (n2 - n1 + 1));
return retVal;
}
}