Programming 3

University of Alicante, 2023–2024

Second Programming Assignment

Relative weight of this assignment in the practice grade: 20%

Relations between objects and error management

(Use the Eclipse base project for assignments for this one. Import the tests you can find below as you did in the lab controls.)

Introduction

In this second programming assignment we will work on the concepts introduced in the theory units 2 (relations between objects) and 3 (error management). The assignment consists of modelling the relation between a car (Car), the wheels of the car (Wheel) and the type of tyres (TyreType) mounted on these wheels. To do this we will implement the classes and the relations between them that can be found in the following class diagram and that are described below.

Figure 1. UML class diagram.
Figure 1. UML class diagram.

The classes Car, Wheel and TyreType belong to the package es.ua.dlsi.prog3.p2.model. All methods of these classes shall check that the parameters they receive as arguments are valid, i.e. that they make sense (e.g. in TyreType a negative pressure, an empty description or a minimum pressure greater than the maximum pressure does not make sense). In case any parameter is invalid, they should throw the unchecked exception IllegalArgumentException and include a message describing the error when creating the exception to throw. Refer to the description of each class for more details on the validity of the arguments. Except where noted, you should not check whether arguments that are object references are null.

Important: if the arguments to a method cannot have invalid values (by the application’s own logic), obviously no checking is necessary.

The names of methods and attributes, even those whose visibility is private, must be the same as in the UML diagram.

You will have to do shallow or deep copying in the methods that require it depending on the type of relation between the objects of these classes.

When a method throws more than one type of exception, in case two or more errors occur simultaneosly, your code must throw the exception that appears first in the method description in this text.

Class TyreType

The responsibility of this class is to store the attributes of the different types of tyres that may exist. It is an immutable class. The methods of this class must comply with the following contracts:

  • The constructor and copy constructor initialise the attributes of the class to the values received as arguments. The overloaded constructor must throw IllegalArgumentException when it receives a negative pressure, an empty or null description, or when the minimum pressure is greater than the maximum pressure.
  • The toString() method returns a string describing the tyre type. This string shall contain the description and the maximum and minimum pressures. The format of this string is illustrated in the following example, where “185/65 R16” is the description and “1.5” and “3.5” are the minimum and maximum pressures, respectively:
      TyreType 185/65 R16 [1.5,3.5].
  • The methods getMinPressure() and getMaxPressure() return the minimum and maximum pressure values, respectively.
  • The equals(·) method returns true if and only if the description and the minimum and maximum pressures are equal.

Class Wheel

This class represents the wheel of a vehicle to which a tyre type (TyreType) can be assigned and whose state is determined by the type of tyre assigned to it and the pressure to which the wheel has been inflated.

The methods of this class must comply with the following contracts:

  • The constructors and the copy constructor of the class initialise their attributes from the parameters they receive. The initial pressure of the wheel (pressure) shall be zero, except in the copy constructor which shall be that of the wheel received as an argument.
  • setTypeTyre(·) and getTypeTyre() are the getter and setter to access and modify the tyre type assigned (TyreType) to the wheel.
  • inflate(double) updates the pressure value of the wheel (pressure). This method shall throw the following exceptions:
    • IllegalArgumentException if the pressure received as an argument is less than zero.
    • NoTyreTypeException if the wheel is not assigned a tyre type (TyreType).
    • PressureWheelException if the pressure received as an argument is outside the minimum and maximum limits for the assigned tyre type.

Class Car

This class represents a vehicle to which wheels (Wheel) can be added up to the maximum indicated in the relation.

The methods of this class must comply with the following contracts:

  • The addWheel(Wheel) method shall add the wheel (Wheel) received as an argument to the car (Car) after checking that the number of wheels does not exceed the maximum allowed and that it is equal to the wheels that might already be present. This method shall throw the following exceptions:
    • TooManyWheelsException if it is not possible to add the new wheel because it would exceed the amount of maximum wheels allowed.
    • WrongTyreTypeException if the tyre type (TyreType) of the new wheel is not equal to that of the existing wheels. If the existing wheels are not assigned a tyre type (TyreType), the new wheel shall not have a tyre type assigned.
  • getWheels() returns all wheels on the vehicle or an empty ArrayList<Wheel> if the car has no wheels installed yet.
  • changeTyres(TyreType,double) changes the tyres of all wheels using the tyre type (TyreType) and pressure (pressure) received as argument. This method shall throw the following exceptions:
    • IllegalArgumentException if the TyreType type parameter is null.
    • PressureWheelException if the pressure received as argument is invalid. This checking shall not be done in this method: the exception thrown by Wheel.inflate(·) shall be caught, a console error message shall be produced and the exception shall be re-thrown.
    • RuntimeException in case of any other error situation.

Exceptions

All exceptions indicated in the UML diagram are checked exceptions and all of them shall belong to the es.ua.dlsi.prog3.p2.exceptions package. Their meaning should be clear from this assignment description.

Class PressureWheelException

You must implement the methods declared in the UML diagram above. These methods must fulfil the following contracts:

  • The constructor stores the pressure received as an argument in the private attribute pressure.
  • getMessage() returns a string of the type “Pressure of 3.5 BAR” where 3.5 will be the specific value of the private attribute pressure.

Unit tests

We provide unit tests in the following file that check the proper behaviour of the classes. It is important that you understand what is tested and how it is done.

Documentation

This section will not be done in the control.

You must include in the source files all necessary comments in javadoc format. These comments must be defined at least for:  

  • Files: you must include name and id number (DNI, NIE, etc.) of the authors using the annotation @author.
  • Classes: purpose of the class: at least 3 lines.
  • Operations/methods: 1 line for trivial functions, and a minimum of 2 lines, input parameters, output parameters and dependent functions for more complex operations.
  • Attributes: purpose of each attribute: at least 1 line.  

Also document the following aspects:

  • Indicate whether attributes and methods are instance or class (except constructors).
  • For each constructor you define, indicate whether it is a default constructor, an overloaded constructor or a copy constructor.
  • Indicate whether the copy constructor performs shallow or deep copy.
  • Indicate that you are using defensive copy wherever you do so.

You can use a non-javadoc comment when necessary.

It is not necessary to generate in HTML the javadoc documentation.

Minimal requirements for grading your assignment

  • Your program must run with no errors.
  • Unless otherwise stated, your program must not emit any kind of message or text through the standard output or standard error streams. Also avoid error output messages.
  • The format of the name of all properties (public, protected and private) of classes must be strictly respected, both in terms of visibility scope and in terms of their name. Make sure that you respect the distinction between class and instance attributes, as well as the uppercase and lowercase letters in the identifiers.
  • Your code must be conveniently documented and significant content has to be obtained after running the javadoc tool.

Submission of the assignment

The practice is delivered on the DLSI practice server.

You must upload a compressed file with your source code (only .java files). In a terminal, place yourself in the ‘src’ directory of your Eclipse project and enter the command

tar czvf prog3-p2.tgz *

Upload the file prog3-p2.tgz to the practice server. Follow the instructions on the page to log in and upload your work.

This delivery is only used to evaluate the documentation and to obtain the result of the oracle.

Grading 

Testing of your assignment will be done automatically. This means that your program must strictly conform to the input and output formats given in this document, as well as to the public interfaces of all the classes: do not change the method signatures (name of the method, number, type and order of arguments, and data type returned) or their behaviour. So, for example, the Clase(int,int) method must have exactly two arguments of type int.

You can find more information about the grading of programming assignments in the subject description sheet.

In addition to the automatic grading, a plagiarism detection application will be used. The applicable regulations of the University of Alicante Polytechnic School in the event of plagiarism are indicated below:

“Theoretical/practical work must be original. The detection of copy or plagiarism will suppose the qualification of”0” in the corresponding assignment. The corresponding Department and the University of Alicante Polytechnic School will be informed about the incident. Repeated conduct in this or any other subject will result in notification of the offences committed to the pertinent vice canchellor’s office so that they study the case and punish it in accordance with the legislation in force”.

Clarifications

  • Although not recommended, you can add private attributes and methods to the classes. Notice, however, that you must implement all the methods indicated in this document and make sure that they work as expected, even if they are never called in your implementation. 
  • Any additional remark will be published in this page. It is recommended that you use this page as the primary source of the instructions.