CS2030 Cheatsheet 1.0
CS2030 Cheatsheet 1.0
Directories
pwd - tells you where you currently are
cd .. - go up one directory
Files
ls - lists yours files in the current directory
vim <file_name> - open a file on Vim. If no such file exist, it will create a temporary new file for
us to save later
CS2030 1
vimtutor - open vim guide
3. type javac <file> to compile e.g javac Hello.java —> creates a new file called Hello.class
which is called the byte code and will be intepreted by the machine to produce an output
VIM commands
press i to enter insert mode
In visual mode, highlight the text that you want, then press d to cut, y to copy, then press p
To highlight an entire line, enter visual line mode by pressing shift + v in normal mode
To highlight an entire block of text, enter visual block mode by pressing Ctrl + v
press w to jump to the start of the next word, b to jump to the start of the previous word
press gg to go to the first line of the file, G to go to the last line of the file
gg=G to fix the indentation of the first line to the last line of the file
/<regex> to search for text that match <regex>, then press N to jump to previous occurence
of <regex>, press n to jump to the next occurence of <regex>
At Ubuntu terminal, :vim -p <file1> <file2> to open multiple vim files on multiple vim tabs
CS2030 2
:split <file> to split screen for buffer horizontally
Ctrl + w & let go, followed by arrow key (up/down/left/right) to traverse between
windows/screens
:wa to save all files, :wqa to save all files and quit vim
Lists
must import with import java.util.List to use
class A {
private final int i;
A(int i) {
this.i = i;
}
int returnNum() {
return 1;
}
CS2030 3
}
class B extends A {
B(int i) {
// super() is used to access parents constructor
super(i);
}
int returnOne() {
// super. is used to access parents properties or methods
// super. cannot be used to access parents properties if property is set as private
return super.returnNum();
}
@Override
int returnNum() {
return 2;
}
object A can be declared as B eg: A test = new B(1); however, this means that it wont be
able to call methods in B as it is recognised as an A object
-only have abstract methods which specifies methods - can have both defined
methods that need to be implemented in subclasses (like a methods and abstract
contract) methods
- can have properties to be
properties - NO properties at all
inherited
inheritance - multiple interfaces for one class is allowed - only one inheritance
CS2030 4
}
Circle(int radius) {
this.radius = radius; // needs to be implemented
} abstract double getArea();
}
// implement both scale() and getArea()
@Override class Circle extends FilledShape {
public double getArea() { // implementing getArea from private
Shape final int radius;
return Math.PI * this.radius * this.radius;
} Circle(int radius, Color color) {
super(color);
@Override this.radius = radius;
public Circle scale(int factor) { // implementing scale} from Scalable
return new Circle(this.radius * factor);
} @Override
double getArea() {
} return Math.PI * radius * radius;
}
}
String.format
Use string.format to:
String.format syntax:
%f - float/double
%s - string
%d - integer
%c - char
// to specify d.p
String.format(“%.3f”, 1.23423); // Output: 1.234
CS2030 5
Comparable vs Comparator
Comparable Interface
The comparable interface compares elements based on a single elements. This means that
we need to override the compareTo method within the class that is implementing the comparable
interface. We usually use comparable when we only want to sort something based on one
attribute. For example, I only want to sort oranges based on sourness so I use comparable.
Comparator Interface
The comparator interface can compares elements based on multiple elements. We do not
need to implement the Comparable interface in the class that is using the comparator. Instead, its
best to create a separate class that implements the comparable interface and overrides the
method compare. We usually use the comparator interface when we want to sort something
based on many attributes or more than one condition. For example, I need to output two sorted
lists of oranges, one sorted according to sourness the other according to cuteness. In this case, I
need 2 comparators as a single compareTo method is not enough (and I can't have more than 1
compareTo methods)
Comparable VS Comparator
Comparable Comparator
Comparable provides a single sorting sequence. The Comparator provides multiple sorting sequences.
Comparable affects the original class, i.e., the Comparator doesn't affect the original class, i.e., the
actual class is modified. actual class is not modified.
Comparable provides compareTo() method to Comparator provides compare() method to sort
sort elements. elements.
class Orange {
// non-private for the sake of example
double cuteness;
double sourness;
@Override
public String toString() {
return String.format("Orange with %f sourness-cuteness ratio", this.sourness / this.cuteness);
CS2030 6
}
}
@Override
public int compareTo(Orange other) { // must return either 1 or -1 and 0 iff they are equal
if (this.sourness / this.cuteness > other.sourness / other.cuteness) {
return 1;
} else if (this.sourness / this.cuteness < other.sourness / other.cuteness) {
return -1;
} else {
return 0;
}
}
@Override
public String toString() {
return String.format("Orange with %f sourness-cuteness ratio", this.sourness / this.cuteness);
}
}
// Example run
orangesList.sort(null);
import java.util.Comparator;
// Example run
orangesList.sort(new OrangeComparator());
CS2030 7