Programming 3

University of Alicante, 2024–2025

First Programming Assignment

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

Introduction to object-oriented programming

Introduction

In this first programming assignment we will work on the concepts introduced in the first theory unit. To do so, we will implement the classes described below.

P1.1 Class Coordinate.

Implement the class Coordinate in the package es.ua.dlsi.prog3.p1 as shown in the attached UML. This class shall represent coordinates within a n-dimensional space. In order for the space to store n dimensions we will use an array of elements as components of the coordinate.

Figure 1. UML class diagram.
Figure 1. UML class diagram of P1.1.

The methods must fulfill the following contracts:

  • Coordinate(double[]): if the array provided is null, a 0-dimensional coordinate shall be created (this.components = new double[0]).
  • getComponents: returns a copy of the components property (defensive copy).
  • getDimensions: returns the number of dimensions corresponding to the size of the array components.
  • equals and hashCode: two coordinates shall be considered equal when they represent exactly the same n-dimensional point.

The following constraints shall be met:

Start your implementation by translating this C++ implementation of class Coordinate to Java. This C++ implementation of Coordinate does not has the hashCode() method, and does not comply with immutability requirements. Read the README-en.txt file tha comes with the C++ implementation for some interesting stuff about the similarities (and differences!) between C++ y Java.

P1.2 Class SummaryStatistics

Implement the class SummaryStatistics in the package es.ua.dlsi.prog3.p1 as shown in the attached UML. The responsibility of this class is to calculate basic statistical values from the list of integers it stores: maximum, minimum, average and number of elements. Integer arithmetic is used to calculate the statistics.

Figure 1. UML class diagram.
Figure 1. UML class diagram of P1.2. The use of ArrayList and the name of the constructor attribute are not usually specified in this way in UML. We have done it this way because this is the first programming assignment and we do not yet have the resources to do it in a standard way.

Each created object will have a different identifier id. To maintain the sequence of unique identifiers we will use the class property NEXT_ID. This property shall be queried by COUNT_INSTANCES() to find out how many instances of the SummaryStatistics class have been created.

The methods must fulfill the following contracts:

  • For each instance to have a unique id, any constructor shall assign NEXT_ID to the id of the object to be created and increment the value of NEXT_ID.
  • Both the constructor from the integer vector and the copy constructor will perform defensive copy, and therefore deep copy. Note that to perform this deep copy it is only necessary to create a new ArrayList and copy the references to the Integer values contained; we can do so because the Integer class is immutable.
  • getAverage: returns the average of the values stored in values. If values is empty it will return null.
  • getMin: returns the minimum value among the values stored in values. If values is empty it will return null.
  • getMax: returns the maximum value among the values stored in values. If values is empty it will return null.
  • add: adds an element to values at any position.
  • getSize: returns the number of elements in values.
  • COUNT_INSTANCES() returns the number of instances of the class created using NEXT_ID as the base value.

You must also comply with the following restrictions:

  • To implement getMin and getMax you must rely on the methods of the Java libraries. Only a conditional if or the ternary operator ?: will be used to check if the array values is empty.
  • In the constructor, the name of the parameter must be values.

Unit tests

We provide unit tests in the following file that partially check the proper behavior of the classes. The tests used in the correction will be more exhaustive. 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.
  • Indicate when boxing or unboxing (implicit or explicit) is performed in the statement where it is used.
  • Explain in the javadoc of the class how you have made the Coordinate class immutable.

You can use a non-javadoc comment when necessary. See the following example:


/**
    This class in a example of....
    To make the class immutable ...
*/
class Example {

    /**
    *    Default constructor.
    */
    public Example() {
        Integer i = 3 ; // boxing
    }

    /**
    * Instance attribute
    * It stores the state of blah, blah...
    */
    private int a;

    /**
    * Class attribute
    * It indicates the position of blah, blah...
    */
    private static int b;

    /**
    *   Instance method
    *   It performs defensive copy of the attribute ...
    *
    *   Description: This method ....
    *   ....
    *   ....
    *    @param i : pass by reference
    *    @param x : pass-by value
    *    @param v : pass by reference
    */
    public void f(Integer i , double x, int v[]) {
        ...
    }

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

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-p1.tgz *

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

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.