Java + Guava 16 (Guava isn't super necessary, but it made some things a little less annoying to write).
Alright, you're supposed to be working? How about a program that actually writes real Java code, that actually compiles (though it doesn't do much).
It's difficult to demonstrate the animation, but this program writes a Java program using either the default dictionary (250 common English words) or a newline delimited file (taken as a command line argument), and types it to the console one character at a time at human seeming speeds. Make sure to run it yourself because this post doesn't do it justice. When it finishes, it waits 1 minute, then prints a lot of blank lines to the console, and starts over again. I tried to write it to make various parameters reasonably tweakable.
Also, normally I would put this into more than one file, but to make it easier to run I smushed all the classes together.
package org.stackoverflow.ppcg;
import java.io.*;
import java.util.*;
import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;
import com.google.common.collect.Lists;
public class CodeGenerator {
public static final Converter<String, String> TOUPPER =
CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL);
public static final Converter<String, String> TOLOWER =
CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL);
public static final String[] TYPES = new String[]{
"int", "long", "double", "String"
};
public static final List<String> DEFAULT_LIST = Arrays.asList(new String[]{
"the", "and", "for", "you", "say", "but", "his", "not", "she", "can",
"who", "get", "her", "all", "one", "out", "see", "him", "now", "how",
"its", "our", "two", "way", "new", "day", "use", "man", "one", "her",
"any", "may", "try", "ask", "too", "own", "out", "put", "old", "why",
"let", "big", "few", "run", "off", "all", "lot", "eye", "job", "far",
"have", "that", "with", "this", "they", "from", "that", "what", "make", "know",
"will", "time", "year", "when", "them", "some", "take", "into", "just", "your",
"come", "than", "like", "then", "more", "want", "look", "also", "more", "find",
"here", "give", "many", "well", "only", "tell", "very", "even", "back", "good",
"life", "work", "down", "call", "over", "last", "need", "feel", "when", "high",
"their", "would", "about", "there", "think", "which", "could", "other", "these", "first",
"thing", "those", "woman", "child", "there", "after", "world", "still", "three", "state",
"never", "leave", "while", "great", "group", "begin", "where", "every", "start", "might",
"about", "place", "again", "where", "right", "small", "night", "point", "today", "bring",
"large", "under", "water", "write", "money", "story", "young", "month", "right", "study",
"people", "should", "school", "become", "really", "family", "system", "during", "number", "always",
"happen", "before", "mother", "though", "little", "around", "friend", "father", "member", "almost",
"change", "minute", "social", "follow", "around", "parent", "create", "others", "office", "health",
"person", "within", "result", "change", "reason", "before", "moment", "enough", "across", "second",
"toward", "policy", "appear", "market", "expect", "nation", "course", "behind", "remain", "effect",
"because", "through", "between", "another", "student", "country", "problem", "against", "company", "program",
"believe", "without", "million", "provide", "service", "however", "include", "several", "nothing", "whether",
"already", "history", "morning", "himself", "teacher", "process", "college", "someone", "suggest", "control",
"perhaps", "require", "finally", "explain", "develop", "federal", "receive", "society", "because", "special",
"support", "project", "produce", "picture", "product", "patient", "certain", "support", "century", "culture"
});
private static final int CLASS_NAME_LENGTH = 2;
private final WordList wordList;
private final Appendable out;
private final Random r = new Random();
private CodeGenerator(WordList wordList, Appendable out) {
this.wordList = wordList;
this.out = out;
}
public static void main(String... args) throws Exception {
List<?> wordSource = getWords(args);
WordList list = new WordList(wordSource);
SleepingAppendable out = new SleepingAppendable(System.out);
CodeGenerator generator = new CodeGenerator(list, out);
while(!Thread.interrupted()) {
generator.generate();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
break;
}
out.setSleeping(false);
for(int i = 0; i < 100; i++) {
out.append(System.lineSeparator());
}
out.setSleeping(true);
}
}
private static List<?> getWords(String[] args) {
if(args.length > 0) {
try {
return getListFromFile(args[0]);
} catch(IOException e) { }
}
return DEFAULT_LIST;
}
private static List<Object> getListFromFile(String string) throws IOException {
List<Object> newList = Lists.newArrayList();
File f = new File(string);
Scanner s = new Scanner(f);
while(s.hasNext()) {
newList.add(s.nextLine());
}
return newList;
}
private void generate() throws IOException {
String className = beginClass();
List<Field> finalFields = generateFields(true);
printFields(finalFields);
out.append(System.lineSeparator());
List<Field> mutableFields = generateFields(false);
printFields(mutableFields);
out.append(System.lineSeparator());
printConstructor(className, finalFields);
printGetters(finalFields);
printGetters(mutableFields);
printSetters(mutableFields);
endClass();
}
private void printGetters(List<Field> fields) throws IOException {
for(Field f : fields) {
out.append(System.lineSeparator());
f.printGetter(out);
}
}
private void printSetters(List<Field> fields) throws IOException {
for(Field f : fields) {
out.append(System.lineSeparator());
f.printSetter(out);
}
}
private void printConstructor(String className, List<Field> finalFields) throws IOException {
out.append("\tpublic ").append(className).append('(');
printArgs(finalFields);
out.append(") {").append(System.lineSeparator());
for(Field f : finalFields) {
f.printAssignment(out);
}
out.append("\t}").append(System.lineSeparator());
}
private void printArgs(List<Field> finalFields) throws IOException {
if(finalFields.size() == 0) return;
Iterator<Field> iter = finalFields.iterator();
while(true) {
Field next = iter.next();
next.printTypeAndName(out);
if(!iter.hasNext()) break;
out.append(", ");
}
}
private List<Field> generateFields(boolean isfinal) {
int numFields = r.nextInt(3) + 2;
List<Field> newFields = Lists.newArrayListWithCapacity(numFields);
for(int i = 0; i < numFields; i++) {
String type = TYPES[r.nextInt(4)];
newFields.add(new Field(type, wordList.makeLower(r.nextInt(2) + 1), isfinal));
}
return newFields;
}
private void printFields(List<Field> finalFields) throws IOException {
for(Field f : finalFields) {
f.printFieldDeclaration(out);
}
}
private String beginClass() throws IOException {
out.append("public class ");
String className = wordList.nextClassName(CLASS_NAME_LENGTH);
out.append(className).append(" {").append(System.lineSeparator());
return className;
}
private void endClass() throws IOException {
out.append("}");
}
private static class WordList {
private final Random r = new Random();
private final List<?> source;
private WordList(List<?> source) {
this.source = source;
}
private String makeUpper(int length) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < length; i++) {
sb.append(randomWord());
}
return sb.toString();
}
private String makeLower(int length) {
return TOLOWER.convert(makeUpper(length));
}
private String randomWord() {
int sourceIndex = r.nextInt(source.size());
return TOUPPER.convert(source.get(sourceIndex).toString().toLowerCase());
}
public String nextClassName(int length) {
return makeUpper(length);
}
}
private static class Field {
private final String type;
private final String fieldName;
private final boolean isfinal;
Field(String type, String fieldName, boolean isfinal) {
this.type = type;
this.fieldName = fieldName;
this.isfinal = isfinal;
}
void printFieldDeclaration(Appendable appendable) throws IOException {
appendable.append("\tprivate ");
if(isfinal) appendable.append("final ");
printTypeAndName(appendable);
appendable.append(';').append(System.lineSeparator());
}
void printTypeAndName(Appendable appendable) throws IOException {
appendable.append(type).append(' ').append(fieldName);
}
void printGetter(Appendable appendable) throws IOException {
appendable.append("\tpublic ");
appendable.append(type).append(" get").append(TOUPPER.convert(fieldName));
appendable.append("() {").append(System.lineSeparator());
appendable.append("\t\treturn ").append(fieldName).append(';');
appendable.append(System.lineSeparator()).append("\t}").append(System.lineSeparator());
}
void printSetter(Appendable appendable) throws IOException {
appendable.append("\tpublic void set");
appendable.append(TOUPPER.convert(fieldName));
appendable.append("(").append(type).append(' ').append(fieldName);
appendable.append(") {").append(System.lineSeparator());
printAssignment(appendable);
appendable.append("\t}").append(System.lineSeparator());
}
void printAssignment(Appendable appendable) throws IOException {
appendable.append("\t\tthis.").append(fieldName).append(" = ").append(fieldName);
appendable.append(';').append(System.lineSeparator());
}
}
private static class SleepingAppendable implements Appendable {
private Random r = new Random();
private Appendable backing;
private boolean sleeping = true;
public SleepingAppendable(Appendable backing) {
this.backing = backing;
}
@Override
public Appendable append(CharSequence csq) throws IOException {
return append(csq, 0, csq.length());
}
@Override
public Appendable append(CharSequence csq, int start, int end)
throws IOException {
for(int i = start; i < end; i++) {
append(csq.charAt(i));
}
sleep(100, 300);
return this;
}
@Override
public Appendable append(char c) throws IOException {
sleep(170, 80);
backing.append(c);
return this;
}
private void sleep(int base, int variation) {
if(!sleeping) return;
try {
Thread.sleep((long) (r.nextInt(80) + 70));
} catch (InterruptedException e) {
}
}
public boolean isSleeping() {
return sleeping;
}
public void setSleeping(boolean sleeping) {
this.sleeping = sleeping;
}
}
}
Sample program output (just one program)
public class GetGroup {
private final double thoughRight;
private final double socialYear;
private final double manOne;
private final int appear;
private double man;
private double comeHis;
private double certain;
public GetGroup(double thoughRight, double socialYear, double manOne, int appear) {
this.thoughRight = thoughRight;
this.socialYear = socialYear;
this.manOne = manOne;
this.appear = appear;
}
public double getThoughRight() {
return thoughRight;
}
public double getSocialYear() {
return socialYear;
}
public double getManOne() {
return manOne;
}
public int getAppear() {
return appear;
}
public double getMan() {
return man;
}
public double getComeHis() {
return comeHis;
}
public double getCertain() {
return certain;
}
public void setMan(double man) {
this.man = man;
}
public void setComeHis(double comeHis) {
this.comeHis = comeHis;
}
public void setCertain(double certain) {
this.certain = certain;
}
}
Another sample output:
public class TryControl {
private final int over;
private final double thatState;
private final long jobInto;
private final long canPut;
private int policy;
private int neverWhile;
public TryControl(int over, double thatState, long jobInto, long canPut) {
this.over = over;
this.thatState = thatState;
this.jobInto = jobInto;
this.canPut = canPut;
}
public int getOver() {
return over;
}
public double getThatState() {
return thatState;
}
public long getJobInto() {
return jobInto;
}
public long getCanPut() {
return canPut;
}
public int getPolicy() {
return policy;
}
public int getNeverWhile() {
return neverWhile;
}
public void setPolicy(int policy) {
this.policy = policy;
}
public void setNeverWhile(int neverWhile) {
this.neverWhile = neverWhile;
}
}
There will be an app for this in Ubuntu 15.04: Hollywood Technodrama, powered by byobu. (Link includes video & screenshots.)
– ojdo – 2015-01-05T13:24:38.567First you don't want to do the job you are supposed to do, but you also don't want to do the job of hiding that you are not doing it. The next step is to ask someone to post this on stackexchange and try out the solutions, so that you don't have to do it yourself. – Florian F – 2016-02-05T12:32:45.860
6What's the winning criteria, [tag:popularity-contest]? – Kyle Kanos – 2014-06-07T01:37:27.593
I think it's technically code-challenge. – undergroundmonorail – 2014-06-07T01:50:20.913
Just so you know, Linux Mint 17 (LTS) is out! – wchargin – 2014-06-07T03:10:57.353
5
still waiting for someone to reimplement http://visikord.com/ghostzilla/
– enthdegree – 2014-06-07T06:02:54.413@KyleKanos Clarified winning condition. – SomeKittens – 2014-06-07T06:09:20.790
@WChargin I've been meaning to upgrade. I need it! – SomeKittens – 2014-06-07T06:10:37.063
36So... what happens if you test an answer and it actually does get you fired? – Bob – 2014-06-07T11:28:00.527
54This gives me an idea for another code golf question. "Make it look like my question shouldn't be put on hold" – twiz – 2014-06-07T13:26:13.733
6
What about the good old http://hackertyper.net/ ?
– german_guy – 2014-06-08T17:45:35.2639
Just write one of these programs, and compile!
– ugoren – 2014-06-08T19:15:23.7531@german_guy the only problem with hackertyper is it requires user intervention – Loktar – 2014-06-08T20:55:13.200
13
I saw a really good remake of the Tron Legacy boardroom on Github the other day: https://github.com/arscan/encom-boardroom
– Paul Prestidge – 2014-06-09T04:40:54.4171@Chron wow thats really good actually! – Loktar – 2014-06-09T14:34:25.067
2"Your challenge is to write this script for me." I think I fractured a funny bone LoL! – Klik – 2014-06-10T19:04:49.633
1@Chron, i'm impressed. Gonna bookmark that page and use it a lot ^_^ – Mathlight – 2014-06-11T12:42:15.500
15 bonus upvotes? Do you have sock puppets? ;-) – Canadian Luke – 2014-06-11T16:43:12.910
@CanadianLuke When it comes to the scoring. – SomeKittens – 2014-06-11T17:05:19.150
I feel like I shouldn't upvote that really great question - future employees now reading StackExchange network messages. – Szymon Toda – 2014-06-11T19:51:01.173
reminds me this video: https://www.youtube.com/watch?v=JqUmXDU6cJE
– Tomas – 2014-06-11T22:12:43.6301It may be relevant for people to know what your job actually is. Then again, it may not. – Thorbjørn Ravn Andersen – 2014-06-12T13:37:32.443
1Is there a way to do this so I can watch the world cup and make it look like I am working? – ford prefect – 2014-06-13T16:40:59.530
2I love the thought that dozens of people are not doing productive work to help you (and the rest of the technical workers in the world) avoid doing productive work. – Art Taylor – 2014-06-13T19:50:39.607
Why don't you do other work if you don't like it... – stommestack – 2014-06-13T21:35:46.860
3I hope your boss doesn't see this thread... – Kaz Wolfe – 2014-06-14T04:16:52.633
Back in the early 90s, "Chessmaster" had a button labeled "The Boss" which brought up a screenshot of some spreadsheet program. Took me a while to figure out what the heck it was for.
– Johnny Mopp – 2014-06-15T12:49:35.740@SomeKittens How is the testing going? – PyRulez – 2014-06-16T14:22:52.310
@PyRulez boring. No comments either way. – SomeKittens – 2014-06-16T17:06:25.893