starterkit/Comparator.java
starterkit/DSample.java
starterkit/ItemNotFoundException.java
starterkit/LinkedList.java
starterkit/ListNode.java
starterkit/ReadFile.class
starterkit/ReadFile.java
starterkit/Sample.java
starterkit/StringComparator.java
starterkit/Comparator.java
starterkit/Comparator.java/*
* Interface : C o m p a r a t o r
*
* @Name : student name
* @StdID: 20XXXXXXX
* @Class: IT114105/1X
* @2021-02-19
*/
public interface Comparator {
public abstract boolean isEqualTo (Object item1, Object item2);
//public abstract boolean isLessThan (Object item1, Object item2);
public abstract boolean isLessThanOrEqualTo (Object item1, Object item2);
public abstract boolean isGreaterThan (Object item1, Object item2);
public abstract boolean isGreaterThanOrEqualTo (Object item1, Object item2);
}
starterkit/DSample.java
starterkit/DSample.javapublic class DSample {
public static void main(String[] args) {
int $a = 0, _a = 1, Sample;
System.out.println($a);
_a = _a*_a – $a;
System.out.println(_a);
}
}
starterkit/ItemNotFoundException.java
starterkit/ItemNotFoundException.java/*
* Class : I t e m N o t F o u n d E x c e p t i o n
*
* @Name : student name
* @StdID: 20XXXXXXX
* @Class: IT114105/1X
* @2021-02-19
*/
public class ItemNotFoundException extends RuntimeException {
public ItemNotFoundException() {
super(“Item is not found!”);
}
}
starterkit/LinkedList.java
starterkit/LinkedList.java/*
* Class : L i n k e d L i s t
*
* @Name : Leung Siu Tim
* @StdID: 200037150
* @Class: IT114105/1B
* @2021-02-19
*/
public class LinkedList {
private ListNode head;
private ListNode tail;
private Comparator comparator;
public LinkedList(Comparator comparator) {
head = tail = null;
this.comparator = comparator;
}
public boolean isEmpty() {
return (head==null);
}
public void addToHead(Object item) {
if (isEmpty()) {
head = tail = new ListNode(item);
} else {
head = new ListNode(item, head);
}
}
public void addToTail(Object item) {
if (isEmpty()) {
head = tail = new ListNode(item);
} else {
tail.next = new ListNode(item);
tail = tail.next;
}
}
public Object removeFromHead() throws EmptyListException {
Object item = null;
if (isEmpty()) {
throw new EmptyListException();
}
item = head.data;
if (head == tail) // there’s only one single node
head = tail = null;
else
head = head.next;
return item;
}
public Object removeFromTail() throws EmptyListException {
if (isEmpty()) {
throw new EmptyListException();
}
Object item = tail.data;
if (head == tail) { // there is only one node
head = tail = null;
return item;
}
ListNode current = head;
while (current.next != tail)
current = current.next;
tail = current;
tail.next = null;
return item;
}
public String toString() {
String s = “[ “;
ListNode current = head;
while (current != null) {
s += current.data + ” “;
current = current.next;
}
return s + “]”;
}
public void insertInOrder (Object item) {
if (isEmpty()) {
head = tail = new ListNode (item);
} else {
if (comparator.isGreaterThanOrEqualTo(head.data, item)) {
addToHead(item);
} else if (comparator.isLessThanOrEqualTo(tail.data, item)) {
addToTail(item);
} else {
// insert in the middle
ListNode current = head;
while (current.next != null) {
if (comparator.isGreaterThanOrEqualTo(current.next.data, item)) {
ListNode newNode = new ListNode(item);
newNode.next = current.next;
current.next = newNode;
return;
}
current = current.next;
}
}
}
}
public void removeItem (Object item) throws ItemNotFoundException {
if (isEmpty()) {
throw new ItemNotFoundException();
}
if (comparator.isEqualTo(head.data, item))
removeFromHead();
else if (comparator.isEqualTo(tail.data, item))
removeFromTail();
else {
// remove a node in the middle
ListNode current = head;
while (current.next != null) {
if (comparator.isEqualTo(current.next.data, item)) {
current.next = current.next.next;
return;
}
current = current.next;
}
throw new ItemNotFoundException();
}
}
}
starterkit/ListNode.java
starterkit/ListNode.java/*
* Class : L i s t N o d e
*
* @Name : student name
* @StdID: 20XXXXXXX
* @Class: IT114105/1X
* @2021-02-19
*/
public class ListNode {
public Object data;
public ListNode next;
public ListNode(Object data) {
this.data = data;
this.next = null;
}
public ListNode(Object data, ListNode next) {
this.data = data;
this.next = next;
}
}
ReadFile
public synchronized class ReadFile {
private static final String DELIMITER = “(?:\\”|[^”])*?”|[\s.,;:+*/|!=><@?#%&(){}\-\^\[\]\&&]+;
public void ReadFile();
public static void main(String[]);
}
starterkit/ReadFile.java
starterkit/ReadFile.javaimport java.io.*;
import java.util.*;
public class ReadFile {
private static final String DELIMITER = "\"(?:\\\\\"|[^\"])*?\"|[\\s.,;:+*/|!=><@?#%&(){}\\-\\^\\[\\]\\&&]+";
public static void main(String [] args){
LinkedList s = new LinkedList();
BufferedReader source;
String line;
int lineCount = 0;
int n = 0 ;
String[] cars;
try
{
System.out.println("SOURCE FILE:"+args[0]);
source = new BufferedReader(new FileReader(args[0]));
line = source.readLine();
while ( line != null)
{
lineCount++;
n++ ;
System.out.println(String.format("%04d | %s", lineCount, line));
String[] tokens = line.split(DELIMITER);
for (String s1 : tokens) {
System.out.println(s1);
}
line = source.readLine();
}
source.close();
}
catch ( IOException iox ) {
System.out.println("Problem encountered in reading file!" );
}
}
}
starterkit/Sample.java
starterkit/Sample.javaimport java.util.*;
public class Sample {
public static void main(String [ ] args) {
Scanner keyboard = new Scanner(System.in);
int num, den;
double result;
System.out.print("Numerator? ");
num = keyboard.nextInt();
System.out.print("Denominator? ");
den = keyboard.nextInt();
try {
result = quotient(num, den);
System.out.println("Answer: " + result);
}
catch (DivideByZeroException e) {
System.out.println("DivideByZeroException caught!");
}
catch (ArithmeticException e) {
System.out.println("ArithmeticException caught!");
}
catch (Exception e) {
System.out.println("Exception caught!");
}
}
public static double quotient(int num, int den) {
if (den==0)
throw new DivideByZeroException();
return (double) num / den;
}
}
public class DivideByZeroException extends ArithmeticException {
}
starterkit/StringComparator.java
starterkit/StringComparator.java/*
* Interface : S t r i n g C o m p a r a t o r
*
* @Name : student name
* @StdID: 20XXXXXXX
* @Class: IT114105/1X
* @2021-02-19
*/
public class StringComparator implements Comparator {
public boolean isEqualTo (Object item1, Object item2) {
if (((String) item1).compareTo((String) item2) == 0)
return true;
else
return false;
}
public boolean isLessThanOrEqualTo (Object item1, Object item2) {
if (((String) item1).compareTo((String) item2) <= 0)
return true;
else
return false;
}
public boolean isGreaterThan (Object item1, Object item2) {
if (((String) item1).compareTo((String) item2) > 0)
return true;
else
return false;
}
public boolean isGreaterThanOrEqualTo (Object item1, Object item2) {
if (((String) item1).compareTo((String) item2) >= 0)
return true;
else
return false;
}
}