Easily browse Java files used in CodeHS, or download Java source files.
ConsoleProgram allows students to easily write basic Java programs with user input, and abstracts away the main method into a simpler run method.
// Hello world program
public class Hello extends ConsoleProgram
{
public void run()
{
System.out.println("Hello world!");
}
}
readLine, readInt, readBoolean, and readDouble let users easily and safely use user input, with a thin wrapper over Scanner.
// Hello world program
public class UserInput extends ConsoleProgram
{
public void run()
{
// Will keep prompting until you type an int
int number = readInt("What is your favorite number?");
System.out.println(number);
String name = readLine("What is your name?");
System.out.println(name);
double rating = readDouble("Enter a rating 0-10: ");
System.out.println(rating);
boolean replay = readBoolean("Play again (true/false)");
System.out.println(replay);
}
}
Randomizer is a thin layer on top of the Java Random class, but lets students use the methods as static methods.
// Hello world program
public class RandomTester extends ConsoleProgram
{
public void run()
{
int roll = Randomizer.nextInt(1, 6);
System.out.println(roll);
// Simulate a weighted coin flip
if(Randomizer.nextBoolean(0.3))
{
System.out.println("Flipping a weighted coin was heads!");
}
// Print out a random double with a range
System.out.println(Randomizer.nextDouble(0, 100));
}
}
ls
and cd
.cd Downloads/HelloWorldTerminalotherwise it will be in a different folder.
javac Hello.java
java Hello HelloWhat's happening here is we are running the program called Hello and passing it as an argument the String "Hello" so that it knows which file to run.