HW2: Hello World and Guessing Game

25 points; due Fri 4/4 @ 11am.

Goals

Getting familiar with Java.

Setup and Requirements

This is an individual assignment. Make sure first that you can log in to the CS machines.

Part 1: Your First Java Program

  1. Open up your favorite code editor.
  2. Create a file named “HelloWorld.java”.
  3. Inside of this file, create a class named “HelloWorld” by typing (not copying-and-pasting) the code:
    public class HelloWorld {
    
    }
    

    (Remember that in Java, your class and your compilation unit (the file) must have exactly the same name: capitalization and everything.)

  4. Inside of the HelloWorld class, add a main method by typing the code:
    public static void main(String[] args) {
    
    }
    

    (Similar to Python, main is a special name with a special declaration. The main method is where your program's execution will begin.)

  5. Inside of your main method, add code that will print out a greeting message:
    System.out.println("Hello, World!");
    
  6. Now use the command line to compile and run your Java code:
    $ javac HelloWorld.java
    $ java HelloWorld
    Hello, World!
    
  7. Note: unlike Python, Java is a compiled language. Therefore, before you execute your program, you have to compile it using javac (“Java Compiler”). This converts your HelloWorld.java code into a HelloWorld.class file that contains the machine-language version of your program that can run on a Java Virtual Machine (VM). Then, you run java (the Java VM) and give it the name of your class that it will execute. The practical implication of this is that any time you alter your code, to run the updated version you will need to run both javac and java again.

  8. Now change your program so that it prints out your name in addition to the message “Hello, World!”. Save it and be prepared to submit it after you're done with the next part.

Part 2: Your Second Java Program

Now you'll write a guessing game in Java.

Create a new file Guess.java, in which you define the class Guess.

Inside of class Guess, write a program that will:

  1. Choose a random integer from 1 to 100 (use either Math.random() or java.util.Random).
  2. Ask a user to guess your integer.
  3. If the user guesses above your integer, print out “smaller!” and ask again.
  4. If the user guesses below your integer, print out “larger!” and ask again.
  5. If the user guesses your integer, print out “you got it!” and quit.

The Java language constructions that you will need to use can be found in Appendix A of the book. Use of the Scanner class (for input) is in Section A.32, Math methods are in Section A.30, loop constructs are A.58, etc.

Submission and Grading

Submit your two Java files — “HelloWorld.java” and “Guess.java” — by zipping them up into hw2.zip, then uploading them to Moodle. Make sure that your zip file contains only these two files (copy it somewhere else on your computer and open it to check): no directories and no extraneous stuff please.

Start early, ask lots of questions, and have fun!