Skip to content

Instantly share code, notes, and snippets.

@kineticz
kineticz / correct version.java
Last active August 29, 2015 14:14
Sort an array of integers interactively. It's not working, why?
package com.company.ex;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
* Created by IDEA on 26/01/15.
*/
@kineticz
kineticz / Rect.java
Created January 26, 2015 21:26
min and max are sort of redundant.
public Rect(int x1, int y1, int x2, int y2) {
this.x1 = min(x1, x2);
this.y1 = min(y1, y2);
this.x2 = max(x1, x2);
this.y2 = max(y1, y2);
}
@kineticz
kineticz / IntList.java
Created January 27, 2015 10:40
Why do I have cast o into a IntList when it is already a IntList?
@Override
public boolean equals(Object o) {
if(o == this) return true;
if(!(o instanceof IntList)) return false;
IntList that = (IntList) o;
if(this.size != that.size) return false;
for(int i = 0; i < this.size; i++) {
if(this.data[i] != that.data[i]) return false;
}
return true;
@kineticz
kineticz / Tokenizer.java
Created January 27, 2015 11:17
What is the advantage of nesting a interface within another?
/**
* Created by IDEA on 27/01/15.
*/
public interface Tokenizer {
public static final int EOF = -1;
public static final int SPACE = -2;
public static final int NUMBER = -3;
public static final int WORD = -4;
public static final int KEYWORD = -5;
public static final int TEXT = -6;
@kineticz
kineticz / AbstractTokenizer.java
Created January 27, 2015 14:29
Is it ok for a field and a method to have the same name?
package com.company;
import java.io.IOException;
import java.util.Map;
/**
* Created by IDEA on 27/01/15.
*/
public abstract class AbstractTokenizer implements Tokenizer {
boolean skipSpaces;
public class Main {
public static void main(String[] args) {
char[] a = "0123456789".toCharArray();
// create a string from a char array
String x = new String(a, 2, 3);
System.out.println(x);
}
}
@kineticz
kineticz / AbstractTokenizer.java
Created January 27, 2015 16:00
How do you read a class with lots of states and long functions? This beast looks terrifying.
package com.company;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* Created by IDEA on 27/01/15.
*/
public abstract class AbstractTokenizer implements Tokenizer {
@kineticz
kineticz / IntList.java
Created January 27, 2015 18:34
Any suggestions to simplify / improve this implementation of quicksort?
public static int[] concatArrays(int[] a1, int[] a2) {
int n1 = a1.length;
int n2 = a2.length;
if(n1 == 0) return a2;
if(n2 == 0) return a1;
int n = n1 + n2;
int counter = 0;
int[] result = new int[n];
for(; counter < n1; counter++) {
@kineticz
kineticz / FileCopy.java
Created January 27, 2015 22:35
What is ====?
public static void copy(String from_name, String to_name) throws IOException {
File to_file = new File(to_name)
File from_file = new File(from_name);
if(!from_file.exists()) throw new IllegalArgumentException("not such file: " + from_name);
if(!from_file.isFile()) throw new IllegalArgumentException("not a regular file: " + from_name);
if(!from_file.canRead()) throw new IllegalArgumentException("not readable: " + from_name);
if(to_file.isDirectory()) to_file = new File(to_file, from_file.getName());
if(to_file.exists()) {
if(!to_file.canWrite()) {
System.out.print("Overwrite existing file? (y/N)");
@kineticz
kineticz / Tail.java
Last active August 29, 2015 14:14
Why do I get a null point exception from this?
package je3.io;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* Created by IDEA on 29/01/15.
*/
public class Tail {
public static String[] tailLines(String filename) throws IOException {