-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotes.tex
More file actions
337 lines (304 loc) · 12.1 KB
/
Notes.tex
File metadata and controls
337 lines (304 loc) · 12.1 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
\documentclass[11pt]{article}
% -------------------- Packages --------------------
\usepackage[margin=1in]{geometry}
\usepackage{amsmath, amssymb}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage{enumitem}
\usepackage{fancyhdr}
\usepackage[listings]{tcolorbox}
\tcbuselibrary{breakable}
\usepackage{colortbl}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,calc}
% ---------- Modular row commands ----------
\newcommand{\memrow}[1]{\texttt{#1} \\ \hline}
\newcommand{\highlightmemrow}[1]{\cellcolor{gray!20}\texttt{#1} \\ \hline}
% ---------- Memory table environment ----------
\newenvironment{memorytable}[1][4cm]{
\begin{center}
\begin{tabular}{|p{#1}|}
\hline
}{
\end{tabular}
\end{center}
}
% ---------- Memory diagram (address -> value) ----------
\newcommand{\memcell}[2]{\texttt{#1} & \texttt{#2} \\ \hline}
\newcommand{\highlightmemcell}[2]{\cellcolor{gray!20}\texttt{#1} & \cellcolor{gray!20}\texttt{#2} \\ \hline}
% Named cells (for drawing arrows between rows/cells)
\newcommand{\memmark}[2]{\tikz[remember picture,baseline=(#1.base)]\node[inner sep=0pt,outer sep=0pt] (#1) {#2};}
\newcommand{\memcellm}[4]{\memmark{#1}{\texttt{#2}} & \memmark{#3}{\texttt{#4}} \\ \hline}
\newcommand{\highlightmemcellm}[4]{\cellcolor{gray!20}\memmark{#1}{\texttt{#2}} & \cellcolor{gray!20}\memmark{#3}{\texttt{#4}} \\ \hline}
% Draw an arrow from one named cell to another.
% Usage: \memarrow{fromNode}{toNode} or \memarrow[bend left=30]{fromNode}{toNode}
\newcommand{\memarrow}[3][bend left=20]{%
\tikz[remember picture,overlay] \draw[-{Stealth[length=2mm,width=2mm]}] (#2.east) to[#1] (#3.east);
}
\newenvironment{memorydiagram}[1][Memory Diagram]{
\begin{tcolorbox}[
breakable,
colback=gray!5,
colframe=gray!50,
title={#1}
]
\begin{center}
\begin{tabular}{|p{3.5cm}|p{9cm}|}
\hline
\textbf{Address} & \textbf{Value} \\ \hline
}{
\end{tabular}
\end{center}
\end{tcolorbox}
}
% Usage example:
% \begin{memorydiagram}[Stack]
% \memcell{0x1000}{\textbf{i} = 33}
% \highlightmemcell{0x1004}{\textbf{p} \textrightarrow 0x2000}
% \end{memorydiagram}
%
% Arrow example (nodes must be on the same page):
% \begin{memorydiagram}[Stack]
% \memcellm{a1}{0x1004}{pCell}{\textbf{p} \textrightarrow 0x2000}
% \end{memorydiagram}
% \begin{memorydiagram}[Heap]
% \memcellm{a2}{0x2000}{objCell}{Point(x=1,y=2)}
% \end{memorydiagram}
% \memarrow{pCell}{objCell}
% -------------------- Header / Footer --------------------
\pagestyle{fancy}
\fancyhf{}
\lhead{ITI1121 Introduction To Computing \hfill Nathan Ng}
\cfoot{\thepage}
% -------------------- Code Styling --------------------
\lstset{
basicstyle=\ttfamily\small,
keywordstyle=\color{blue},
commentstyle=\color{gray},
stringstyle=\color{teal},
numbers=left,
numberstyle=\tiny,
stepnumber=1,
numbersep=8pt,
frame=single,
breaklines=true,
tabsize=2
}
% -------------------- Custom Commands --------------------
\newcommand{\topic}[1]{\section*{#1}}
\newcommand{\subtopic}[1]{\subsection*{#1}}
\newcommand{\important}{\textbf{Important: }}
% ---------- Definitions (boxed + auto-numbered) ----------
\newcounter{definition}
\newenvironment{definition}[1][]
{%
\refstepcounter{definition}%
\begin{tcolorbox}[
breakable,
colback=gray!5,
colframe=gray!50,
title={Definition~\thedefinition\if\relax\detokenize{#1}\relax\else\ (#1)\fi}
]%
}
{%
\end{tcolorbox}%
}
% One-line convenience command (keeps your original intent)
\newcommand{\defn}[2][]{\begin{definition}[#1]#2\end{definition}}
% ---------- Big Questions / Main Ideas (boxed + auto-numbered) ----------
\newcounter{bigquestion}
\newenvironment{bigquestion}[1][]
{%
\refstepcounter{bigquestion}%
\begin{tcolorbox}[
breakable,
colback=gray!5,
colframe=gray!50,
title={Big Question~\thebigquestion\if\relax\detokenize{#1}\relax\else\ (#1)\fi}
]%
}
{%
\end{tcolorbox}%
}
% One-line convenience command
\newcommand{\bigq}[2][]{\begin{bigquestion}[#1]#2\end{bigquestion}}
% Question + answer convenience command
\newcommand{\bigqa}[3][]{%
\begin{bigquestion}[#1]
\textbf{Q:} #2\par\medskip
\textbf{A:} #3
\end{bigquestion}%
}
% Usage examples:
% \bigq{What is the main idea of today's lecture?}
% \bigqa[Variables]{How do variables map to memory?}{A variable is a named label for a memory location that stores a value of a specific type.}
% \begin{bigquestion}[Tickets]
% Why make a field \texttt{static}? What changes across instances?
% \end{bigquestion}
% -------------------- Document --------------------
\begin{document}
% -------------------- Title --------------------
\begin{center}
{\Large \textbf{ITI1121 -- Introduction To Computing}}\\
\end{center}
\topic{Variables}
\defn[Variables]{A variable is a place in memory to hold a value, which we refer to using a label.}
\begin{lstlisting}[language=Java]
byte i = 33;
\end{lstlisting}
\topic{Data Types in Java}
\begin{itemize}
\item Java is a strongly typed programming language. Each variable has a size and a known type at compile time.
\item The type of each variable must be declared.
\end{itemize}
\begin{lstlisting}[language=Java]
int i;
i = 33;
\end{lstlisting}
\bigqa[Data Types]{What are data types for?}{
\begin{itemize}
\item Data types tell the compiler how much memory to allocate.
\subitem Example: Instantiation of a double data type tells the compiler to allocate 8 bytes.
\item Data types also give information about which operations are allowed.
\subitem Example: When a char is declared, the compiler knows to treat it as a char. So it cannot be multiplied by a number, etc. (operations).
\end{itemize}
}
\begin{itemize}
\item Java has both primitive and reference data types.
\defn[Primitive Data Types]{A primitive data type specifies the type of a variable and the kind of values (stored at memory location) it can hold.}
\defn[Reference Data Types]{A reference data type references/points to the location of an object.}
\subsubitem - User-defined, and refers to an instance of a class.
\item The value of a reference variable is a memory location which points to the location of an object.
\item The declaration of a reference variable only allocates memory to store the address/location of an object.
\subitem - These objects contain functions which return values.
\item Declaring a reference variable allocates memory for the location; \texttt{null} is when there is no object being referred to.
\end{itemize}
\topic{Memory Diagrams}
{Below shows code and its memory diagrams.}
\begin{lstlisting}[language=Java]
public class Constant {
private String name;
private double value;
public Constant( String name, double value) {
this.name = name;
this.value = value;
}
}
\end{lstlisting}
\begin{lstlisting}[language=Java]
Constant c;
c = new Constant("Golden ratio", 1.61803399)
\end{lstlisting}
\begin{memorydiagram}[Reference variable of type Constant]
\memcellm{a1}{c}{pCell}{}
\end{memorydiagram}
\begin{memorydiagram}[Constant Class Instance]
\memcellm{a2}{value}{objCell}{1.61803399}
\memcellm{a3}{name}{objCell}{}
\end{memorydiagram}
\begin{memorydiagram}[String Class Instance]
\memcellm{a4}{}{String}{"Golden ratio"}
\end{memorydiagram}
\memarrow{pCell}{a2}
\memarrow{objCell}{a4}
Reference variable, c, is pointing to the Constant Class, which has an int variable and a String variable. But since String is also a reference type it points to the String class.
\topic{Classes}
\defn[Auto-Boxing]{Auto boxing is when an object is declared, but is made equal to its primitive version. Java 5 and up will auto-box it and compile, Java 1.4 and under will not.}
\begin{lstlisting}[language=Java]
Integer i = 1;
\end{lstlisting}
Java 5 treats it as:
\begin{lstlisting}[language=Java]
Integer i = Integer.valueOf(1);
\end{lstlisting}
Java 5's implementation was deprecated since Java 9 and is now just:
\begin{lstlisting}[language=Java]
Integer i = new Integer(1);
\end{lstlisting}
\important{Auto-Boxing will seriously affect runtime}
\topic{Comparison Operators}
Primitive variables can be compared using basic operators such as \textless , \textgreater , = etc.
Reference variables require dedicated functions to compare the values of objects. \\In cases where technically, a basic operator compares a reference variable to reference variable, it will treat them as different objects.
\begin{lstlisting}[language=Java]
MyInteger a = new MyInteger(5);
MyInteger b = new MyInteger(5);
if (a == b) {
System.out.println("a equals b");
} else {
System.out.println("a does not equal b"); \\fuckin latex format
}
\end{lstlisting}
This code will print as "a does not equal b" as Java is comparing two refrences which's value is different at memory location. \\
This implies that if two objects point to the same object (same memory location) the latter wouldnt have printed, but rather "a equals b".
\topic{Call-by-Value}
\defn[Formal Parameter]{A formal parameter is a variable which is part of the definiition of the method}
\defn[Actual Parameter]{The actual parameter is the real input values of the method when the method is called}
\begin{lstlisting}
int sum(int a, int b) { //formal parameters a, b
return a + b;
}
int x,y,z;
total = sum(x,y); //actual parameters x,y
\end{lstlisting}
In Java, when a method is called the values of the actual parameters are copied to the location of the formal parameters. Below is what happens when a function is called.
\begin{enumerate}[label=\arabic*.]
\item the execution of the calling method is stopped
\item an activation frame (activation block or record) is created (it contains the formal paramerters as well as the local variables)
\item the value of the actual parameters are copied to the locaiton of the formal parameters
\item the body of the method is executed
\item a return value or void is saved
\item activation frame is destroyed
\item the execution of the claling method restarts with the next instruction
\end{enumerate}
Example:
\begin{lstlisting}[language=Java]
public static void increment(int a) {
a = a + 1;
}
public static void main( String[] args) {
int a = 5;
increment(a);
}
\end{lstlisting}
the increment function doesnt actually return any real values so step 5 is skipped and the activation frame for increment(a) is destroyed with no real outcome on the main function.
\newpage
\topic{Array-Based Implementation of Queues}
\bigq[What does this look like?]{CircularQueue implements Queue interface}
\begin{lstlisting}[language=Java]
public class CircularQueue<E> implements Queue<E> {
/* public interface Queue {
private E[]
public boolean isEmpty();
public void enqueue(E 0);
public E dequeue();
}
*/
private E[] = q;
public boolean isEmpty();
public void enqueue(E 0);
public E dequeue();
}
\end{lstlisting}
{Implementation 1}
\begin{itemize}
\item The front of the queue is a fixed lication, say 0. and we will use an instance favriable to indicate the location of the rear element.
\subitem Rear can be made to designate the first free location (instead of the element itself)
\subitem a queue could be implmented in the upper part of the array
\item Insertion (enqueue)
\item Removal (dequeue)
\subitem - All the elements must be move one position toward the front of the queue so that the fron telement remains at a fixed location, here 0.
\end{itemize}
{Implementation 2}
\begin{itemize}
\item The front and the rear of the queue are now moving, requiring two instance variables: one pointing at the front element, other that points to the rear.
\subitem - Say we want to add 3 elements to a 9 block array, if there is no space left in the back, even though there is technically space in the queue, the code will crash.
\subitem - If array isnt full, we can shift the elements (refer as group)
\end{itemize}
{Implementation 3}
\begin{itemize}
\item To make both operations fast, this implementation uses a circular array.
\item Uses modulo to change front variable in the case which front is pointing to the end indexs, but removal changes the front to the start indexes.
\end{itemize}
\end{document}