Problem Set 2 - Strings & Conditionals

Assigned: January 21, 2026

Due: January 28, 2026 at 11:59 PM EST

Objectives

Instructions

For each problem, create a class named ProblemX, where X is the problem number (e.g., Problem1.java). Write corresponding JUnit test classes named ProblemXTest. Each method must include proper Javadoc comments describing the (1) purpose of the method, (2) parameters, and (3) return value.

Do not round your solutions. For example, do not use Math.round or float your output to, e.g., 2 decimal places, unless specified.

What You Cannot Use

You may not use anything beyond Chapter 2.1. This includes but is not limited to:

Any violation results in a score of 0 on the problem set.

Please contact a staff member if you are unsure as to whether you're allowed to use something.

Notes and Version Changes:

Problems

  1. Design the String cutUsername(String email) method that receives an email address of the form X@Y.Z and returns the username. The username of an email address is X.

    Files to Submit: Problem1.java, Problem1Test.java

  2. Design the boolean lessThan20(int x, int y, int z) method that, when given three integers x, y, and z, returns whether or not at least one of them is less than twenty away from another. For example, lessThan20(19, 2, 412) returns true because 2 is less than 20 away from 19. Another example is lessThan20(999, 888, 777), which returns false because none of the numbers have a difference less than twenty.

    Files to Submit: Problem2.java, Problem2Test.java

  3. Design the boolean isEvenlySpaced(int x, int y, int z) method, which receives three integers x, y, and z, and returns whether they are evenly spaced. Evenly spaced means that the difference between the smallest and medium number is the same as the difference between the medium and largest number.

    Files to Submit: Problem3.java, Problem3Test.java

  4. Design the String cutTry(String s) method, which receives a string s and, if s ends with "try", it is removed. Otherwise, the original string is returned.

    Files to Submit: Problem4.java, Problem4Test.java

  5. In propositional logic, there are several connectives that act on boolean truth values. These include logical conjunction \(\land\), disjunction \(\lor\), conditional \(\to\), biconditional \(\leftrightarrow\), and negation \(\lnot\). We can represent schemata as a series of composed method calls. For example, an evaluation of \(P \to \lnot (Q \leftrightarrow \lnot R)\) are assigned to false and \(\texttt{Q}\) is assigned to true, is equivalent to:

    static final boolean P = false;
    static final boolean Q = true;
    static final boolean R = false;
    
    cond(P, not(bicond(Q, not(R))))

    The presented schema resolves to true. Design methods for the five connectives according to the following truth tables. These methods should be called cond, bicond, and, or, and not. Assume that \(\mathrm{T}\) is true and \(\mathrm{F}\) is false.

    \[ \begin{array}{c|c} P & \lnot P \\ \hline \mathrm{T} & \mathrm{F} \\ \mathrm{F} & \mathrm{T} \end{array} \]

    \[ \begin{array}{c@{\qquad\qquad}c} \begin{array}{cc|c} P & Q & P \land Q \\ \hline \mathrm{T} & \mathrm{T} & \mathrm{T} \\ \mathrm{T} & \mathrm{F} & \mathrm{F} \\ \mathrm{F} & \mathrm{T} & \mathrm{F} \\ \mathrm{F} & \mathrm{F} & \mathrm{F} \end{array} & \;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\; \begin{array}{cc|c} P & Q & P \lor Q \\ \hline \mathrm{T} & \mathrm{T} & \mathrm{T} \\ \mathrm{T} & \mathrm{F} & \mathrm{T} \\ \mathrm{F} & \mathrm{T} & \mathrm{T} \\ \mathrm{F} & \mathrm{F} & \mathrm{F} \end{array} \end{array} \]

    \[ \begin{array}{c@{\qquad\qquad}c} \begin{array}{cc|c} P & Q & P \to Q \\ \hline \mathrm{T} & \mathrm{T} & \mathrm{T} \\ \mathrm{T} & \mathrm{F} & \mathrm{F} \\ \mathrm{F} & \mathrm{T} & \mathrm{T} \\ \mathrm{F} & \mathrm{F} & \mathrm{T} \end{array} & \;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\; \begin{array}{cc|c} P & Q & P \leftrightarrow Q \\ \hline \mathrm{T} & \mathrm{T} & \mathrm{T} \\ \mathrm{T} & \mathrm{F} & \mathrm{F} \\ \mathrm{F} & \mathrm{T} & \mathrm{F} \\ \mathrm{F} & \mathrm{F} & \mathrm{T} \end{array} \end{array} \]

    Files to Submit: Problem5.java, Problem5Test.java

  6. Design the boolean isInsideRectangle(double rx, double ry, double w, double h, double px, double py) method that, when given a rectangle centered at \((r_x, r_y)\), width \(w\) and height \(h\) as well as a point \((p_x, p_y)\), returns whether the point is located strictly inside the rectangle.

    Files to Submit: Problem6.java, Problem6Test.java

  7. Carlo is shipping out orders of candy to local grocery stores. Boxes have a maximum weight defined by a value \(w\), and we can (potentially) fit both small and large bars of candy in a box. Design the int fitCandy(int s, int l, int w) method that, when given a number of small bars \(s\), large bars \(l\), and maximum weight \(w\), determines the number of small candy bars he can fit in the box. Large bars weigh five kilograms, and small bars weigh one kilogram. Note that Carlo always tries to fit large candies first before small. Return -1 if it is impossible to fill the box with the given criteria. Below are some test examples. You cannot use loops, recursion, or data structures to solve this problem. Hint: consider this as an analysis of three cases.

    fitCandy(4, 1, 9)      => 4
    fitCandy(4, 1, 4)      => 4
    fitCandy(1, 2, 6)      => 1
    fitCandy(6, 1, 13)     => -1
    fitCandy(60, 100, 550) => 50
    fitCandy(7, 1, 12)     => 7
    fitCandy(7, 1, 13)     => -1

    Files to Submit: Problem7.java, Problem7Test.java

  8. An IPv4 address contains four integer values stored in four octets, separated by dots. For instance, 192.168.1.244 is a valid IPv4 address. Another example is 149.165.192.52. Design the boolean isValidIpv4(String ip) method that, when given a string, determines whether or not it represents a valid IPv4 address. Each octet must be an integer between 0 and 255 inclusive. Note that some IPv4 addresses are, in reality, nonsensical, e.g., 0.0.0.0, but we will not consider these as invalid.

    Below the examples is a helper method, isNumeric, which returns whether or not a string is "numeric," i.e., if the string represents an integer. Understanding how this helper method works is unimportant for the time being. You will need to use Integer.parseInt, substring, and indexOf.

    Warning: Again, you cannot use, e.g., arrays, loops, or regular expressions to solve this problem, and if you do, you will receive a 0 on the entire problem set.

    isValidIpv4("192.168.1.244")    => true
    isValidIpv4("149.165.192.52")   => true
    isValidIpv4("192.168.1.256")    => false
    isValidIpv4("192.168.1201.23")  => false
    isValidIpv4("192.168.1201.ABC") => false
    isValidIpv4("ABC.DEF.GHI")      => false
    isValidIpv4("192.168.1A6.201")  => false
    /**
     * Determines whether or not we can convert a given string into 
     * an integer datatype.
     * @param n input string.
     * @return true if we can convert n to an int, false otherwise.
     */
    static boolean isNumeric(String n) {
      try {
        Integer.parseInt(n);
        return true;  
      } catch (NumberFormatException ex) {
        return false;
      }
    }

    Files to Submit: Problem8.java, Problem8Test.java