COMP161 Lecture Notes
COMP161 Lecture Notes
COMP161
Lecture summaries for final exam study.
COMP161 1
💾 Unicode & ASCII ordering:
' ' < digits < symbols / punctuation < A < Z < a < z
Lecture 4: Expressions
The remainder operator (%) used on integers is sometimes called ‘mod’ or
‘modulo’.
Lecture 5: Objects I
Instance objects are created with the keyword new. This is the followed by a
constructor method of that object.
Constructors have the same name as the class they belong to, and may be
overloaded. They do not have a return type. They let us create objects that are
instances of the same class, but in different states.
Accessors return the value in a data field.
Lecture 6: Graphics I
Windows are a visible representation of an instance object constructed from the
library class JFrame. GUI elements are usually created within panels, instances
of the library class JPanel.
To add drawing content to a JPanel, it must have a paintComponent method.
JPanel is a library class, so we must extend it to give it a paintComponent
method.
COMP161 2
💾 Extending a (parent) class makes a new (child) class. The child has
all of the (non private) members (data fields & methods) of the
parent, and can also include its own members.
if (condition == true) {
//block of statements;
} else {
//block of statements;
}
Lecture 8: Selection II
Switch lets us choose from a finite number of possible cases depending on the
value of a selector variable. Case labels must be the same type as the selector.
int n;
switch (n) {
case 1:
System.out.println("n is 1");
COMP161 3
break;
case 2:
case 3:
System.out.println("n is 2 or 3");
break;
case 4:
System.out.println("n is 4");
break;
default:
System.out.println("n is another value");
}
If a case is missing a break; statement at the end, execution will carry on into the
next case.
Data fields and methods are members of a class. If a member is declared with
static it is a class member, part of the class object. If it is declared without static it
is an instance member, part of the instance object.
Class members are created and usable before the program starts to run;
members of the application class are usually class members, support classes are
usually not.
You can use You can’t You can use Using a class member in an
class members use instance instance object works, but
in class instance members in you are really using the
objects. members instance member in the underlying
in class objects. class object.
objects.
COMP161 4
(don’t
exist!)
An object is distinct and seperate from a reference to the object. References are
integer values referring to an address in memory. If an instance object is
referenced by multiple variables they are called aliases. If it is referenced by no
variables it cannot be accessed and will get eaten by the garbage collector.
Lecture 10: Structured programming
Service methods are called on an object. This means that they must be public
and called from elsewhere in the program. Support methods are intended to be
called within the class and thus should be private.
Strings in java are immutable. This means they can’t be changed, however
you can build a new String and assign it to a String variable and thus
overwrite the original value. The variable is now storing the address of the
new String.
//Writing to file:
import java.io.*; import java.nio.file.*;
public static void writeToFile (String newContent, String fileName) {
try {
FileWriter fw = new FileWriter(fileName);
fw.write(newContent);
fw.close();
catch (Exception e) { }
}
COMP161 5
Development process: Typical tools programmers use include
Unified Modelling language (UML),
1. Establish requirements
flowchats, and pseudocode.
2. Design program
3. Implement design
4. Test program
Pre 1950’s
Programs were “machine codes” based very closely on the binary hardware
of the computers. Programmers used machine codes, console buttons and
switches, physical re-wiring (ENIAC)!
1950’s
1960’s
Growth of machine independent “high level” languages such as BASIC, Lisp,
Algol 60, Cobol. Most “serious programming” still done in assembly language
(slow to write, fast to run).
1970’s
“Structured programming” methods increase program correctness. There is a
further development of high level structured languages such as Pascal, C,
and Algol 68.
1980’s
Large projects with more focus on reducing the complexity of the task.
Program management systems introduced such as Make, APSE, and Project
Builder.
1990’s
Impact of the internet and the Web popularise HTML, XML, network portable
applications and “applets”, Java. Database integration streamlined.
COMP161 6
2000+
Languages created for distributed and parallel / concurrent computing.
Significant impact of Artificial Intelligence / Neural Networks. Quantum
computing?
There are two ways of classifying languages, by generation and by style. Java is
an object-orientated 3rd generation language.
while (condition) { do {
//block of statements; //block of statements;
} } while (condition);
do..while loops are used when we want to execute the body at least once, and
then continue executing it while the condition remains true.
//For-each loops process Arrays or Collections that implement the iterable interface
Datatype[] listOfData = {obj1, obj2, obj3};
for (Datatype data: listOfData) {
//block of statements;
{
COMP161 7
for (initialisation; condition; update) {
//block of statements;
{
int [] intArr = new int[5] //Creates an array of int with a fixed size of 5
int [] initialisedIntArr = {3, 7, 9, 23, 2}; //Creates and initialises an int Array
For-each loops are very useful for iterating through an array, however they are
“read only”. It can’t be used to write values back into the array.
COMP161 8
Java can also hold ragged arrays, arrays that are not perfectly rectangular.
Because of this, processing of elements should be based upon the length of that
row.
try {
//code that might cause an
} catch (ExceptionClass variable) {
//try to handle exception or warn user
}
COMP161 9
Always refer to class (static) data fields with ClassName.name, not
variableName.name (if you’ve created an instance object).
Packages are a related group of classes. You can use any class without
importing it by using its fully qualified name (e.g. java.util.Random). You can use
a class’s simply name (e.g. Random) if we import the class, the class is in
java.lang, or the classes are in the same package as each other. To put a class in
a named package (override the default), the name is stated at the top of the file:
package java.lang;
Collection classes include ArrayList, Vector, Hashtable, List, HashMap, Map, Set,
and more. Collections have useful methods such as copy, fill, min, max, reverse,
shuffle, sort, contains, etc. Collection classes often implement library interfaces
such as Iterator and Iterable.
ArrayLists can collect groups of objects of related types, if objects have the same
parent type somewhere in the hierarchy.
For example, JButton extends AbstractButton extends JComponent extends
Container extends Component extends Object. An ArrayList declared as
<JComponent> would be able to store both JButton and JPanel.
If a class is abstract you can’t make instances of it, but it can be extended and
you can make instances of its children. An abstract class can have abstract
methods that child classes must implement. This is a way of ensuring that all
children must have their specialised versions of the same functionality.
COMP161 10
//Removing elements from an ArrayList
ArrayList<String> myArrayList = new ArrayList<String>();
ArrayList<String> toBeRemoved = new ArrayList<String>();
for(String word: myArrayList){
if(word.length() > 10){ //arbitrary condition
toBeRemoved.add(word);//if condition true for that element, add to new ArrayList
}
}
for(String word: toBeRemoved){
myArrayList.remove(word);//then iterate through toBeRemoved list
}
Testing concepts:
• code reviews: a group looks at the code together
• walkthroughs: step through code line by line
• defect testing (test case, test suite): actively look for possible errors
• black-box testing: check that inputs = correct outputs (not looking at code)
• white/glass-box testing: check code in detail (follow all paths)
• unit testing: test individual “units” of code (e.g. methods) in isolation
• integration testing: test how units interact with each other
• regression testing: if you fix one bug, run your other tests again
• system testing: test the entire finished program / product (alpha, beta)
• test-driven development: testing is central to coding, not an afterthought!
COMP161 11