forked from pdeitel/JavaFullThrottle-pre2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaFullThrottleCopyPasteFile.java
More file actions
210 lines (156 loc) · 5.87 KB
/
JavaFullThrottleCopyPasteFile.java
File metadata and controls
210 lines (156 loc) · 5.87 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
// Java Full Throttle Copy/Paste File
// Updated April 28, 2018
// Chapter numbers correspond to the chapters of Java How to Program, 11/e, Early Objects Version
// In JShell
/set feedback verbose
/***************************************************************
* Chapter 1: Test Drive
***************************************************************/
/***************************************************************
* Chapter 2: Intro to Java
***************************************************************/
/***************************************************************
* Chapter 3: Intro to Classes
***************************************************************/
/***************************************************************
* Chapter 4: Control Statements 1
***************************************************************/
int grade = 80
// if statement
if (grade >= 60) {
System.out.println("Passed");
}
// if/else
if (grade >= 60) {
System.out.println("Passed");
} else {
System.out.println("Failed");
}
// nested if/else
if (grade >= 90) {
System.out.println("A");
} else if (grade >= 80) {
System.out.println("B");
} else if (grade >= 70) {
System.out.println("C");
} else if (grade >= 60) {
System.out.println("D");
} else {
System.out.println("F");
}
// while: first power of 3 greater than 100
int product = 3;
while (product <= 100) {
product = 3 * product;
}
// Conditional Expression
int grade = 80;
System.out.println(grade >= 60 ? "Passed" : "Failed");
/***************************************************************
* Chapter 5: Control Statements 2
***************************************************************/
// for
for (int counter = 1; counter <= 10; counter++) {
System.out.printf("%d ", counter);
}
// do/while
int counter = 1;
do {
System.out.printf("%d ", counter);
++counter;
} while (counter <= 10);
int grade = 77;
// switch
switch (grade / 10) {
case 9: // grade was between 90
case 10: // and 100, inclusive
System.out.println("Grade is A");
break; // exits switch
case 8: // grade was between 80 and 89
System.out.println("Grade is B");
break; // exits switch
case 7: // grade was between 70 and 79
System.out.println("Grade is C");
break; // exits switch
case 6: // grade was between 60 and 69
System.out.println("Grade is D");
break; // exits switch
default: // grade was less than 60
System.out.println("Grade is F");
break; // optional; exits switch anyway
}
String grade = "A";
// switch
switch (grade) {
case "A":
System.out.println("GPA is 4.0");
break;
case "B":
System.out.println("GPA is 3.0");
break;
case "C":
System.out.println("GPA is 2.0");
break;
case "D":
System.out.println("GPA is 1.0");
break;
default:
System.out.println("GPA is 0.0");
break;
}
/***************************************************************
* Chapter 6: Methods
***************************************************************/
/***************************************************************
* Chapter 7: Arrays and ArrayLists
***************************************************************/
/***************************************************************
* Chapter 8: Classes--A Deeper Look
***************************************************************/
/***************************************************************
* Chapter 9: Inheritance
***************************************************************/
Object class documentation
https://docs.oracle.com/javase/11/docs/api/java/lang/Object.html
/***************************************************************
* Chapter 10: Polymorphism
***************************************************************/
/***************************************************************
* Chapter 11: Exception Handling
***************************************************************/
/***************************************************************
* Chapter 12: JavaFX 1
***************************************************************/
/***************************************************************
* Chapter 13: JavaFX 2
***************************************************************/
/***************************************************************
* Chapter 16: Generic Collections
***************************************************************/
// JSHELL: Type Wrappers
Integer x = new Integer(7);
x.<tab> // shows methods
// JSHELL: boxing
Integer x = 7;
x
int y = x;
y
/***************************************************************
* Chapter 17: Lambdas and Streams
***************************************************************/
// Fig. 17.7 modified pipeline
IntStream.rangeClosed(1, 10)
.filter(
x -> {
System.out.printf("%nfilter: %d%n", x);
return x % 2 == 0;
})
.map(
x -> {
System.out.println("map: " + x);
return x * 3;
})
.sum()
/***************************************************************
* Chapter 23: Concurrency and Parallel Streams
***************************************************************/