CS 1401, Exam #4, Typical mistakes

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.


Typical mistakes:

* not returning the length

* Misuse of toString.
toString is a string-returning method that is invoked when we use
print or println to print an object. It is always there, but if
we do not override it and use Java's default method, we often get
a cryptic printout.

* boolean is a name of the type, not name of the variable.

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.


Typical mistakes:

* several return statements in one method

* methods which instead of computing the length of this object compute
the length of some other object

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.


Typical mistakes:

* not calling a constructor method at all, or calling it with the
wrong number of parameters;

* wrong calls to a length-returning method

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


Typical mistakes: Tracing does not mean re-phrasing your program in
English. Tracing means drawing boxes for all the variables, all the
objects and methods, and showing how the contents of these variables
change.

Typical mistakes: not showing objects or not showing methods, or
drawing empty boxes without explaining what is in them.

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.


Typical mistake: The computer does not automatically generate a
zero-length exception, there is nothing wrong with strings of length 0.
Thus, you need to throw the corresponding exception.

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.


Typical mistakes:

* assuming that we have array of integers

* ignoring the requirement that we only need the average of open-source
softwares and computing the average of all of them

* not writing a method

* for extra credit: not tracing step-by-step, thus missing errors in
the code