Skip to content

Instantly share code, notes, and snippets.

Avatar

Robert C. Martin unclebob

View GitHub Profile
View gist:526188
(ns life (:use clojure.test clojure.set))
(def under-populated 1)
(def over-populated 4)
(def fertile 3)
(defn north [[x y]] [x (inc y)])
(defn south [[x y]] [x (dec y)])
(defn east [[x y]] [(inc x) y])
(defn west [[x y]] [(dec x) y])
View PayCalculator.java
package payroll;
public class PayCalculator {
private boolean isHourlyWorker;
public PayCalculator(boolean hourlyWorker) {
isHourlyWorker = hourlyWorker;
}
public double calculate(double hours, double rate) {
View PayCalculator.java
package payroll;
public class PayCalculator {
public static float calculate(double hours,
double rate,
boolean isHourlyWorker) {
if (hours < 0 || hours > 80) {
throw new RuntimeException("Hours out of range: " + hours);
}
float wages = 0;
View PayCalculatorTest.java
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static payroll.PayCalculator.calculate;
public class PayCalculatorTest {
private void assertPay(double expectedPay, double actualPay) {
assertEquals(expectedPay, actualPay, .001);
}
View PayCalculator.java
package payroll;
public class PayCalculator {
public static double calculate(double hours,
double rate,
boolean isHourlyWorker) {
if (hours < 0 || hours > 80) {
throw new RuntimeException("Hours out of range: " + hours);
}
View ContractorCalculator.java
package payroll;
public class ContractorCalculator extends PayCalculator {
public ContractorCalculator() {
super(false);
}
}
View ContractorCalculator.java
package payroll;
public class ContractorCalculator extends PayCalculator {
public ContractorCalculator() {
super(false);
}
public double calculate(double hours, double rate) {
if (hours < 0 || hours > 80) {
throw new RuntimeException("Hours out of range: " + hours);
View ContractorCalculator.java
package payroll;
public class ContractorCalculator extends PayCalculator {
public double calculate(double hours, double rate) {
validateHours(hours);
return hours * rate;
}
}
View Wrapper.java
public class Wrapper {
public static String wrap(String s, int col) {
if (s.length() <= col)
return s;
else if (s.charAt(col-1) == ' ')
return (s.substring(0, col-1) + "\n" + wrap(s.substring(col), col));
else
return (s.substring(0, col) + "\n" + wrap(s.substring(col), col));
}
}
View Wrapper.java
public class Wrapper {
public static String wrap(String s, int col) {
return new Wrapper(col).wrap(s);
}
private int col;
private Wrapper(int col) {
this.col = col;
}
You can’t perform that action at this time.