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.
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.
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.
Formulas:
Hint:Use a second level of inheritance
-- 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).