You are on page 1of 3

Overview of the Design: The class design is done keeping in mind that any type of variables should be swapped

of their value without use of a single class for every data type. So generics have been used to achieve this purpose. Any kind of data type can be used since the compile time casting is automatically done.

Design Benefits: 1. Type safety: Since the compile time type casting is done all the data types are guaranteed to be type safe. 2. One Single Class: No need to have different classes for string or int or float. Design details of Function:

Constructor: Initializes the object to the given value. public void Variable(T _value) { this.value = _value; }

public void setValue(T newvalue): Changes the stored value in the object to the provided new value. public void setValue(T newvalue) { this.value = newvalue; }

public T getValue(): Returns the value of the type stored in the object. public T getValue() { return this.value; }

public void swap(Variable other):


Swaps the value stored in present object to the value stored in another object. public void swap(Variable other) { T tmp = this.value; this.setValue((T)other.getValue()); //System.out.println(this.value); other.setValue(tmp); //System.out.println(other.value); }

Output of Test case:

You might also like