CS 112
Spring 2026
  • Home
  • Syllabus
  • Labs
  • Problem Sets
  • Staff
  • Office Hours
  • Resources
  • Collaboration
  • Policies
  • Lectures
  • Piazza
  • Gradescope

Lab 2: Moving on from the basics...

  • Task 1: Debugging a Simple Program with static methods
  • Task 2: Logical Decomposition

Task 1: Debugging a Simple Program with static methods

Download the following file on your computer (or copy and paste in your IDE) and try finding all the intentional bugs in the program.

Debugging2.java

  1. Once our code compiles, we should test our methods. We have two ways we can do so.

    1. We can use the main method in our .java file and include calls to test our method. Example:

      System.out.println(triArea(10, 3));
      

      should output:

      15.0
      
    2. We can write a separate simple test class containing a main method that includes calls to test our method.

      System.out.println(DebuggingErrors.triArea(10, 3));
      

      should output:

      15.0
      

      Note the difference! Because this call is now being made in a method which is written in a separate class to which the method triArea belongs, we need to prepend the class name when we call the method.

    In both cases, this result is correct, because the area of a triangle with base 10 and height 3 is indeed 15.0.

  2. One test is almost never good enough! Try adding this line to (one of) your main functions and rerunning. What is the result? Is it correct?

    System.out.println(DebuggingErrors.triArea(9, 3));
    

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

  3. 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.

  4. Save your code and rerun your program to ensure it works out.

Task 2: Logical Decomposition

Download Greetings.pdf which contains a specification for the program we would like you to write. As you will see it is an enhanced version of the program you wrote in the previous lab.

Wrire your program on the paper copy that the TAs will give you. At the end of lab, please create a PDF of your code and submit it to the portal in Gradescope.

Last updated on February 3, 2026.