On this page:
Practice Problems
Problem 1: Vehicles
Problem 2: Time

Assignment 4: Abstraction; Constructors

Goals: Learn to abstract common code and use custom constructors.


Be very, very careful with naming! Again, the solution files expect your submissions to be named a certain way, so that they can define their own Examples class with which to test your code. Therefore, whenever the assignment specifies:
  • the names of classes,

  • the names and types of the fields within classes,

  • the names, types and order of the arguments to the constructor,

  • the names, types and order of arguments to methods, or

  • filenames,

...be sure that your submission uses exactly those names.

Make sure you follow the style guidelines that Bottlenose enforces. For now the most important ones are: using spaces instead of tabs, indenting by 2 characters, following the naming conventions (data type names start with a capital letter, names of fields and methods start with a lower case letter), and having spaces before curly braces.

You will submit this assignment by the deadline using the Bottlenose submission system. You may submit as many times as you wish. Be aware of the fact that close to the deadline the Bottlenose system may slow down to handle many submissions - so try to finish early.

There will be a separate submission for each problem - it makes it easier to grade each problem, and to provide you with the feedback for each problem you work on.

This assignment has only one part:

Due Date: Thursday, October 5th, 9:00pm

Practice Problems

Work out these problems on your own. Save them in an electronic portfolio, so you can show them to your instructor, review them before the exam, use them as a reference when working on the homework assignments.

Problem 1: Vehicles

Related files:

In this problem, our data will represent different kinds vehicles. Each vehicle has a name, passenger capacity, per passenger fare, and fuel capacity. Different kinds of vehicles can also contain additional information as shown in the supplied Vehicle.java file.

Note: only the totalRevenue() method is properly implemented. The others are stubs that currently return a dummy value, so the code will compile but not yet work.

Warmup: Download the file and work out the following problems:

Once you have finished these methods and are confident that they work properly, save the work you have done to a separate file. Do not submit the code as written so far. The problems below are the main point of this exercise, and it will be helpful for you to preserve the code written so far as a reference against which to compare your revised code below. Again, submit only the work below.

Problem 2: Time

A Time can be represented as an hour between 0 and 23, minute between 0 and 59, and second between 0 and 59.

Define the Time class with: