Finish lab 10: Lists

This commit is contained in:
2022-03-02 11:15:53 +01:00
parent b2ddd0b8f9
commit 64a05245d6
31 changed files with 966 additions and 5 deletions

View File

@ -1,5 +0,0 @@
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");
}
}

Binary file not shown.

32
src/lab10_lists/App.java Normal file
View File

@ -0,0 +1,32 @@
package lab10_lists;
import java.util.Vector;
public class App {
public static void main(String[] args) throws Exception {
// 1
Vector<String> v = new Vector<>();
v.add("Louis");
v.add("Paul");
v.add("Henri");
v.add("Pascal");
// 2
System.out.println("2: " + v.toString());
// 3
System.out.print("3: ");
if(v.contains("Paul")){
System.out.println("Paul is on this list");
} else {
System.out.println("Paul is not on this list");
}
// 4
v.remove("Paul");
// 5
System.out.println("5: " + v.toString());
}
}

View File

@ -0,0 +1,179 @@
package lab10_lists;
public class LinkedList {
// Task 2
public Node head;
public Node tail;
public LinkedList(){
this.head = null;
}
public void addToStart(String s){
Node n = new Node(s, head);
head = n;
}
public int getSize(){
Node foo = head;
int n = 0;
while (foo != null) {
n++;
foo = foo.next;
}
return n;
}
public String toString(){
Node foo;
if( this.head == null){
foo = new Node(null, null);
} else {
foo = new Node(head);
}
String s = "";
s += "List content (size " + this.getSize() + ") : ";
for (int i = 0; i <= this.getSize(); i++) {
if (i>0) {
s += " -> ";
}
if(foo == null){
s += "null";
} else {
s += foo.item;
foo = foo.next;
}
}
return s;
}
// Task 3
public void removeFirstElement(){
if(head == null) return;
Node foo = new Node(head);
this.head = foo.next;
}
public Node getLastElement(){
if(head == null) return null;
Node tailNode = head;
while (tailNode.next != null) {
tailNode = tailNode.next;
}
return tailNode;
}
public void addToEnd(String element){
Node newNode = new Node(element);
if(head == null) {
head = newNode;
tail = newNode;
}
else {
tail.next = newNode;
tail = newNode;
}
}
public boolean isPresent(String e){
if(head == null) return false;
boolean present = false;
Node foo = new Node(head);
while (foo != null) {
if (foo.item.equals(e)) {present = true;}
foo = foo.next;
}
return present;
}
// Task 4
public Node findElement(String s){
if(head == null) return null;
Node tailNode = head;
while (tailNode != null) {
if(tailNode.item.equals(s)) return tailNode;
tailNode = tailNode.next;
}
return null;
}
public void swapElements(String e1, String e2){
Node n1 = findElement(e1);
Node n2 = findElement(e2);
if(n1!=null && n2!=null){
n1.item = e2;
n2.item = e1;
}
}
public void removeLastElement(){
if (head == null) return;
if (head.next == null) {
head = null;
return;
}
Node n = head;
while (n.next.next != null) {
n = n.next;
}
n.next = null;
}
public void removeElement(String e){
Node n = head;
Node prev = null;
if (n != null && n.item.equals(e)) {
head = n.next; // Changed head
return;
}
while (n != null && !n.item.equals(e)) {
prev = n;
n = n.next;
}
if (n == null) return;
prev.next = n.next;
}
public void insertAfter(String before, String after){
if(head == null) return;
if(before == null) return;
if(after == null) return;
Node prevNode = findElement(before);
if(prevNode == null) return;
Node newNode = new Node(after, prevNode.next);
prevNode.next = newNode;
}
public static void main(String[] args) {
LinkedList flightList = new LinkedList();
System.out.println(flightList);
flightList.addToEnd("Bonn");
System.out.println(flightList);
flightList.addToEnd("Zurich");
System.out.println(flightList);
flightList.addToEnd("Rome");
System.out.println(flightList);
System.out.println(flightList);
flightList.removeFirstElement();
System.out.println(flightList);
flightList.addToEnd("London");
System.out.println(flightList);
flightList.swapElements("Rome", "London");
System.out.println(flightList);
flightList.removeLastElement();
System.out.println(flightList);
flightList.removeLastElement();
System.out.println(flightList);
flightList.removeLastElement();
System.out.println(flightList);
flightList.removeLastElement();
System.out.println(flightList);
}
}

View File

@ -0,0 +1,312 @@
package lab10_lists;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
/**
* JUnit 5 regression tests for LinkedList laboratory
* @author Marc Pignat
* @author P.-A. Mudry
*/
@DisplayName("In this lab, a linked list that you have implemented")
class LinkedListTest {
@Nested
@DisplayName("Has some basic operations that must work - Task 2")
class Task2_Basic_Operations {
@DisplayName("The constructor must create an instance correctly")
@Test
void test_constructor() {
LinkedList n = new LinkedList();
assertEquals(null, n.head, "head should be null after calling the constructor");
}
@DisplayName("The size of the list should be reported correctly")
@Test
void test_getSize() {
LinkedList n = new LinkedList();
assertEquals(0, n.getSize(), "list is empty, getSize() MUST return 0");
n.addToStart("a");
assertEquals(1, n.getSize(), "list of 1 getSize() MUST return 1");
n.addToStart("b");
assertEquals(2, n.getSize(), "list of 2 getSize() MUST return 2");
}
@DisplayName("The toString method should be implemented")
@Test
void test_toStringImplemented() {
LinkedList n = new LinkedList();
String defaultToString = n.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(n));
assertNotEquals(n.toString(), defaultToString, "toString() MUST be implemented");
}
@DisplayName("The toString method should report the list elements correctly")
@Test
void test_toString() {
LinkedList n = new LinkedList();
assertTrue(n.toString().contains("0"), "toString is expected to show the list size (0)");
n.addToStart("Roma");
assertTrue(n.toString().contains("Roma"), "toString is expected to show the element (here 'Roma')");
assertTrue(n.toString().contains("1"), "toString is expected to show the list size (1)");
n.addToStart("Geneva");
assertTrue(n.toString().contains("Roma"), "toString is expected to show the element (here 'Roma')");
assertTrue(n.toString().contains("Geneva"), "toString is expected to show the element (here 'Geneva')");
assertTrue(n.toString().indexOf("Geneva") < n.toString().indexOf("Roma"),
"toString is expected to show 'Geneva' before 'Roma'");
assertTrue(n.toString().contains("2"), "toString is expected to show the list size (2)");
}
}
@Nested
@DisplayName("Has a correct implementation of insertion and removal operators - Task 3")
class Task3_Operations {
@DisplayName("Remove first element works")
@Test
void testRemoveFirstElement() {
LinkedList n = new LinkedList();
n.addToStart("Cathy");
n.addToStart("Bob");
n.addToStart("Alice");
n.removeFirstElement();
assertEquals(2, n.getSize(), "size MUST be 2 after removing an element from a 3 element list");
assertEquals("Bob", n.head.item, "first element must be 'Bob'");
n.removeFirstElement();
assertEquals(1, n.getSize(), "size MUST be 1 after removing an element from a 2 element list");
assertEquals("Cathy", n.head.item, "first element must be 'Cathy'");
n.removeFirstElement();
assertEquals(0, n.getSize(), "size MUST be 0 after removing an element from a 1 element list");
assertEquals(null, n.head, "Head must be null");
n.removeFirstElement();
assertEquals(0, n.getSize(), "size MUST be 0 after removing an element from a 0 element list");
assertEquals(null, n.head, "Head must be null");
}
@DisplayName("Getting the last element works")
@Test
void testGetLastElement1() {
LinkedList n = new LinkedList();
assertEquals(null, n.getLastElement(), "last element must be null");
n.addToStart("Cathy");
n.addToStart("Bob");
n.addToStart("Alice");
assertEquals("Cathy", n.getLastElement().item, "last element must be 'Cathy'");
assertEquals(3, n.getSize(), "size MUST be 3 (getLastElement MUST not remove the element)");
}
@DisplayName("Adding an element at the end works")
@Test
void testAddToEnd() {
LinkedList n = new LinkedList();
n.addToEnd("Alice");
assertEquals("Alice", n.getLastElement().item, "last element must be 'Alice'");
n.addToEnd("Bob");
assertEquals("Bob", n.getLastElement().item, "last element must be 'Bob'");
n.addToEnd("Cathy");
assertEquals("Cathy", n.getLastElement().item, "last element must be 'Cathy'");
}
@DisplayName("Must be able to find elements correctly")
@Test
void testIsPresent() {
LinkedList n = new LinkedList();
assertFalse(n.isPresent("Alice"), "'Alice' MUST not be in the empty list");
assertFalse(n.isPresent("Bob"), "'Bob' MUST not be in the empty list");
assertFalse(n.isPresent("Cathy"), "'Cathy' MUST not be in the empty list");
n.addToEnd("Bob");
assertFalse(n.isPresent("Alice"), "'Alice' MUST not be in this list");
assertTrue(n.isPresent("Bob"), "'Bob' MUST be in this list");
assertFalse(n.isPresent("Cathy"), "'Cathy' MUST not be in this list");
n.addToEnd("Cathy");
assertFalse(n.isPresent("Alice"), "'Alice' MUST not be in this list");
assertTrue(n.isPresent("Bob"), "'Bob' MUST be in this list");
assertTrue(n.isPresent("Cathy"), "'Cathy' MUST be in this list");
n.addToStart("Alice");
assertTrue(n.isPresent("Alice"), "'Alice' MUST be in this list");
assertTrue(n.isPresent("Bob"), "'Bob' MUST be in this list");
assertTrue(n.isPresent("Cathy"), "'Cathy' MUST be in this list");
n.removeFirstElement();
assertFalse(n.isPresent("Alice"), "'Alice' MUST not be in this list");
assertTrue(n.isPresent("Bob"), "'Bob' MUST be in this list");
assertTrue(n.isPresent("Cathy"), "'Cathy' MUST be in this list");
n.removeFirstElement();
assertFalse(n.isPresent("Alice"), "'Alice' MUST not be in this list");
assertFalse(n.isPresent("Bob"), "'Bob' MUST not be in this list");
assertTrue(n.isPresent("Cathy"), "'Cathy' MUST be in this list");
n.removeFirstElement();
assertFalse(n.isPresent("Alice"), "'Alice' MUST not be in the empty list");
assertFalse(n.isPresent("Bob"), "'Bob' MUST not be in the empty list");
assertFalse(n.isPresent("Cathy"), "'Cathy' MUST not be in the empty list");
}
}
@Nested
@DisplayName("Has a correct implementation complex operators - Task 4")
class Task4_Operations {
@Test
@DisplayName("Can find elements")
void testFindElement() {
LinkedList n = new LinkedList();
assertEquals(null, n.findElement("Alice"), "'Alice' MUST not be in empty list");
n.addToEnd("Alice");
assertEquals(n.head, n.findElement("Alice"), "'Alice' MUST be the head element");
assertEquals(n.getLastElement(), n.findElement("Alice"), "'Alice' MUST be the end element");
n.addToEnd("Bob");
assertEquals(n.getLastElement(), n.findElement("Bob"), "'Bob' MUST be the end element");
assertNotEquals(null, n.findElement("Alice"), "'Alice' MUST be in the list");
n.addToEnd("Cathy");
assertNotEquals(null, n.findElement("Alice"), "'Alice' MUST be in the list");
assertNotEquals(null, n.findElement("Bob"), "'Bob' MUST be in the list");
assertEquals(n.getLastElement(), n.findElement("Cathy"), "'Cathy' MUST be the end element");
}
@Test
@DisplayName("Can swap elements")
void testSwapElements() {
LinkedList n = new LinkedList();
n.addToEnd("Cathy");
n.addToEnd("Bob");
n.addToEnd("Alice");
n.swapElements("Alice", "Bob");
assertEquals("Bob", n.getLastElement().item, "'Bob' MUST be the last element");
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements("Cathy", "Alice");
assertEquals(n.head, n.findElement("Alice"), "'Alice' MUST be the head element");
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements("Cathy", "Bob");
assertEquals("Cathy", n.getLastElement().item, "'Cathy' MUST be the last element");
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements("Alice", "Dan");
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements("Dan", "Dan");
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements("Bob", "Dan");
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements("Dan", "Bob");
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements("Alice", null);
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements("Bob", null);
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements("Cathy", null);
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements("Dan", null);
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements(null, "Alice");
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements(null, "Bob");
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements(null, "Cathy");
assertEquals(3, n.getSize(), "size MUST not have changed after swap");
n.swapElements(null, "Dan");
n = new LinkedList();
n.swapElements("Alice", "Bob");
assertEquals(0, n.getSize(), "size MUST not have changed after swap");
}
@Test
void testRemoveLastElement() {
LinkedList n = new LinkedList();
n.addToEnd("Alice");
n.addToEnd("Bob");
n.addToEnd("Cathy");
n.removeLastElement();
assertEquals(2, n.getSize(), "size MUST be 2");
assertEquals(null, n.findElement("Cathy"), "'Cathy' MUST not be in this list");
assertNotEquals(null, n.findElement("Alice"), "'Alice' MUST be in this list");
assertNotEquals(null, n.findElement("Bob"), "'Bob' MUST be in this list");
n.removeLastElement();
assertEquals(1, n.getSize(), "size MUST be 1");
assertEquals(null, n.findElement("Bob"), "'Bob' MUST not be in this list");
assertNotEquals(null, n.findElement("Alice"), "'Alice' MUST be in this list");
n.removeLastElement();
assertEquals(0, n.getSize(), "size MUST be 0");
assertEquals(null, n.findElement("Alice"), "'Alice' MUST not be in this list");
n.removeLastElement();
assertEquals(0, n.getSize(), "size MUST be 0");
n = new LinkedList();
n.removeLastElement();
assertEquals(0, n.getSize(), "size MUST be 0");
}
@Test
@DisplayName("Can remove elements")
void testRemoveElement() {
LinkedList n = new LinkedList();
n.addToEnd("Alice");
n.addToEnd("Bob");
n.addToEnd("Cathy");
n.removeElement("Alice");
assertEquals(2, n.getSize(), "size MUST be 2");
assertEquals(n.head, n.findElement("Bob"), "'Bob' MUST be head");
assertEquals(null, n.findElement("Alice"), "'Alice' MUST not be in this list");
assertEquals("Cathy", n.getLastElement().item, "'Cathy' MUST be at the end");
n.addToStart("Alice");
n.removeElement("Bob");
assertEquals(2, n.getSize(), "size MUST be 2");
assertEquals(null, n.findElement("Bob"), "'Bob' MUST not be in this list");
assertEquals("Cathy", n.getLastElement().item, "'Cathy' MUST be at the end");
n = new LinkedList();
n.addToEnd("Alice");
n.addToEnd("Bob");
n.addToEnd("Cathy");
n.removeElement("Cathy");
assertEquals(2, n.getSize(), "size MUST be 2");
assertEquals(null, n.findElement("Cathy"), "'Bob' MUST not be in this list");
assertEquals("Bob", n.getLastElement().item, "'Bob' MUST be at the end");
n.removeElement("Dan");
n = new LinkedList();
n.removeElement("Dan");
n.addToEnd("Alice");
n.removeElement("Alice");
assertEquals(0, n.getSize(), "size MUST be 0");
assertEquals(null, n.findElement("Alice"), "'Alice' MUST not be in this list");
n.removeElement("Alice");
assertEquals(0, n.getSize(), "size MUST be 0");
assertEquals(null, n.findElement("Alice"), "'Alice' MUST not be in this list");
}
@Test
@DisplayName("Can insert an element after another")
void testInsertAfter() {
LinkedList n = new LinkedList();
n.addToEnd("Alice");
n.insertAfter("Alice", "Cathy");
assertEquals(2, n.getSize(), "size MUST be 2");
assertEquals(n.head, n.findElement("Alice"), "'Alice' MUST be head");
assertEquals("Cathy", n.getLastElement().item, "'Cathy' MUST be at the end");
n.insertAfter("Alice", "Bob");
assertEquals(3, n.getSize(), "size MUST be 3");
assertEquals("Cathy", n.getLastElement().item, "'Cathy' MUST be at the end");
n.insertAfter("Cathy", "Dan");
assertEquals(4, n.getSize(), "size MUST be 4");
assertEquals("Dan", n.getLastElement().item, "'Dan' MUST be at the end");
n.insertAfter("Erin", "Frank");
assertEquals(4, n.getSize(), "size MUST be 4");
assertEquals("Dan", n.getLastElement().item, "'Dan' MUST be at the end");
assertFalse(n.isPresent("Erin"), "'Erin' MUST not be in the list");
assertFalse(n.isPresent("Frank"), "'Frank' MUST not be in the list");
n.insertAfter("Frank", null);
assertEquals(4, n.getSize(), "size MUST be 4");
assertEquals("Dan", n.getLastElement().item, "'Dan' MUST be at the end");
assertFalse(n.isPresent("Frank"), "'Frank' MUST not be in the list");
n.insertAfter(null, "Frank");
assertEquals(4, n.getSize(), "size MUST be 4");
assertEquals("Dan", n.getLastElement().item, "'Dan' MUST be at the end");
assertFalse(n.isPresent("Frank"), "'Frank' MUST not be in the list");
}
}
}

19
src/lab10_lists/Node.java Normal file
View File

@ -0,0 +1,19 @@
package lab10_lists;
public class Node {
String item;
Node next;
Node(String item, Node next){
this.item = item;
this.next = next;
}
Node(Node node){
this.item = node.item;
this.next = node.next;
}
Node(String item){
this.item = item;
this.next = null;
}
}

Binary file not shown.

View File

@ -0,0 +1,158 @@
package lab11_debugging;
/**
* Class used to compare resources consumption between different data structures
*
* @author Pierre Roduit (pierre.roduit@hevs.ch)
* @author Pierre-Andr<64> Mudry (pandre.mudry@hevs.ch)
* @version 1.2
*
*/
public class DynamicStructuresComparison {
private SampleArray tstArray;
private SampleVector tstVector;
private SampleLinkedList tstList;
private int size;
/**
* Constructor. Produce the three data structures
*
* @param inputArray
*/
public DynamicStructuresComparison(int[] inputArray) {
tstArray = new SampleArray(inputArray);
tstVector = new SampleVector(inputArray);
tstList = new SampleLinkedList(inputArray);
// Define the position of insertion for the tests
size = inputArray.length;
}
/**
* Compare the difference between the data structures for the insertions
*
* @param nTimes
* Number of times the test is repeated
*/
public void testInsertion(int nTimes, int position) {
long start, end;
System.out.println("Insertion test with " + nTimes
+ " insertions and an array size of " + size + " elements.");
// Measure insertion with array
start = System.nanoTime();
this.repeatInsertArray(nTimes, position);
end = System.nanoTime();
System.out.println("Array insertion took : \t" + ((end- start)/1000.0) + " uS");
// Measure insertion with vector
start = System.nanoTime();
this.repeatInsertVector(nTimes, position);
end = System.nanoTime();
System.out.println("Vector insertion took: \t" + ((end - start)/1000.0) + " uS");
// Measure insertion with list
start = System.nanoTime();
this.repeatInsertList(nTimes, position);
end = System.nanoTime();
System.out.println("List insertion took : \t" + ((end - start)/1000.0) + " uS");
}
/**
* Compare the difference between the data structures for access
*
* @param nTimes
* Number of times the test is repeated
*/
public void testAccess(int nTimes, int position) {
long start, end;
System.out.println("Access test of the whole data with " + nTimes
+ " repetitions and an array size of " + size + " elements.");
// Measure insertion with array
start = System.nanoTime();
this.repeatAccessArray(nTimes, position);
end = System.nanoTime();
System.out.println("Array access took : \t\t" + ((end- start)/1000) + " uS");
// Measure insertion with list
start = System.nanoTime();
this.repeatAccessVector(nTimes, position);
end = System.nanoTime();
System.out.println("Vector access took : \t\t" + ((end- start)/1000) + " uS");
// Measure insertion with list
start = System.nanoTime();
this.repeatAccessList(nTimes, position);
end = System.nanoTime();
System.out.println("List access took : \t\t" + ((end- start)/1000) + " uS");
}
/**
* Make a specific number of insertions
*
* @param number
*/
private void repeatInsertArray(int repetitions, int position) {
for (int i = 0; i < repetitions; i++) {
tstArray.insertZero(position);
}
}
/**
* Make a specific number of insertions
*
* @param number
*/
private void repeatInsertVector(int repetitions, int position) {
for (int i = 0; i < repetitions; i++) {
tstVector.insertZero(position);
}
}
/**
* Make a specific number of insertions
*
* @param number
*/
private void repeatInsertList(int repetitions, int position) {
for (int i = 0; i < repetitions; i++) {
tstList.insertZero(position);
}
}
/**
* Make a specific number of access to the whole data
*
* @param number
*/
private void repeatAccessArray(int repetitions, int position){
for (int i = 0; i < repetitions; i++) {
tstArray.increment(position);
}
}
/**
* Make a specific number of access to the whole data
*
* @param number
*/
private void repeatAccessVector(int repetitions, int position){
for (int i = 0; i < repetitions; i++) {
tstVector.increment(position);
}
}
/**
* Make a specific number of access to the whole data
*
* @param number
*/
private void repeatAccessList(int repetitions, int position){
for (int i = 0; i < repetitions; i++) {
tstList.increment(position);
}
}
}

View File

@ -0,0 +1,47 @@
package lab11_debugging;
import java.util.Random;
/**
* Class to run the test
*
* @author Pierre Roduit (pierre.roduit@hevs.ch)
* @author Pierre-Andr<64> Mudry (pandre.mudry@hevs.ch)
* @version 1.2
*
*/
public class Launcher {
static final boolean TEST_ACCESS = false;
static final boolean TEST_INSERTION = true;
public static void main(String[] args) {
// Create a random array and create its perfect equivalent with a Vector or a list
final int size = 500000;
final int position = 0;
final int nTimes = 1000;
int[] randomArray = createRandomArray(size, 10);
DynamicStructuresComparison test = new DynamicStructuresComparison(randomArray);
if(TEST_ACCESS)
test.testAccess(nTimes, position);
if(TEST_INSERTION)
test.testInsertion(nTimes, position);
}
/**
* Creates a random array
*
* @param size
* @param maxValue
* @return
*/
static int[] createRandomArray(int size, int maxValue) {
int[] result = new int[size];
Random randomizer = new Random();
for (int i = 0; i < size; i++) {
result[i] = randomizer.nextInt(maxValue);
}
return result;
}
}

View File

@ -0,0 +1,66 @@
package lab11_debugging;
import java.util.Random;
/**
* Class to try the debugger
*
* @author Pierre Roduit (pierre.roduit@hevs.ch)
* @author PA Mudry (pierre-andre.mudry@hevs.ch)
* @version 1.1
*
*/
public class NumberAnalyzer {
public static void main(String[] args) {
int[] array = createRandomArray(100, 100);
displayArray(array);
int position = firstRepeatedNumber(array);
System.out.println("The first repeated element is at location " + position);
}
/**
* Function search for the first element repeated in the vector. It returns
* the position of this duplicated element.
*
* @param inputArray The input array
* @return The position of the first repeated element
*/
public static int firstRepeatedNumber(int[] inputArray) {
for (int i = 0; i < inputArray.length; i++) {
if (inputArray[i] == inputArray[i + 1])
return i;
}
return -1;
}
/**
* Create a random array
*
* @param size
* Number of element of the array produced
* @param maxValue
* Maximal value of the generated elements
* @return The generated array
*/
static int[] createRandomArray(int size, int maxValue) {
int[] result = new int[size];
Random randomizer = new Random();
for (int i = 0; i < size; i++) {
result[i] = randomizer.nextInt(maxValue);
}
return result;
}
/**
* Display the array
*
* @param array
* The array to display
*/
static void displayArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + ",");
}
System.out.println();
}
}

View File

@ -0,0 +1,54 @@
package lab11_debugging;
/**
* Class to test efficiency of arrays
*
* @author Pierre Roduit (pierre.roduit@hevs.ch)
* @author Pierre-André Mudry
* @version 1.2
*
*/
public class SampleArray {
/**
* Stored values
*/
int[] data;
/**
* Constructor
*/
SampleArray(int[] inputArray) {
data = inputArray.clone();
}
/**
* Increment an element by one
*/
void increment(int position) {
if(position < data.length)
data[position]++;
}
/**
* Insert a zero at the specified position
*
* @param position
* @return
*/
void insertZero(int position) {
int[] tmpArray;
tmpArray = new int[data.length + 1];
// Copy the elements till position
for (int i = 0; i < position; i++) {
tmpArray[i] = data[i];
}
// Move all the other element one position on the right
for (int i = position; i < data.length; i++) {
tmpArray[i + 1] = data[i];
}
// Insert a zero at position
tmpArray[position] = 0;
data = tmpArray;
}
}

View File

@ -0,0 +1,48 @@
package lab11_debugging;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* Class to test efficiency of Linked Lists
*
* @author Pierre Roduit (pierre.roduit@hevs.ch)
* @author Pierre-Andr<64> Mudry (pandre.mudry@hevs.ch)
* @version 1.2 (mui) - Changed incrementAll with iterator
*
*/
public class SampleLinkedList {
/**
* Stored values
*/
LinkedList<Integer> data;
/**
* Constructor
*
* @param inputArray
*/
public SampleLinkedList(int[] inputArray) {
data = new LinkedList<Integer>();
// TODO Add your code here
}
/**
* Increment an element by one
*/
public void increment(int position) {
// TODO Add your code here
}
/**
* Insert a zero at the specified position
*
* @param position
*/
public void insertZero(int position) {
// TODO Add your code here
}
}

View File

@ -0,0 +1,49 @@
package lab11_debugging;
import java.util.Vector;
/**
* Class to test efficiency of Vectors
*
* @author Pierre Roduit (pierre.roduit@hevs.ch)
* @author Pierre-Andr<64> Mudry (pandre.mudry@hevs.ch)
* @version 1.2 (mui) - In {@link #insertZero(int)}, changed insertElementAt with add
*
*/
public class SampleVector {
/**
* Stored values
*/
Vector<Integer> data;
/**
* Constructor
*
* @param inputArray
*/
SampleVector(int[] inputArray) {
data = new Vector<Integer>();
// TODO Complete this
}
/**
* Increment an element by one
*
* @return
*/
void increment(int position) {
// TODO Complete this
}
/**
* Insert a zero at the specified position
*
* @param position
* @return
*/
void insertZero(int position) {
// TODO Complete this
}
}