Midterm Lab Exam #2 Practice

Provided files:

Problem 1: Square.java

Download Square.java. You must define a class that models a square. This particular program does not take any input, be it from the user directly or via command-line arguments. The expected output of this program is shown below:

With an initial side length of 5:
Side length: 5
Area: 25
After setting side length to 10:
Side length: 10
Area: 100

More details are given in the comments in Square.java.

Problem 2: MaxRandom.java

Download MaxRandom.java. You must write code to get the given main method compiling and producing the correct output. This program takes a seed value as a command-line argument. Example output with the command-line argument 123 is shown below:

Max 1: 1
Max 2: 2
Max 3: 3
Max 4: 4
Max 5: 3
Max 6: 4

Further example output is shown below, with the command-line argument 321:

Max 1: 1
Max 2: 1
Max 3: 3
Max 4: 2
Max 5: 0
Max 6: 5

The comments in MaxRandom.java provide further details.

Problem 3: HasString.java

Download HasString.java. This program is intended to implement a class with a toString method. Example output for this program is shown below:


More details are given in the comments in HasString.java.

Problem 4: Compare.java

Download Compare.java. This program will print different things, depending on the values of three command-line arguments provided by the user. More details are given in the comments in Compare.java.

Problem 5: Testing Conditions

Download Conditions.java and ConditionsTest.java. You don't need to make any changes to Conditions.java, but you'll need to write a sufficient number of tests in ConditionsTest.java to test all the branches of the something method in Conditions.java.


The solutions for each of the files are below. Do not look at the solutions until you have tried to solve the problems yourself. Being able to verify that a given solution is correct is a different problem than deriving a correct solution, and the exam has you derive correct solutions.