CS 2510 - Assignment 3: Class Definitions, Containment, Unions, and Methods


Notes on this (and later) assignments:

This problem set is to reinforce basic data definitions Java. We will only use features that are described in How to Design Classes (i.e., like ProfessorJ), but we will use Eclipse and a specialized Testing Library (Documentation)

Make sure the solution to each problem is labeled correctly (top to bottom) and that your examples class (say Assignment2Examples) includes at least 2 examples for each of the classes you define. You must provide purpose statements for each class and method that you design.


Now for the Real problems...

How to Design Classes (HtDC) Problems:

We suggest that you complete these problems on paper first, then typed in to Eclipse. Working them out before hand will give you practice for the exam.

After you finish them, if you feel like you need more practice, try nearby problems in the book/packet.

:

:

:

:



Other Problems:

Remember create examples and test thoroughly!!

Problem A1: Classes and Containment

A big company is in the process of designing a new television recording device, codenamed the Teli-Vizon Recorder (or TVR). The TVR is required to keep track of when (the date and time) it will be recording, the duration in minutes, and the channel name.
  1. Draw a class-diagram for a class representing Dates.

    Draw a class-diagram for a class representing Times, in 24 hour format (e.g., 5pm == 17:00).

    Combine your two earlier class-diagrams into a single diagram that contains a class representing TV recordings. Don't forget to draw the right arrows.

  2. Create the class definitions to match your diagrams. Don't forget the purpose statement for each class.

Problem A2: Basic Methods

  1. Times...
    1. Write a template for your Time class.
    2. Design a method, toSeconds() in your Time class that returns the time of day in seconds. Don't forget to add examples to your Examples class, and add a test method (e.g., testTime()).
  2. Dates...
    1. Write a template for your Date class.
    2. Design a method, isValid(), in your Date class that returns whether or not the date is valid. A Date is valid if the year is greater than 1900, the month is between 1 and 12, and the day is between 1 and 31 (inclusive).

      Don't worry about different months and leap years
    3. Design a method, sameMonth, that determines whether or not this Date has the same month and year as a given one.
    4. Design a method, difference, that computes the number of days (positive or negative) between this and a given Date.

      Assume that a year is 365 days, and a month is 31 days.
    5. Design a method, before, that determines whether or not this Date occurs before a given one. Remember to test year first, then month, then day.

      Hint: What happens when the year is the same? And the month? ...
  3. Recordings...
    1. Write a template for your Recording class. Remember to expand on the containment arrows in your template: what can you access?
    2. Design a method, length, that returns the length of the Recording in seconds.
    3. Design a method, before, that determines whether or not this recording will occur before the given one.
    4. Design a method, timeDifference, that returns the number of seconds between this and a given Recording, assuming they are on the same day.

      Hint: Try to reuse your earlier methods...

Problem A3: Unions

  1. Definitions
    1. Design an a class hierarchy (i.e., and interface and two classes) to represent a List of Recordings. Make a class diagram (in your Java File) but you do not need to copy your Recording/Date/Time diagram from before.
    2. Define the interface/classes to represent your list (say ILoR, MtLoR, and ConsLoR), don't forget purpose statements.
    3. Write a template for your ConsLoR class that expands the template for it's first field.
  2. Methods
    Remember that methods for unions means more than just one implementation (e.g., interface/header and class implementations).
    1. Design the method, isEmpty, that determines whether or not this List of Recordings is Empty.
    2. Design the method, howMany, that determines how many Recordings are in this List of Recordings.
    3. Design the method, totalDuration, that computes the total duration of Recordings in this List of Recordings.