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
Figure 1:
Class Planet
Figure 2:Class
Test