CS 1401, Exam #4

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 source code 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.






































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.
























































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.




















































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


























































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.

























































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.