## CSci 2101 Data Structures: Lab 5

### Problem 1

In the class Node of the first version of the linked list implementation add a static counter for the constructor and each of the other three methods. The counter for a method will be incremented every time the method gets called. Add a static method printCounter() to print out all of the counters. Add a call to printCounter() at the end of the program TestLinkedLists.java to find out how many time each method was called. Write the results in comments.

This approach can be used to compare efficiency of an implementation.

### Problem 2: LazyObject

Write a class LazyObject as follows:
• Every LazyObject has an integer private data field.
• The class LazyObject has a static constant (i.e. final variable) called `enough` which is set to 5.
• The class also has a static counter.
• Each object has a void method `flip()` which flips the sign of the data (for instance, if the data is 1, after the flip it will become -1, and the other way around).
• The method works only until the counter is less than `enough`. Otherwise the object prints a message "I am too tired, no more flips", and returns without changing the data.
• The class also has a static method `getRest()`. If this method is called when the counter has reached `enough`, then the counter gets reset to 0 and the message gets printed "I am ready for more flips". If the counter is less than `enough`, then the counter is not changed and the message gets printed "I don't need any rest yet"
• An object has a method `printData()` to print the data.
The following test program uses the class `SampleLazyObject.class` (you can download it):
``````
public class TestLazy {
public static void main(String [] args) {
SampleLazyObject slo1 = new SampleLazyObject(3);
SampleLazyObject slo2 = new SampleLazyObject(2);
slo1.flip();
slo2.flip();
slo1.flip();
SampleLazyObject.getRest();
slo1.flip();
slo2.flip();
slo1.flip();
slo1.getRest();
slo2.flip();
slo1.printData();
slo2.printData();
}
}
``````
You can use it for testing by replacing `SampleLazyObject` by `LazyObject`. The behavior should not change.

A similar technique is used to create objects that can have only one instance (such as Java SecurityManager).

### Problem 3: Passing objects and primitive types to methods.

Consider the following program:
``````
public class LabFiveProbThree {
public static void main(String [] args) {
int n = 1;
int m = 2;
SomeObject so1 = new SomeObject(1);
SomeObject so2 = new SomeObject(2);

tryInt(n,m);
System.out.println("main: n = " + n + " m = " + m);
System.out.println();

tryObjectOne(so1,so2);
System.out.println("main: so1 data = " + so1.getData() +
" so2 data = " + so2.getData());
System.out.println();

tryObjectTwo(so1,so2);
System.out.println("main: so1 data = " + so1.getData() +
" so2 data = " + so2.getData());
System.out.println();
}

public static void tryInt(int x, int y) {
System.out.println("tryInt before: x = " + x + " y = " + y);
// "switching" the values of x and y
int temp = x;
x = y;
y = temp;
System.out.println("tryInt after: x = " + x + " y = " + y);
}

public static void tryObjectOne(SomeObject s1, SomeObject s2) {
System.out.println("tryObjectOne before: s1 = " + s1.getData() +
" s2 = " + s2.getData());
// "switching" the values of s1 and s2
SomeObject temp = s1;
s1 = s2;
s2 = temp;
System.out.println("tryObjectOne after: s1 = " + s1.getData() +
" s2 = " + s2.getData());
}

public static void tryObjectTwo(SomeObject s1, SomeObject s2) {
System.out.println("tryObjectOne before: s1 = " + s1.getData() +
" s2 = " + s2.getData());
// "switching" the values of data fields of s1 and s2
int temp = s1.getData();
s1.setData(s2.getData());
s2.setData(temp);
System.out.println("tryObjectOne after: s1 = " + s1.getData() +
" s2 = " + s2.getData());
}

}
``````
and the class SomeObject:
``````
public class SomeObject {
private int data;

public SomeObject(int d) {
data = d;
}

public void setData(int d) {
data = d;
}

public int getData() {
return data;
}
}

``````
Draw memory diagrams to explain how each of the methods works. What would be printed in main after each of the methods returns? Run the program to check your answers. Correct the diagram if needed.

### Problem 4

In the previous program add a static method in LabFiveProbThree class which takes two objects of class SomeObjects and adds one to the data of each of them. Print out the objects in main to test your method.

### Problem 5

In the same file add a method ```public SomeObject minimum(SomeObject s1, SomeObject s2)``` that takes two SomeObject objects and returns a reference to the one with the smaller data. After the call to the method in main show that the returned reference is to the existing object, and not to the new one (modify it, and print the corresponding SomeObject to see that it got modified).

### Problem 6

In the same file write a method `copyMinimum` which is similar to minimum, but instead of returning one of the existing references, it creates a new SomeObject whose data field has the value of the smaller data of the two. Write the code in main to show that the new object is distinct from the two original ones.

### Problem 7

In a new file with its own main create an array of SomeObjects and print it out. The data values should go in increasing order, starting at 1.

### Problem 8

In the file from the previous problem write a loop to reverse the array. Do not use the setData() method, exchange the references instead. Print out the data values of the resulting array.
This is a lab from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.