0% found this document useful (0 votes)
5 views

Array Worksheet

Uploaded by

ju3b.bb
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Array Worksheet

Uploaded by

ju3b.bb
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SWE5302 Data Structures and Algorithms

Practical Exercises – Arrays

Exercise 1
Create a new Java project (call it Array) and in this project create a new class called Dog which
implements the Comparable interface and has the attributes and methods shown below.

Class name: Dog (Attributes)


private Long chipID A unique number stored on a chip and inserted into
the dog
private String name The dog’s name e.g. Fido

Class name: Dog (Methods)


public Dog(Long chipID, String name) ctor – creates a Dog object using data passed by the
parameters.
public String toString() returns a string e.g. ‘Fido (123456)’
public int compareTo(Object obj) returns -1, 0 or +1 depending on comparison with
the object passed
In addition provide suitable getters and setters for the class attributes

You have probably not used the toString and compareTo methods before so here is the code for
these 2 methods.

public String toString()


{ String s;
s= name + " (" + chipID + ")";
return s;
}

public int compareTo(Object obj) {


Dog d = (Dog) obj;
if(d == null)
return 1;
else
return chipID.compareTo(d.chipID);
}

BEFORE continuing ensure that your code is syntactically correct – if you have any errors or
warnings that you cannot resolve then ask!

The toString and compareTo methods are ‘special’ compared with the others we have created and
this can be seen in the Eclipse IDE.

Page 1 of 4 02/10/24 A. Razak


How does Eclipse indicate they are special?
They are usually annotated with the @Override
Why are they special? They define the object
in the class, providing a string representation
og an object

In your project create a class called Test001 and inside the class enter the following code.

public static void main(String[] args) {


Dog d1 = new Dog(new Long(56498), "Fido");
Dog d2 = new Dog(new Long(34999), "Rover");

System.out.println(d1);
System.out.println(d2);
}

Experiment 1
Run the program and notice what is displayed in the output window? Compare the output to the
Dog class, are there any conclusions that can be made? Not sure, then comment out the toString
method and run the program again any difference?
Experiment 2
Modify the method so that it looks like this
public static void main(String[] args) {
Dog d1 = new Dog(new Long(56498), "Fido");
Dog d2 = new Dog(new Long(34999), "Rover");

System.out.println(d1);
System.out.println(d2);

if(d1.compareTo(d2) == 0)
System.out.println("Same doggy");
else
System.out.println("Different doggy");
}

Change the name of d2 to Fido and run the program again. Do the dogs match?
Experiment 3
Change the name of d2 back to Rover and its chip ID to 56498 and run the program again. Do the
dogs match?
Experiment 4
Change the Dog class so that dogs with the same name match.

Describe in your own words the rules for implementing the compareTo method.
Why is the toString method useful?
Include the code for the compareTo method you created in experiment 4.
What is the difference between Long and long and what similar cases can you find?

Page 2 of 02/10/24 A. Razak


4
Exercise 2
In your project create a class called Test002 and inside this class enter the following code

private static final int NO_DOGS_IN_POUND = 200000;


private static final int NO_REPORTED_LOST = 2000;
private static final int RANGE = NO_DOGS_IN_POUND * 2;
private static final Random rnd = new Random();

private Dog dogPound[] = new Dog [NO_DOGS_IN_POUND];


private Dog reportedLost[] = new Dog [NO_REPORTED_LOST];

public Test002()
{ long id;
for(int i = 0; i < NO_DOGS_IN_POUND; i++)
{ id = Math.abs(rnd.nextInt()) %
RANGE; dogPound[i] = new Dog(id, "");
}
for(int i = 0; i < NO_REPORTED_LOST; i++)
{ id = Math.abs(rnd.nextInt()) %
RANGE; reportedLost[i] = new Dog(id,
"");
}
}

public void pause(long delay){


try{
Thread.sleep(delay);
}
catch (Exception e){
e.printStackTrace();
}
}

// Perform single sequential search


public boolean search(Dog value){
int i = 0;
while(i < NO_DOGS_IN_POUND && dogPound[i].compareTo(value) != 0){ i+
+;
}
return (i < NO_DOGS_IN_POUND);
}

public void performSequentialSearch()


{ long time;
int nbrFound = 0;

System.out.print("#### SEQUENTIAL SEARCH (Data Set Size ");


System.out.println(NO_DOGS_IN_POUND + " ) ###");

time = System.currentTimeMillis();
for(int i = 0; i < NO_REPORTED_LOST ; i++){
if(search(reportedLost[i]))
nbrFound++;
}
time = System.currentTimeMillis() - time;

System.out.print(" Number matched : " + nbrFound);


System.out.println(" out of " + NO_REPORTED_LOST + " searchs");
System.out.println(" Time taken : " + time + " milliseconds\n");
}

Page 3 of 02/10/24 A. Razak


4
public void performBinarySearch(){
long time;
int nbrFound = 0;

System.out.print("#### BINARY SEARCH (Data Set Size ");


System.out.println(NO_DOGS_IN_POUND + " ) ###");

time = System.currentTimeMillis();
for(int i = 0; i < NO_REPORTED_LOST ; i++)
{ if(Arrays.binarySearch(dogPound, reportedLost[i]) >= 0)
nbrFound++;
}
time = System.currentTimeMillis() - time;

System.out.print(" Number matched : " + nbrFound);


System.out.println(" out of " + NO_REPORTED_LOST + " searchs");
System.out.println(" Time taken : " + time + " milliseconds\n");
}

public void execute()


{ long time;

performSequentialSearch();
pause(100);

// Sort the data set array


int nbrFound = 0;
System.out.print("SORTING " + NO_DOGS_IN_POUND + " elements took ");
time = System.currentTimeMillis();
Arrays.sort(dogPound);
time = System.currentTimeMillis() - time;
System.out.println("" + time + " milliseconds\n");
pause(100);

pause(100);
performBinarySearch();
}

public static void main(String[] args) {


Test002 that = new Test002();
that.execute();
}

Once you have entered this program run it and study the output. Comment out the line in yellow and
run the program again study the output.

What conclusions can we draw from these two sets of results?

Page 4 of 02/10/24 A. Razak


4

You might also like