Z OLD Homework 00 - james-bern/CS136 GitHub Wiki

image

README

the 50 ft rule

setup-loop structure

A simple structure for an interactive computer program is to first do some stuff once ("setup"), and then enter into a loop that executes over and over forever (or at least until, for us, the user presses the Reset button or Control + C.)

class Main {
    public static void main(String[] arguments) {
        // setup
        System.out.println("This line gets executed only once.");

        while (true) {
            // loop
            System.out.println("This line gets executed over and over.");
        }
    }
}

๐Ÿงช Don't just take my word for it! Try actually running the above code!

Specification

  • To get an A:
    • Extend the starter code so that it tells the player how many days they have been in the room when they wake up. Each time they go to sleep, this number should increase by one.
    • Extend the starter code to make the game a bit more fun and tell us something unique about you (e.g., that you like to cook; that you don't like the number three; or that your favorite color is clear) ๐Ÿ™‚๐Ÿ‘
    • Note: Please write all your code inside of the main function.
    • Hint: You can use the Interactions pane to quickly test things out.
    • Hint: We have Documentation. It's like Geeks for Geeks but with more emojis.
  • To get an S (A+): Make something awesome (that is still, somehow, a text-based choice game)! ๐Ÿ‘ฝ

Submission Instructions

  • Upload your code file to GradeScope by the due date.
  • Live demo your code to Jim a Grading TA in Lab on Wed/Thu.
    • ๐Ÿšจ You must do this in order to receive points.
    • Demo Checklist
      • Student understood and followed the 50 ft. rule (OR student does now and will do so moving forward)
      • Student is working on their laptop (OR student strongly prefers using the lab machines)
      • Student's GitHub is in light mode (OR student very strongly prefers dark mode)
      • Student is actually using DrJava (OR student strongly prefers, e.g., Vim)
      • Student has DrJava set up to indent with 4 Spaces and display Line Numbers (OR student strongly prefers, e.g., 3 spaces, AND is indenting consistently)
      • Student has been asked how they are feeling about CSCI 136 (if concerned, Student has been informed about the Purple Tuesday 7-9 HW/Lecture Help Hours) ๐Ÿ™‚๐Ÿ‘

Starter Code

Please paste this code into a file named HW00.java.

import java.util.*;

class HW00 {
    public static void main(String[] arguments) {
        System.out.println("Welcome to CS136!");
        while (true) {
             System.out.println("You awaken in a dark room...");
             System.out.println("- Type 1 and press Enter to go back to sleep.");
             System.out.println("- Type 2 and press Enter to turn on the lights.");
            
            int choice = getIntFromUser();
            if (choice == 1) {
                System.out.println("You go to sleep. It is awesome.");
            } else if (choice == 2) {
                System.out.println("Congratulations, you have turned on the lights. :) The game is now over.");
                break;
            }
        }
    }


    // Returns the (first) integer the user typed.
    // Keeps waiting until the user types an int and presses Enter.
    static int getIntFromUser() {
        Scanner scanner = new Scanner(System.in);
        while (!scanner.hasNextInt()) { scanner.nextLine(); }
        return scanner.nextInt();
    }
}