CS 1401, Exam #4, Solutions

Date: Thursday April 26, 2007
Section (9 am or 10:30 am): ____________________________
Name (please type legibly, ideally in block letters): ______________________________________________________________________

Since 2001, April 26 is celebrated as the World Intellectual Property Day, the day when everyone is reminded of creativity and of its protection, via copyrights, patents, etc. The topic of the 2007 World Intellectual Property Day is Encouraging Creativity.

1. Define a class Software in which each object has two parameters: the program itself (an object of type String), and a Boolean variable indicating whether the program is proprietary (of limited use) or open-source (unlimited use). Include a constructor method, accessor methods, mutator methods, a method that returns the length of the the software, and an appropriate toString method that tells the user whether the program is proprietary or not, and if the program is not proprietary, it should also return the code itself.

 Solution:

public class Software{
  protected String program;
  protected boolean proprietary;
  public Software(String aprogram, boolean aproprietary)
    {program = aprogram; proprietary = aproprietary;}
  public String getProgram(){return program;}
  public boolean getProprrietary(){return properietary;}
  public void setProgram(String newProgram){program = newProgram;}
  public void setProprietary(boolean newProprietary)
    {proprietary = newProprietary;}
  public int getLength(){return program.length();}
  public String toString(){
    String message;
    if (proprietary)
      {message = "This program is proprietary. "}
    else
      {message = "This is an open source program. " + "\n" + program;}
    return message;
  }
}

2. It is desirable to honor the software authors. Considering the class Software (defined in Problem 1) as a superclass, define a subclass AuthoredSoftware in which each object has an additional field: the name of its author. Override the original toString method so that its printout includes the name of the author.


Solution:

public class AuthoredSoftware extends Software{
  private String name;
  public AuthoredSoftware(String aprogram, boolean aproprietary,
    String aname)
    {super(aprogram, aproprietary); name = aname;}
  public String getName(){return name;}
  public void setName(String newName) {name = newName;}
  public String toString(){
    String message;
    if (proprietary)
      {message = "This program is proprietary. " +
        "\n" + "It was written by " + name;}
    else
      {message = "This is an open source program. " +
        "\n" + "It was written by " + name + ".\n" + program;}
    return message;
  }
}

3. In the main method, define a new object of type AuthoredSoftware (as defined in Problem 2). This object should describe an open-source program written by you; for simplicity, let us assume that the corresponding software is just an empty main method:
public static void main(String[] args){}
Use the corresponding method to compute and print the length of this software.

Solution:

String message = "public static void main(String[] args){}"
AuthoredSoftware mySoftware =
  new AuthoredSoftware(message, false, "Carlos Rubio");
int length = mySoftware.getLength();
System.out.println(length);

4. Trace your code from Problem 3 step-by-step.


Tracing:

first, we assign to the variable message the desired value:

 -------------
|         \  |
 ----------\---
   message  \
            _\|
              --------------------------------------------
             |   public static void main(String[] args){} |
              --------------------------------------------

on the next line, we call a constructor method:

 -----------------
|  -------------  |
| |  public ... | |
|  -------------  |
|    aprogram     |
|  ------------   |
| |    false   |  |
|  ------------   |
|  aproprietary   |
|  ------------   |
| | Carlos ... |  |
|  ------------   |
|    aname        |
|-----------------|
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
 -----------------
 AuthoredSoftware

this method, in its turn, calls a constructor method of the superclass:

 -----------------
|  -------------  |
| |  public ... | |
|  -------------  |
|    aprogram     |
|  ------------   |
| |    false   |  |
|  ------------   |
|  aproprietary   |
|                 |
|-----------------|
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
 -----------------
     Software

after the second line, we get a new object

 -----------------
|  -------------  |
| |  public ... | |
|  -------------  |
|     program     |
|  ------------   |
| |    false   |  |
|  ------------   |
|   proprietary   |
|  ------------   |
| | Carlos ... |  |
|  ------------   |
|     name        |
 -----------------
   mySoftware


after the third line, we get

 ----------
|   40     |
 ----------
   length


5. Modify the length method from Problem 1 as follows: use try/catch block to produce a meaningful error message when the total length is 0.


Solution:

    public class ZeroException extends Throwable{}
 ...

    public int getLength()
    {length = -1;
     try{
       length = program.length();
       if (length == 0)
          throw new ZeroException();
     }
    catch(ZeroException e){
      System.out.println("Warning: there is no program, the string is empty.");
    }
    return length;
  }

6. Write a method that, given an array s of softwares of type AuthoredSoftware, computes the average length of an open-source software. For extra credit: test your method step-by-step on a simple example.


Solution:

public double average(AuthoredSoftware[] s){
  int total = 0;
  for (int i = 0; i < s.length; i++)
    {total += s[i].getLength();}
  return (double) total / (double) s.length;
}