CS 112
Fall 2020

Old version

This is the CS 112 site as it appeared on December 31, 2020.

Lab 2: The Basics: Understanding Program Flow

Task 0: The Basics

Using folders

We strongly encourage you to create a separate folder for each lab and each problem set, so that you can more easily keep track of your work. For example, you could create a folder called lab2 for your work on this lab, and put all of the files for this lab in that folder.

In Lab 1, you practiced writing a basic Java program to output Hello World using VSC, or some other IDE that you are comfortable with.

In today’s lab we will continue learning to write and read Java code with a few simple exercises.

Task 1: Simple Debugging

Let’s start with a simple exercise to get used to the Development environment. Using a GUI based editor gives us multiple advantages by highlighting and marking a multitude of errors while coding. Download the following file on your computer and try finding all the intentional bugs in the program.

Debugging.java

Open up Debugging.java and pretend that you are the Java compiler. How many errors can you find?

  1. Load up the program into your IDE (follow TF instructions), fix all the errors and try to run your program. If you fixed all the compilation errors your program should run. If you did not find all the errors, look through the compiler errors to see if you can fix the remaining. Run your program once all the errors are fixed.

  2. Now look at the result. Is it correct? If not, there may be a logic error. Can you find it?

Task 2: Writing our first custom program

In case you are not able to complete the challenge portion in lab today, it can be a fun weekend activity! Remember lab solutions will be posted!

  1. Write a Java program that greets you. Add code to the main method of your program to issue a prompt asking for your name, display a polite (or not so polite) greeting message and then prompt you to enter your age.

    Use the methods of the Scanner class to perform the user input. You should use the method next to read and return a string value and nextInt to read and return an integer value. Your program may run as follows:

    Please enter your name: Christine
    Hello Christine, Welcome to CS112!!!
    Christine how old are you? 58
    
  2. Enhance your program to output a personal insult based on the value of age that was entered. Use the following age cuttoffs for creating your insults:

    1 <= 10 everyone is sweet
    11 <= 17 they are dweebs
    18 <= 20 they are counting down to legal age
    21 exactly they just made legal age
    22 <= 29 they are counting down to 30
    30 <= 40 they are suffering adults
    41 < 50 they are miserable adults
    >= 50 you are speechless!!
    

    A few possible sample runs (but feel free to be creative!):

    Please enter your name: Aileen
    Hello Aileen, Welcome to CS112!!!
    Aileen how old are you? 21
    
    Wow Aileen! You just made it!
    
    Please enter your name: Mike
    Hello Mike, Welcome to CS112!!!
    Mike how old are you? 19
    
    Wow Mike! You have 2 more years to go!!
    
    Please enter your name: John
    Hello John, Welcome to CS112!!!
    John how old are you? 13
    
    Wow John! you are such a dweeb!!
    

Task 3: Challenge: Enhanced Program Control

  1. For those of you familiar and/or comfortable with Java loops, enhance your program to continually prompt for new information - let’s say 3 times. What loop would you use? How about if you wanted the program to run continuously until you have had enough? What loop construct would you use then? If you are not familiar with Java loops, here is some fun bedtime reading!