CS 112
Spring 2024

Lab 1: Getting started, Java basics

Task 0: Review lab policies

Note

You cannot get credit for completing the lab tasks at home and coming to lab only to sign the attendance sheet. Part of participation is attending the lab to interact and ask questions.

Task 1: Simple debugging

Using folders

You should create a separate folder for each lab.

If you haven’t already created a folder named cs112 for your work in this course, follow these instructions to do so.

Then create a subfolder called lab1 within your cs112 folder for your work on this lab, and put all of the files for Lab 1 in that folder.

In Lab 0, you should have installed VS Code on your computer. Let’s start with a simple exercise to get used to the VSC development environment.

  1. Download the following file: Debugging.java

    Make sure to put the file in the lab1 folder that you created above. If your browser doesn’t allow you to specify where the file should be saved, try right-clicking on the link above and choosing Save as... or Save link as..., which should produce a dialog box that allows you to choose the correct folder for the file.

  2. In VS Code, select the File->Open Folder or File->Open menu option, and use the resulting dialog box to find and open your lab1 folder. (Note: You must open the folder; it is not sufficient to simply open the file.)

    The name of the folder should appear in the Explorer pane on the left-hand side of the VS Code window, along with the name of the Debugging.java file that you downloaded above.

  3. Click on the name Debugging.java, which will open an editor window for that file.

  4. Now pretend you are the Java compiler and try finding all the bugs in the program. How many errors can you find?

    You can try to run the program by using the F5 key, or by right-clicking the name of the program in the Explorer pane and choosing Run or Run Java.

  5. If you fixed all of the syntax errors, your program should run. If it doesn’t, look through the compiler error messages and see if you can fix the remaining issues. Make all of the fixes

  6. 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 this task you will write a simple program that involves user input and conditional execution.

Getting started

  1. As needed, open your lab1 folder using the File->Open Folder or File->Open menu option in VS Code.

  2. Select File->New File, which will open up an empty editor window.

  3. Select File->Save, and give the new file the name Greeting.java.

Writing the program

  1. Add comments at the top of the new file that include:

    • a brief description of what the program is supposed to do.
    • your name and email address

    See the comments that we provided in the starter code for previous task for an example of what they should look like.

  2. Below the comments – and before any class header – add an import statement for the java.util package:

    import java.util.*;
    

    Including this import statement will allow you to create a Scanner object in your program.

  3. Create a class named Greeting that will serve as a container for your program.

  4. Inside the Greeting class, add a main method with the usual header.

  5. Start the main method by creating a Scanner object for getting user inputs from the keyboard and assigning it to an appropriate variable.

  6. Now add code to the main method of your program so that it:

    • issues a prompt asking for the user’s first name
    • reads the name from the keyboard
    • displays a polite (or not so polite!) greeting message
    • prompts the user to enter their 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. Here’s an example of what a run of the program should look like:

    Please enter your first name: Gloria
    Hello Gloria, Welcome to CS112!!!
    How old are you Gloria? 57
    
  7. Enhance your program to output a personal insult based on the value of age that was entered. Use the following age-range descriptions when creating your insults:

    age range  description
    -----------------------------
    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 first name: Aileen
    Hello Aileen, Welcome to CS112!!!
    How old are you Aileen? 21
    
    Wow Aileen! You just made it!
    
    Please enter your name: Mike
    Hello Mike, Welcome to CS112!!!
    How old are you Mike? 19
    
    Wow Mike! You have 2 more years to go!!
    
    Please enter your name: John
    Hello John, Welcome to CS112!!!
    How old are you John? 13
    
    Wow John! You are such a dweeb!!
    

Task 3: Debugging a static method

Let’s continue to practice our debugging skills using VS Code. This time we will focus on debugging a simple static method.

  1. Download the following file: Debugging2.java

    Make sure to put the file in the lab1 folder that you created above. If your browser doesn’t allow you to specify where the file should be saved, try right-clicking on the link above and choosing Save as... or Save link as..., which should produce a dialog box that allows you to choose the correct folder for the file.

  2. If your lab1 folder isn’t already open, use File->Open Folder or File->Open to find and open it.

  3. Click on the name Debugging2.java in the Explorer Pane on the left-hand side of VS Code, which will open an editor window for that file.

    You should see a simple static method called triArea, and a main method that includes an example test call to that method.

  4. Fix all of the syntax errors in triArea that prevent the program from compiling and running. Once everything works, you should see the following:

    Testing the triArea method
    triArea(10, 3) is 15.0
    
  5. Once the code compiles and runs, you should test the method for any logic errors.

    The provided test produces the correct result, since the area of a triangle with base 10 and height 3 is indeed 15.0.

    However, one test is almost never good enough! Try adding this line to your main method and rerunning the program:

    System.out.println("triArea(9, 3) is: " + triArea(9, 3));
    

    You should see a result of 12.0. However, the actual area of a triangle with base 9 and height 3 is 13.5.

  6. To fix this logic error, we need to realize the following:

    • Java’s / operator is used for both integer division and floating-point division.

    • If both of the operators are integers, / performs integer division, which truncates the digits after the decimal. This is what is happening when we compute b/2.

    • In order to get floating-point division — which preserves the digits after the decimal — we need to make sure that at least one of the operands is a floating-point number.

    Go ahead and make the change needed to get floating-point division.

  7. Rerun your program to ensure it works.

Task 4: Practice with Java methods

  1. Download the following file: Methods.java

    Make sure to put the file in the lab1 folder that you created above. If your browser doesn’t allow you to specify where the file should be saved, try right-clicking on the link above and choosing Save as... or Save link as..., which should produce a dialog box that allows you to choose the correct folder for the file.

  2. If your lab1 folder isn’t already open, use File->Open Folder or File->Open to find and open it.

  3. Click on the name Methods.java in the Explorer Pane on the left-hand side of VS Code, which will open an editor window for that file.

  4. You should see a sample method called print3Times that takes a string as its parameter and prints it three times. For example:

    print3Times("hello");
    

    should output:

    hello
    hello
    hello
    

    One thing worth noting: the header of the method has the word void in it. Make sure you understand what that means, and ask us if you’re not sure.

  5. Add a main method to the program, and add a test call to main for print3Times. Then run the program to see if produces the correct result.

  6. Write a static method called printNTimes that takes an integer n and a string (in that order) as its parameters and prints the string n times. For example:

    printNTimes(5, "hello");
    

    should output:

    hello
    hello
    hello
    hello
    hello
    

    and,

    printNTimes(2, "hello");
    

    should output:

    hello
    hello
    
  7. Add one of more test calls to main for your new method, and run the program to ensure that it works.

More extra practice!

If you get through the exercises above, congratulations!

For extra practice, you can try some exercises from an excellent site called Practice-It.