Programming Assignment #1: Boats and Objects
(due on 04/22 by midnight - submitted from a lab machine)

(Please click the refresh button on your browsers to ensure that you have the latest version of this page).


CODE DOCUMENTATION -- Code for all the programming assignments should be well documented. A working program with no comments will only receive partial credit. Documentation entails writing a description of each function/method, class/structure, and comments throuhghout the code to explain what the program statements do.

Note: While few programming assignments pretend to mimic the "real" world, they may, nevertheless, contain some of the ambiguity that exists outside the classroom. If, for example, an assignment is amenable to differing interpretations, such that more than one algorithm may implement a correct solution to the assignment, it is incumbent upon the programmer to document not only the functionality of the algorithm (and more broadly his/her interpretation of the program requirements), but to articulate clearly the reasoning behind a particular choice of solution.
INTEGRITY OF SCHOLARSHIP -- University rules on integrity of scholarship will be strictly enforced. By taking this course, you implicitly agree to abide by the UCSD Policy on Integrity of Scholarship described beginning on page 62 of the Academic Regulations section (PDF) of the 2002-2003 General Catalog, in particular, "all academic work will be done by the student to whom it is assigned, without unauthorized aid of any kind."

You are expected to do your own work on all assignments; there are no group projects in this course. You may (and are encouraged to) engage in general discussions with your classmates regarding the assignments, but specific details of a solution, including the solution itself, must always be your own work. All exams are closed book; no implements other than your brain and a writing instrument are to be used.

Incidents which violate the University's rules on integrity of scholarship will be taken seriously. In addition to receiving a zero (0) on the assignment/exam in question, students may also face other penalties, up to and including, expulsion from the University. Should you have any doubts about the moral and/or ethical implications of an activity regarding the course, please see the instructors.


ASSIGNMENT -- The overall objective of this assignment is for you to experience the differences and relative merits of imperative languages and object-oriented programming languages. For this assignment you will have to write a program in C, then in Java, and discuss advantages/disadvantages of both approaches. See the bottom of this page for instructions about what to turn in.

Consider a program that simulates boat journeys. The program asks the user to input information, such as boat type and distance to cover (an integral number of miles), and produces a value for the ETA and the estimated consumption of fuel.

Initially, the program must implement two types of boats: motorboats, and sailboats. Motorboats are described by a number of engines. Each engine has a speed that it can deliver (an integral number of mph between 1 and 10), and consumes a certain amount of fuel for every 10 miles covered (an integral number of gallons between 1 and 5). In this assignment we assume that a motorboat can have an arbitrary large (integral) number of engines. A sailboat has between 1 and 3 sails and is also described by the expected speed of the wind (an integral number of mph). Each sail allows the boat to increase its speed by 20% of the wind speed.

Here is a sample execution of the program, with user input in bold:

% boat

Choose the type of boat:
1. Motorboat
2. Sail boat
>1
You have chosen a Motorboat.
How many engines?
>-1
Invalid input: -1
How many engines?
>2
What is the speed of engine #1?
>3
What is the fuel consumption of engine #1?
>2
What is the speed of engine #2?
>100
Invalid input: 100
What is the speed of engine #2?
>5
What is the fuel consumption of engine #2?
>5

Enter the travel distance (in miles):
>10

The speed of the Motor Boat with 2 engines (3/2, 5/5) is 8.0 mph, the ETA is 1.25 hours, and the fuel consumption is 7.0 gallons.

As it is shown in the example above, your program should handle invalid input and keep querying the user until a valid input is given. For simplicity we assume that all input must be strictly positive integers. Also, you see that the last output must summarize the characteristics of the boat. Use common sense for the handling input for the sailboat and trireme case. Also, note that what is above is a vague specification. We will not use an automatic program verifier to grade this assignment.

Formulas:

Of course, this program is far from being an actual boat simulation and has a really simple behavior and somewhat incomplete specification. However, you will see that it is sufficient to demonstrate the contrast between imperative and object-oriented programming.


Question #1: Implement the boat program in imperative C.
Requirements and hints:
  • You must define and use an engine struct
  • You must use a dynamically allocated array of engines to support an arbitrary number of engines
  • You should use the fgets() function to read characters from the keyboard, and the strtol() function to convert the characters into strings. See the manual pages for these two functions (type 'man fgets' and 'man strtol' at the UNIX prompt). Here is an example that uses these two functions.
  • It may be a good idea to write a single function to read integer user input.

  • Question #2: Implement the boat program in Java, with an (abstract) Boat class and 2 derived classes.
    Requirements and hints:
  • You must use an abstract Boat class, from which other boat-related classes are derived.
  • Here is a fragment of code that reads an integer from the keyboard, that you should use as an example.
  • It is probably a good idea to have the constructor of each class do the I/O.
  • It may be a good idea to create a class that is in charge of reading integer user input.

  • Question #3: Discuss the differences between the two implementations. In particular, discuss the structure of the "main" function.
    We now introduce a new type of boat: a Trireme. A Trireme has between 1 and 3 sails, but also has between 10 and 30 pairs of oars to increase speed. As for a sailboat, each sail increases the trireme's speed by 20% of the wind speed. Additionally, each pair of oars increases speed by 0.1 mph.

    Formulas:


    Question #4: Modify your C Boat program to include the Trireme.
    Question #5: Modify your Java Boat program to include the Trireme
    Hint:
  • Use a second level of inheritance

  • Question #6: Discuss the differences when adding Trireme to the C program vs. adding it to the Java program. In particular, explain which version was more difficult to extend and why, and quantify the amount of code reuse in both versions. What if one were to define boats with engines and sails, engines and oars, etc.?

    -- What to turn in --

    Step #1: Creating a tarball


    You should create a tar ball called <username>_cse130_pa1.tar, where <username> is your account username. The tarball should contain the following:

    Step #2: Testing


    Your program must compile and run on an ACS machine. So although you may do development on any system, make sure that everything works on the ACS sytem before proceeding to step #3.

    Step #3: Submitting via e-mail

    Due Date: 4/22, by midnight
    On an ACS machine, at the UNIX prompt, you should run the following command (note that this may appear to be 2 or more lines on your browser but is of course entered on a single line at the UNIX prompt):

    % echo "See attachment" | mutt -a <username>_cse130_pa1.tar -s "CS130 PA#1" cs130s1@ieng9.ucsd.edu cs130s2@ieng9.ucsd.edu

    Remember that <username> should be replaced by your UNIX username. This command sends an email to the TAs Abhishek (cs130s1) and Haixia (cs130s2), with the tar file as attachment. It is your responsibility to ensure that you send the correct attachment. You will receive a confirmation email after the submission deadline expires, if the submission was received successfully (i.e., we are able to untar a correctly named attachment).