Midterm Lab Exam #2 Practice


This is a an ungraded practice exam for the second midterm exam. In the practice environment, you are given 45 minutes to answer these questions. In a real exam environment, you will have the entire class, though there will be more questions.

You might not have enough time to complete all the questions. If you have to prioritize your time, it is better to get one problem working than it is to attempt two problems and have them both broken. With that in mind, it is recommended to read all the problems first, determine which are easiest to solve, and then solve them.

Unlike the normal assignment environment, you may NOT discuss with anyone else, and you may NOT access the Internet, except for the course materials in Canvas. The point of this practice exam is to simulate a real exam environment.

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: CommandLineLetters.java

Download CommandLineLetters.java. This program is intended to take command-line arguments, and will print out each argument that begins with either an 'a', a 'b', or a 'c'. Example output for this program is shown below, with the command-line arguments apple pear banana lime cucumber orange:

apple
banana
cucumber

If you don't know how to get the first character of a String, but you know how to do other parts of the problem, then write a “stub” method that takes a String and returns a char. This method can return any character that you want; the point of writing this is to separate the specific part of getting a character from the rest of the problem.


When Done

If you complete all the above problems before the time is up, go ahead and begin the written portion of the practice exam.