You are on page 1of 7

CS2030

UNIX Basic commands


Checkstyle
Directories
Files
Executing program in ubuntu terminal
VIM commands
Executing program in jshell terminal
Lists
Inheritance and Polymorphism
Abstract classes and Interfaces
String.format
Comparable vs Comparator

UNIX Basic commands


Checkstyle
java -jar ./checkstyle-8.2-all.jar -c ./cs2030_checks.xml

Directories
pwd - tells you where you currently are

cd <dir_path> - change directory

cd ~ / cd - go back to the home directory

mkdir <file_name> - make a new directory (folder)

cd .. - go up one directory

Files
ls - lists yours files in the current directory

mv <file_name> <dir> - moves a file

mv <file_name> <file_new_name> - rename a file

cp <1> <2> - copies <1> to <2>

rm <file> - removes a file

rm -rf <file> - recursively and forcefully remove

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

cat <file> - to look at the content of the file

Executing program in ubuntu terminal


1. Enter vim to edit code

2. go back to ubuntu terminal

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

4. type java <file> without extension e.g java Hello

5. The output of the program will be printed

VIM commands
press i to enter insert mode

press Esc to go back to normal mode

:write or :w during normal mode to save file

:quit or :q to exit vim and go back to Ubuntu terminal

:wq to save file and quit vim immediately

:q! forcefully exit vim without saving file

press v in normal mode to enter visual mode

In visual mode, highlight the text that you want, then press d to cut, y to copy, then press p

to paste in normal mode

press u to undo, Ctrl + R to redo in normal mode

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

<number> + arrow (up/down/left/right/w/b) to jump <number> words/lines

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

:<n> to navigate to line <n> immediately

/<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

gt to go to the next tab, gT to go to the previous tab

CS2030 2
:split <file> to split screen for buffer horizontally

:vsplit <file> or :vsp to split screen for buffer vertically

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

Executing program in jshell terminal


1. Enter jshell while opening the program e.g jshell Hello.java

2. excecute the methods that you want to execute.

Lists
must import with import java.util.List to use

Immutable List (not in


Types of list List ArrayList
API)

Mutability Immutable Mutable Immutable


ArrayList<Integer>
list = new
ArrayList<Integer>() ImList<Integer> list =
Creating one List<Integer> list = List.of(1,2,3) (only an empty Array new ImList<Integer>
(List.of(1,2,3))
List can be created
first)
will return new ImList
will update current list with new element
add() and set() gives an exception error of Exception
with new element inside while current
methods java.lang.UnsupportedOperationException
inside ImList would still be
the same

Inheritance and Polymorphism

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;
}

Since B is a subclass of A , B is an A B<:A

any methods in A can be invoked from B

However, an object A cannot invoke methods only in B

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

from example above, test.returnOne() would lead to an error

Abstract classes and Interfaces


Features Interface Abstract classes

-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

// Interface // Abstract class


abstract class FilledShape {
interface Scalable { private final Color color;
Scalable scale(int i);
} FilledShape(Color color) {
this.color = color;
interface Shape { }
double getArea();
} // this method is a concrete method
// that does not need to be implemented
class Circle implements Scalable, Shape { Color getColor() {
private final int radius; return this.color;

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:

add spaces before any output

specify decimal places

String.format syntax:

%f - float/double

%s - string

%d - integer

%c - char

Here are examples on how to use it:

// to specify d.p
String.format(“%.3f”, 1.23423); // Output: 1.234

String.format(“%s world”, “hello”); // Output: hello world

String.format(“%04d”, 12); // Output: 0012

String.format(“%4d”, 12); // Output: 12

String.format(“%4s”, “.”); // Output: . (4 spaces in the front)

String.format(“%s world %d”, “hello”, 34); // Output: hello world 3

String name = "Muhd Mursyid";


int age = 21;

String.format("My name is %s and I am %d years old", name, age);


//Ouput: My name is Muhd Mursyid and I am 21 years old

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.

Comparable is present in java.lang package. A Comparator is present in the java.util package.


We can sort the list elements of Comparable We can sort the list elements of Comparator type by
type by Collections.sort(List) method. Collections.sort(List, Comparator) method.

class Orange {
// non-private for the sake of example
double cuteness;
double sourness;

Orange(double sourness, double cuteness) {


this.sourness = sourness;
this.cuteness = cuteness;
}

@Override
public String toString() {
return String.format("Orange with %f sourness-cuteness ratio", this.sourness / this.cuteness);

CS2030 6
}
}

class Orange implements Comparable<Orange> {


// non-private for the sake of example
double cuteness;
double sourness;

Orange(double sourness, double cuteness) {


this.sourness = sourness;
this.cuteness = cuteness;
}

@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;

class OrangeComparator implements Comparator<Orange> {


public int compare(Orange o1, Orange o2) {
return (int) (o1.sourness / o1.cuteness - o2.sourness / o2.cuteness);
}
}

// Example run
orangesList.sort(new OrangeComparator());

CS2030 7

You might also like