CodeHSJava Libraries

Easily browse Java files used in CodeHS, or download Java source files.

Quick Start

ConsoleProgram

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!");
    }
}
                     

Simple User Input

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

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));
    }
}
                     

Running in Eclipse

Quick ConsoleProgram HelloWorld in Eclipse

  1. Download the zip of the Hello project. and unzip it.
  2. Select File/Import, then choose Existing Project into Workspace, then selec the Hello folder.
  3. Now to run Hello.java, right click it select "Run As", then "Run Configurations"
  4. Create a new Run Configuration with these values
    On the Main tab:
    Project: Hello
    Main Class: ConsoleProgarm
    on the Arguments tab:
    Program arguments: Hello
  5. Then click the green run arrow, and run Hello

Creating HelloWorld in Eclipse from Scratch

  1. Download the zip of the source files here and unzip them.
  2. Create a new Java project in Eclipse by selecting File/New/Java Project
  3. Right click on the project, select New/Class. Enter "Hello" as the name of the class. Add your file.
  4. Right click on the src folder, select "Import" then choose file system. Then choose the folder you just downloaded. Then select all files.
  5. Now to run Hello.java, right click it select "Run As", then "Run Configurations"
  6. Create a new Run Configuration with these values
    On the Main tab:
    Project: Hello
    Main Class: ConsoleProgarm
    on the Arguments tab:
    Program arguments: Hello
  7. Then click the green run arrow, and run Hello

Running in BlueJ

Quick ConsoleProgram HelloWorld in BlueJ

  1. Download the zip of the Hello project. and unzip it.
  2. Double click the package.bluej file to open the BlueJ project
  3. Right click on Hello and select "void main(String[] args)"
  4. Then as the arguments type {"Hello"}

Running in the Terminal

Java HelloWorld in the Terminal

  1. Download the zip of the Hello project and unzip it.
  2. Save it to the Downloads folder. If you save it somewhere else then you will need to navigate there in the terminal using ls and cd.
  3. Open your terminal (on Macs the Terminal app)
  4. Change to the right directory. If you put it in Downloads it will be:
    cd Downloads/HelloWorldTerminal
    otherwise it will be in a different folder.
  5. Compile the code with
    javac Hello.java
  6. Run the code with
    java Hello Hello
    What'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.