Show Menu

COMP160 Cheat Sheet by

COMP160 final exam

Lecture 14 - References

* Reference types include String, Array and other classes.
* This is a keyword that always refers to the current instance object.
* No need for different parameter names.

Lecture 16 - Arrays 2

* Arrays are objects and can hold references to objects.
* The state of an array can be changed by a reference to an array (a change to the reference b for example will also change a)
* An array of classes can be made just as if you were creating an array of primative values:
AClass [] z = {new AClass(), new AClass­(3.3, "­hel­lo", 3), new AClass() };

Lecture 18 - events

* Objects that represent events in the GUI (button clicks, mosue clicks, text input etc.)
* Remember to import java.a­wt.*; javax.s­wi­ng.*; java.a­wt.e­ve­nt.*;
* Event model: (event generator object makes an) Programmer creates components and registers listener for each one. (even object reference sent to) automa­tically handlered whe user generates an event. (event listener object) programmer creates listener objects

Lecture 21 - Hierar­chies, Inheri­tance

* Extends: we can extend a class to create­/define a new class (subcl­ass­/child) A child class has it own member (dataf­ields & members) and also non private members of the parent class.
* Hierarchy: A class inherits from its parent, this is how objects get methods like toString that aren't its own
* Super: keyword refering to members of current class/­object inherited from the parent class

Lecture 24 - Collec­tions, ArrayList, Applets

* Enumerated Types: Enumerated types are a way of creating a new type by enumer­ating (listing) every possible value.
* Generics: where programs are written in terms of "to be specified later".
* Collec­tions: Java has many ways of repres­enting and processing collec­tions of objects, using classes and interfaces in java.util. These are called collection classes, collection framework, or collection API.
* ArrayList: A data structure which can store varying numbers of (refer­ences to) objects in a flexible list. import java.u­til.Ar­ray­List. Doesn't hold primitive types.
 ArrayL­ist­<St­rin­g> a1 = new ArrayList <St­rin­g>(); 



String a = a1.get(1); 



Lecture 15 - Arrays 1

* An array is a named collection of data of same type and a fixed size.
* Index can only be an int (or a char cast to an int)
* Hold primitive types, references / handles / pointers to objects
* Initia­liser lists:
 int []intAr = { 3, 7, 9, 23, 2 };

* An array always starts at position 0 and goes to length -1
* Multid­ime­nsional Arrays -
int [][]a = new int [3] [5]; 
initia­lizing =
double [][]b = { {1.0, 3.4}, {5.6, 8.9} };


for(double [ ] row : b ) {
  for(double x : row ) {
     System.out.print( x + " " );


public void actionPerformed (ActionEvent event) {
  if (event.getSource() == left)
    label.setText( "Left" );
    label.setText( "Right" ); 

Lecture 22 - Visibi­lity, overriding

* Packages: A package is a related group of classes. Ones that you don't put into a package get put into the default package.
* Visibility - package visibility is used when no visibility is specified - accessible only to classes in this package. protected visibility - accessible to classes in this package and any class extending this one
* Method overlo­ading: A class can have multiple methods of the same name as long as they have different parameter specif­ica­tions.
* Method Overri­ding: methods with same signature (name and parameter specif­ica­tion) exist in both parent and child class.
* When datafields have the same name it is called shadowing



Constr­uctor: creates an initially empty list
void add (Object obj)

Inserts the specified object to the end of this list
void add(int index, Object object)

Inserts the specified element at the specified position in this list
void clear()

removes all of the elements from this list
Object remove (int index)

Removes the element at the specified position in this list
Object get (int index)

Returns the element at the specified position in this list
int indexO­f(O­bject obj)

Returns the index of the first occurrence of the specified element in this list,
or -1 if this list does not contain the element
boolean contai­ns(­Object obj)

Returns true if this list contains the specified element
boolean isEmpty

Returns true if this list contains no elements
int size()

Returns the number of elements in this list

Printing out a 2D array

for(int row = 0; row < b.length; row++) {
  for(int col = 0; col < b[row].length; col++){
    System.out.print( b[row] [col ] + " ");

Lecture 17 - Graphics components

* Components: Objects that are drawn in the GUI (including containers such as frames and panels).
* A Container is a type of component that holds and organises other components JPanel holds other compon­ents, JFrame holds JPanels in a window
* Layout: (default) FlowLa­yout: Left to right top to bottom. Border­Layout: 5 positions, north south east west centre. BoxLayout: Single row or column. GridLa­yout: a grid. Example:


Lecture 20 - Input/­Output

* A stream is a source or destin­ation of a sequence of data.
* Tokens are text items separated by "­white space"
* try..c­atch: exception handling. when error occurs: do nothing - program will crash, handle the error with try catch.
* Use a scanner to read from a file.

Lecture 23 - Abstract classes

* instanceof (boolean): returns true if an object is an instance of a class i.e. s intanceof Square
* Abstract Classes: They are used when you want to create instances of subclasses but not the abstract parent itself. Abstract class can have its own normal methods.
* Abstract methods must be implem­ented in child classes, have no body in parent class.
* A child class can only have one parent. Multiple inheri­tance.
* Java has interfaces to avoid full multiple inheri­tance. Interfaces may only contain abstract methods and static final data fields.
* Final: a final class may not be extended, a final method may not be over ridden.


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.