26 January, 2020

Lab 4 - Mutability



Lecturer:

Report Title:

Submit to:

Deadline for submission:


Student Name:

Student Number:

Programme of Study:

Module:










Contents

 



Description

An immutable project can be defined as a program that cannot be changed once a variable has been instantiated. The program contains attributes that makes it impossible to be changed once it has been created (Kjolstad, Dig, Acevedo, & Snir, 2010). The project contains protective measures that make it impossible to add or remove objects once it has been created. Immutable program contains different objects that work together to ensure that the program is not destroyed once it has been created. The project involves testing an existing class known as Planet.java to see whether it has attributes that makes it an immutable program. The test is conducted by reading the code and establishing objects that makes it immutable and also developing a test.java class to test the planet. Java. The test class should be able to use the existing methods in the class planet and establish whether or not the class is immutable.

Aims

       I.            To determine whether the program is immutable
    II.            Develop a strategy that can be used to define immutable objects
 III.            To study and understand the Date object
 IV.            To develop a driver class for the project

Method

The methodology used to analyze the program to determine whether it is immutable is conducted using NetBeans IDE.  
Determining whether the program is immutable
The code below shows the declaration of the code as public final class Planet; this is because no children classes should be created from the immutable class Planet.
public final class Planet
{
}
The fragment of code from the program below shows string name and Date discoveryDate objects. It is an immutable program because the constructor public Planet has parameters.  The parameterized constructor makes it impossible for the project to be changed after it has been created.
public Planet (String name, Date discoveryDate)
{
     this.name = name;
this.discoveryDate = new Date (discoveryDate.getTime ());
  }
The object string name is immutable because once it is created it cannot be changed anywhere in the program.
Both the variables name and discoveryDate have get methods have seen below for each one of them.
public String getName()
  {
    return name;
  }
The public String getName() return name as it is evident in the fragment of code above.


  public Date getDiscoveryDate()
  {
    return new Date(discoveryDate.getTime());
  }
The code above public Date getDiscoveryDate() returns new Date().
The program does not have setter methods to enable change of the variables in the program once it has been created.
The tester code below proves that the class is immutable
// Driver class

import java.util.*;

class Test
{
    public static void main(String args[])
    {
        Date date = new Date();
        Planet p;
        p = new Planet("Testing the Planet Class", date);
        System.out.println(p.getName());
        System.out.println(p.getDiscoveryDate());
 
       
    }
}
The tester code shows that the variables defined in the class planet cannot be changed and it has to be re-used. As it is evident on the code above the class Planet has to be called so as to be able to access the variables name and discoveryDate using the get methods available in class Planet.
Strategy to define immutable objects
The best strategy to define immutable objects is by first opening a program using different java IDE. Once the program has been opened reading and understanding the code is important in understanding the type of program and objects. The objects is plays a critical part in determining the immutable nature of a program. The program should have a constructor that has parameters, there should be a get method for all the variables in the program and finally the program should not have a set method for any of the variables.
Understanding the date variable
The date variable when it comes to immutable objects is complex. This is because the date belongs to data type Date. It is evident in our test class above as we can see in the code below:-
Date date = new Date();
The object date had to be initiated using the function new Date (). The double initiation of date object makes it complex to use. Date object is discouraged from use because it has major flows since it was developed. Such flows includes it lacks time zone, the world contains different time zones. The lack of time zones in the java.util.Date makes it lack necessary features that is important to the users of the systems developed using the object. It also lacks time format that is necessary in ensuring that users of the system understand the time being displayed (Nikolic, 2017). The weakness makes it harder because the software engineer has to design a time format for the program which is time consuming. It also lacks a calendar system. The calendar system has to be developed hence adding work to the individual or team using java util Date for the function of software development. This is not flexible because it rates years based on the year 1900 and it results in problems when using years prior to 1900 (Nikolic, 2017).

Results and Testing

It displayed the string “Testing the Planet Class”, the string was defined with the string posted in the test class. The date displayed was the current time defined on the test class but it was displayed using the function getDiscoveryDate from the Planet class. The same applies to the string name that was from class Planet.

Conclusions

In conclusion, the aims of the study has been met, this because the program has been confirmed to be immutable, the object date has been studying and understood perfectly,  the test class for the main program was developed and finally the strategy to define an object based on immutable program developed. The project is immutable based on (Haack, Poll, Schafer, & Schubert, 2017) definition of an immutable program. The program does not have setter methods that can be used to change the program after it has been instantiated. The program only contains constructors and getter methods that cannot be used to change the program variables once they have been instantiated.

References

 

Haack, C., Poll, E., Schafer, J., & Schubert, A. (2017). Immutable Objects for a Java-like
Language. German research foundation, Retrieved from https://www.cs.ru.nl/E.Poll/papers/esop07.pdf .
Kjolstad, F. B., Dig, D., Acevedo, G., & Snir, M. (2010). Refactoring for Immutability.
Making Classes Immutable, Retrieved from https://core.ac.uk/download/pdf/4824892.pdf .
Nikolic, D. (2017). Still using java.util.Date? Don’t! Programming Hints, Retrieved from
 https://programminghints.com/2017/05/still-using-java-util-date-dont/ .


Appendices

2020-01-25 22_07_41-JavaApplication5 - NetBeans IDE 8.0.2.png

Figure 1: Class Planet
test.png
Figure 2:Class Test


Lab 4 - Mutability


Lab 4 - Mutability

“Immutable objects are simple. They can only be in one state, which is carefully controlled by the constructor. One of the most difficult elements of program design is reasoning about the possible states of complex objects. Reasoning about the state of immutable objects, on the other hand, is trivial.

Immutable objects are also safer. Passing a mutable object to untrusted code, or otherwise publishing it where untrusted code could find it, is dangerous — the untrusted code might modify its state, or, worse, retain a reference to it and modify its state later from another thread. On the other hand, immutable objects cannot be subverted in this manner by malicious or buggy code, so they are safe to share and publish freely without the need to make defensive copies.” 
Brian Goetz, Java Concurrency in Practice

Introduction

An immutable object is one whose externally visible state cannot change after it is instantiated. The String, Integer, and BigDecimal classes in the Java class library are examples of immutable objects -- they represent a single value that cannot change over the lifetime of the object.

Lab Instructions:

Consider the following code:
import java.util.Date;

public final class Planet {

  String name;
  privatefinalDate discoveryDate;

  public Planet (String name, Date discoveryDate) {
     this.name = name;
this.discoveryDate = new Date(discoveryDate.getTime());
  }


  public String getName() {
    return name;
  }


  public Date getDiscoveryDate() {
    return new Date(discoveryDate.getTime());
  }



}




1.      Is this program immutable? If you think it is not, what changes would need to be implemented and why? If you think it is, explain why? To explain you need to create a tester class to run the code and see if you can change any values.
2.      Did you notice anything about the Date object? The Date object is no longer used in the latest versions of Java. Research why this is so and how it relates to Mutable Code.
3.      Using what you have learned about Mutability. Detail a strategy that could be used for defining immutable objects.
Remember to write your conclusions on your work.

Submission and Grading Details

This lab will form part of your complete Lab book, which must be submitted at the end of the module. You must reference your work.  You should follow the layout of a typical lab book, and add extra headings as necessary. Sample layout below:
·         Lab3 Mutability
o   Description
o   Aims
o   Method  
o   Results
§  Section 1 What is Mutability and Immutability
§  Section 2 Immutable Code or Mutable Code?
§  Section 3 Deprecated Object ‘Date’
§  Section 4 A strategy for defining immutable objects
o   Conclusions
o   Appendix
o   References 



Marks Available
Description
5
Aims
5
Method
5
Results/Testing/Evaluation
10
Is code Mutability or Not, explain
15
Deprecated Object ‘Date’
10
Outline a Strategy that could be used
10
Conclusion
40


TOTAL
100