-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference_sheet.tex
More file actions
289 lines (256 loc) · 7.76 KB
/
reference_sheet.tex
File metadata and controls
289 lines (256 loc) · 7.76 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
\documentclass[10pt]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{multicol}
\usepackage{xcolor}
\definecolor{titlecolor}{RGB}{0,0,128}
\usepackage{titlesec}
\usepackage{enumitem}
\usepackage{listings}
\usepackage{tcolorbox}
\setlength{\columnsep}{0.3in}
\pagestyle{empty}
% Define custom colors
\definecolor{codebg}{rgb}{0.97,0.97,1.0}
\definecolor{keywordcolor}{rgb}{0.4,0.1,0.6}
\definecolor{commentcolor}{rgb}{0.6, 0.4, 0.8}
\definecolor{stringcolor}{rgb}{0.1,0.3,0.6}
% Listings setup
\lstset{
backgroundcolor=\color{codebg},
basicstyle=\ttfamily\footnotesize,
breaklines=true,
frame=single,
showstringspaces=false,
tabsize=2,
language=Java,
keywordstyle=\color{keywordcolor}\bfseries,
commentstyle=\color{commentcolor}\itshape,
stringstyle=\color{stringcolor}
}
% Section title formatting
\titleformat{\section}{\large\bfseries\color{titlecolor}}{antiquefuchsia}{0em}{}
\titleformat{\subsection}{\normalsize\bfseries\color{black}}{}{0em}{}
\begin{document}
\begin{center}
\Large \textbf{Java Programming II - Reference Sheet} \\
\end{center}
\vspace{0.2cm}
\begin{multicols}{2}
% Java Section
\section*{Java Essentials}
\subsection*{Instance Keyword}
\begin{lstlisting}
// "this" refers to the current object instance
public class Dog {
String name;
public Dog(String name) {
this.name = name;
}
}
\end{lstlisting}
\subsection*{Inheritance \& Overriding}
\begin{lstlisting}
// Method overriding with @Override
class Animal {
void speak() { System.out.println("Sound"); }
}
class Dog extends Animal {
@Override
void speak() { System.out.println("Bark"); }
}
\end{lstlisting}
\subsection*{Abstract \& Interface}
\begin{lstlisting}
// Abstract class has abstract method(s)
abstract class Shape {
abstract void draw();
}
// Interface contains only abstract methods by default
interface Drawable {
void draw();
}
\end{lstlisting}
\subsection*{Instanceof \& synchronized}
\begin{lstlisting}
// Check if object is an instance of a class
if (obj instanceof Dog) {
((Dog) obj).speak();
}
synchronized(obj) { /* safe code */ }
\end{lstlisting}
\subsection*{ArrayList}
\begin{lstlisting}
// Dynamic array
ArrayList<String> list = new ArrayList<>();
list.add("Hello");
\end{lstlisting}
\subsection*{Modifiers}
public – accessible everywhere \\
private – accessible within class only \\
protected – accessible within package and subclasses
\subsection*{String Methods \& Format}
\begin{lstlisting}
// trim() removes leading/trailing spaces
String name = " John ".trim().toUpperCase();
// Format a string with variables
String s = String.format("Age: %d", 20);
\end{lstlisting}
\subsection*{Parsing}
\begin{lstlisting}
// Convert String to int or double
int x = Integer.parseInt("5");
double d = Double.parseDouble("3.14");
\end{lstlisting}
% JavaFX Section
\section*{JavaFX Essentials}
\subsection*{App Structure}
\begin{lstlisting}
// JavaFX main app structure
public class App extends Application {
public void start(Stage s) {
VBox root = new VBox(); // layout container
s.setScene(new Scene(root, 300, 200));
s.setTitle("My App");
s.show();
}
}
\end{lstlisting}
\subsection*{Layouts}
\begin{lstlisting}
// VBox with spacing and alignment
VBox v = new VBox(10);
v.setAlignment(Pos.CENTER);
// GridPane with gaps
GridPane g = new GridPane();
g.setHgap(10); g.setVgap(10);
\end{lstlisting}
\subsection*{TextField, Button}
\begin{lstlisting}
// TextField with prompt and disabled editing/focus
TextField tf = new TextField();
tf.setPromptText("Enter");
tf.setEditable(false);
tf.setFocusTraversable(false);
// Button with action handler
Button btn = new Button("Click");
btn.setOnAction(e -> doSomething());
\end{lstlisting}
\subsection*{ImageView}
\begin{lstlisting}
// Load and display an image
Image img = new Image("file:img.png");
ImageView iv = new ImageView(img);
iv.setFitWidth(100);
iv.setPreserveRatio(true);
\end{lstlisting}
% Animations & Media
\section*{JavaFX Animation}
\begin{lstlisting}
// Fade animation: 1s duration, fully visible to 30% opacity
FadeTransition ft = new FadeTransition(Duration.seconds(1), node);
ft.setFromValue(1.0); ft.play();
// Move node by X axis
TranslateTransition tt = new TranslateTransition(Duration.seconds(1), node);
tt.setByX(100); tt.play();
// Rotate 180 degrees
RotateTransition rt = new RotateTransition(Duration.seconds(2), node);
rt.setByAngle(180); rt.play();
// Play multiple animations together
ParallelTransition pt = new ParallelTransition(rt, ft);
pt.play();
// Scale to 2x size
ScaleTransition st = new ScaleTransition(Duration.seconds(1), node);
st.setToX(2.0); st.setToY(2.0); st.play();
// Fill color from red to blue
FillTransition fill = new FillTransition(Duration.seconds(1), shape);
fill.setFromValue(Color.RED); fill.setToValue(Color.BLUE); fill.play();
// Stroke color from black to green
StrokeTransition stroke = new StrokeTransition(Duration.seconds(1), shape);
stroke.setFromValue(Color.BLACK); stroke.setToValue(Color.GREEN); stroke.play();
// Pause for 2 seconds
PauseTransition pause = new PauseTransition(Duration.seconds(2));
pause.setOnFinished(e -> System.out.println("Paused done!")); pause.play();
// Move along a path
Path path = new Path();
path.getElements().addAll(new MoveTo(0, 0), new LineTo(100, 100));
PathTransition pathT = new PathTransition(Duration.seconds(2), path, node); pathT.play();
// Play animations one after another
SequentialTransition seq = new SequentialTransition(ft, rt);
seq.play();
\end{lstlisting}
\subsection*{Event Handler}
\begin{lstlisting}[language=java]
button.setOnAction(e ->{
});
node.setOnKeyPressed(e ->{
});
mouse.setOnMouseClicked(e ->{
});
\end{lstlisting}
\section*{Media (Audio)}
\begin{lstlisting}
// Load and play audio file
Media m = new Media(new File("music.mp3").toURI().toString());
MediaPlayer mp = new MediaPlayer(m);
mp.play(); // Also: pause(), stop(), etc.
\end{lstlisting}
% File I/O and Networking
% File I/O
\section*{File I/O}
\begin{lstlisting}
// Read
Scanner sc = new Scanner(new File("file.txt"));
while(sc.hasNextLine()) System.out.println(sc.nextLine());
sc.close();
// Write
PrintWriter pw = new PrintWriter("file.txt");
pw.println("Text"); pw.close();
// Binary
FileOutputStream out = new FileOutputStream("file.bin");
out.write(65); out.close();
FileInputStream in = new FileInputStream("file.bin");
int byte = in.read(); in.close();
// Checks
File f = new File("file.txt");
f.exists() && f.canRead() && !f.isDirectory();
\end{lstlisting}
\subsection*{Java Primitive Types }
\begin{lstlisting}[language=Java, basicstyle=\footnotesize\ttfamily]
// Type Size Default Range / Example
boolean 1 bit false true / false
byte 8 bit 0 -128 to 127
char 16 bit '\u0000' Unicode ('A', '1')
int 32 bit 0 -2B to 2B
float 32 bit 0.0f Decimal (add f)
double 64 bit 0.0 Precise decimal
\end{lstlisting}
\subsection*{Threads}
\begin{lstlisting}
new Thread(() -> { System.out.println("Run");
try { Thread.sleep(1000); } catch (Exception e) {}}).start();
class MyRunnable implements Runnable {
public void run() { System.out.println("Runnable"); }
}
class MyThread extends Thread {
public void run() { System.out.println("Subclass"); }
}
} Thread.sleep(ms); // Pause t.join(); // Wait
\end{lstlisting}
\section*{Networking}
\subsection*{Server}
\begin{lstlisting}
// Server waits for a client connection
ServerSocket server = new ServerSocket(6000);
Socket client = server.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(client.getInputStream()));
\end{lstlisting}
\subsection*{Client}
\begin{lstlisting}
// Client connects and sends message
Socket s = new Socket("localhost", 6000);
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
out.println("Hi Server");
\end{lstlisting}
\end{multicols}
\end{document}