CPSC 124, Spring 2017: Sample Answers to Test #2

These are sample answers only. Often, answers that are less
detailed than the ones given here can still receive full credit.

Question 1. Using parameters is an important aspect of using subroutines. What are parameters, and why are they so important? Where are they found?

Answer. Parameters are used to send information into a subroutine when the subroutine is called. A subroutine can have formal parameters in its definition such as the str in public static void reverse(String str). When the subroutine is called, an actual parameter is passed to the subroutine, such as "Hello" in reverse("Hello"). The actual parameter value is assigned to the formal parameter so that its value can be used in the subroutine. Parameters are so important because they allow a subroutine to process different data each time it is called.

Question 2. An instance variable can be declared to be private. What does this mean? Why would it be done? What does this have to do with getter and setter methods?

Answer. A private instance variable can only be used in the class where it is declared. (More exactly, this means the "outer," non-nested class that contains the declaration of the variable.) This protects the variable from having anything done to it, except for what is done in that class. This makes it possible, for example, to be sure that the value of the instance variable is a valid value at all times. It is possible to allow indirect access to the variale from outside the class by writing a getter method for reading the value and/or a setter method for changing the value. Note that a setter method can ensure that the value that is being assigned to the variable is valid.

Question 3. Explain the meaning of "extends YourClass" in this first line from a class definition. (Your answer should mention inheritance.)

   public class MyClass extends YourClass {

Answer. This line says that MyClass is being defined as a subclass of YourClass. The class YourClass must already exist. All of the instance variables and methods that are already defined in YourClass will be inherited into MyClass (almost as if the definition of YourClass were re-typed into MyClass). Anything that I write in the definition of MyClass will either add to or override things that are inherited from YourClass.

Question 4. Write a static subroutine named shorterString that has two parameters of type String. The subroutine should return the string that has the smaller length. (If both strings have the same length, it can return the first parameter.)


  public static String shorterString( String s1, String s2 ) {
      if (s1.length() <= s2.length()) {
          return s1;
      else {
          return s2;

Question 5. Write a static subroutine with one parameter of type String and a return type of boolean. The subroutine should return true if the char 'Z' occurs in the string and should return false if not.


  public static boolean containsZ(String str) {
      for (int i = 0; i < str.length(); i++) {
          if (str.charAt(i) == 'Z') {
             return true;
      return false;

Note that this can be written more simply using the indexOf method from the String class:

  public static boolean containsZ(String str) {
      return str.indexOf('Z') >= 0;

Question 6. Write a static subroutine named fill with two parameters, where one parameter is an array of double, and the other is a simple double value. The subroutine should fill the entire array with copies of the second parameter. There is no return value.


  public static void fill( double[] array, double value ) {
      for (int i = 0; i < array.length; i++) {
          array[i] = value;

Question 7. Write a complete class definition for a class named Averager. An object of type Averager can be used to compute the average of a dataset of numbers. The class should have a private instance variable of type double to keep track of the total of the numbers and a private instance variable of type int to keep track of the count of the numbers. It should have an instance method that adds one number to the dataset; the method should add the specified number to the total and should add 1 to the count. There should also be a method that computes the average by dividing the total by the count; this method should return the average as its return value.


  public class Averager {
      private double total;  // sum of the numbers entered
      private int count;     // count of the numbers entered
      public void addData( double item ) {
          total = total + item;
          count = count + 1;
      public double getAverage() {
          return total / count;

Question 8. Write a code segment that will use the class from the previous problem to compute and print the average of the numbers 3.7, 21, and 18.9. Start by creating an object of type Averager.


  Averager avg = new Averager;
  avg.addData( 3.7 );
  avg.addData( 21 );
  avg.addData( 18.9 );
  System.out.println( "The average is " + avg.getAverage() );

Question 9. Suppose that Date is the class shown on the left below. Write a code segment that will declare two variables, today and start of type Date, and that will set up the situation shown in the illustration on the right.

public class Date {
     int month;
     int day;
     int year;


  Date today, start;
  today = new Date();
  start = today;
  today.month = 4;
  today.day = 5;
  today.year = 2017;

Question 10. Both subroutines and classes are examples of "black boxes" (or "modules"), which are important for constructing complex systems. Explain what is meant by black boxes, and how they help to handle complexity. Your answer should include mention of interface and implementation. How does the general idea of black box apply to subroutines and to classes?

Answer. The idea of a black box is that it is something that can be used without understanding what goes on inside the box. A black box has an interface, which defines how the box is used, and it has an implementation, which is what is inside the box. To use a black box, you only need to know its interface. When you are building a black box, you are creating the implementation, and for that you don't need to think about what the black box will be used for -- you just have to make it work correctly internally. This makes it easier to build complex systems, because you can work on smaller components of the system (the black boxes or modules) independently.

A subroutine is a black box in the sense that you don't need to understand the code inside the subroutine in order to use it. You just need to know its interface (including, for example, its name and parameter list.) Similarly, to use a class, you just need to know its public API. You don't need to know anything about the private part of the class or about the code inside its methods.