CS 112
Fall 2026
  • Home
  • Syllabus
  • Staff
  • Office Hours
  • Labs
  • Problem Sets
  • Resources
  • Collaboration
  • 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 syntax 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. Use the main method in our .java file and include calls to test the method. Example:

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

      should output:

      triArea(10, 3) is: 15.0
      

      This has already been done for you in Debugging2.java.

    2. Create a separate simple test class in the same folder containing a main method that includes calls to test our method.

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

      should output:

      triArea(10, 3) is: 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.

    Note that we add some text to be printed with the test. This will help us stay organized as we add more test cases over time.

  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("triArea(9, 3) is:" + Debugging2.triArea(9, 3));
    
  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 Lab2Practicum.pdf which contains a specification for the program we would like you to write.

Write 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 September 10, 2026.