Lab 1 - Java Review and Object-Oriented Programming - CSCI-C243

There are three problems.

For this assignment, as in all programs for this course, you should follow the coding standards for the chapters we have covered so far.

Use of a version control system, such as darcs or git, is recommended, not required, for this assignment.

Problem 1

Write and test a program with four static methods for computing the factorial function, n! = 1 × 2 × … × n, for n > 0, and 0! = 1. For example, 5! = 1 × 2 × 3 × 4 × 5 = 120. The argument and result of the function are non-negative integers. Since the result can be very big, use the Java long type (64-bit integer).

The four methods should use:

  1. for loop
  2. while loop
  3. do while loop
  4. Recursion. Hint: n! = n × (n − 1)! if n > 0. The base case should be when n = 0. And what is 0! ? If you don’t remember, reread the first paragraph of this problem.

The main method should ask the user to input n and print four lines showing the results of the four methods calculating n!.

Compile your program and test it. Capture the test I/O in a file. Test cases should include n = 0, n = 10, and two other values where n > 0.

Problem 2

Write and test a Java class for book objects. The Book class should have fields for instance data including the book’s ISBN-13, author (keep it simple by assuming only one author), title, number of pages, and year of publication. Each of these fields should be suitably encapsulated and “information-hidden”: use private declarations, and provide public accessor and mutator methods for each field (also called getter and setter or reader and writer methods). For full credit, the mutator methods for ISBN, number of pages, and year of publication should check for values that are out of reasonable range.

Hints: Don’t bother with hyphenating ISBNs; just treat them as numbers. Modern ISBNs contain decimal 13 digits (ISBN-13); which of the four Java integer data types should you use?

Also define a toString method, which produces a multi-line string that tells us all about the book (i.e., shows us all field values, suitably labeled).

In the main method, create two instances of Book and print them, exercising the toString method. Use the mutator methods to change at least two of the fields, and use the accessor methods and print to confirm the changes. Also show what happens when invalid data are passed to the mutator methods.

Compile and test. Capture your test I/O.

Problem 3

Besides printed books, libraries now have books on CD, as well as other formats. Think about the similarities and differences between printed books and books on CD. Design an inheritance class hierarchy with AbstractBook as the superclass, PrintBook and BookOnCD as subclasses. Move as many fields, constructors, and methods from (Print)Book into AbstractBook as you reasonably can—all of those that apply generally to books of all kinds. Then define what’s left of the PrintBook and BookOnCD classes.

Estimate how many lines of code you saved writing by using inheritance instead of copy and paste.

If you have time, extend your class collection with classes for other types of library items.

What to Turn In

This is an ungraded assignment; there is nothing to turn in.

Scoring

This assignment is ungraded.


Revisions