CS 1401, Quiz 14, based on Test 4

Date: Tuesday, May 1, 2007
Section (9 am or 10:30 am): ____________________________
Name (please type legibly, ideally in block letters): ______________________________________________________________________

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.

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.

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.

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 the code from Problem 3 step-by-step.











































6. Write a method that, given an array s of softwares of type AuthoredSoftware, computes the average length of an open-source software.