Created
September 5, 2019 03:58
-
-
Save ryanorsinger/d670c3b7a625933da6d0552048b89f53 to your computer and use it in GitHub Desktop.
"A Little Java, A Few Patterns" Starter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Seasoning {} | |
class Salt extends Seasoning { | |
public String toString() { | |
return "new " + getClass().getName() + "()"; | |
} | |
} | |
class Pepper extends Seasoning { | |
public String toString() { | |
return "new " + getClass().getName() + "()"; | |
} | |
} | |
class Thyme extends Seasoning { | |
public String toString() { | |
return "new " + getClass().getName() + "()"; | |
} | |
} | |
abstract class Num {} | |
class Zero extends Num { | |
public String toString() { | |
return "new " + getClass().getName() + "()"; | |
} | |
} | |
class OneMoreThan extends Num { | |
Num predecessor; | |
OneMoreThan(Num _p) { | |
predecessor = _p; | |
} | |
public String toString() { | |
return "new " + getClass().getName() + "(" + predecessor + ")"; | |
} | |
} | |
/** | |
* To run this Main class, compile from the terminal with javac Main.java | |
* Once combiled, run on the JVM with java Main | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
Seasoning seasoning1 = new Salt(); | |
Seasoning seasoning2 = new Pepper(); | |
System.out.println(seasoning2); | |
System.out.println(seasoning1); | |
Num a = new Zero(); | |
System.out.println(a); | |
Num b = new OneMoreThan(a); | |
System.out.println(b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment