Question: Your job today is to write the code for the following two methods: The one method which we forgot to include was a get() method
Your job today is to write the code for the following two methods:
- The one method which we forgot to include was a get() method that would get data out of the class so that we can use the information.
- This method should travel to the int node and return the value from that node.
- If the user asks for a index that does not exist then you should return an IllegalArgumentException.
- Otherwise it should return the data value.
- The other method I want to see is a removeLast() method that will remove the last method
- if the size is zero, don't do anything.
- If the size is one, the list will be emptied
- Otherwise the list will lose the last element.
Make the changes to the WoodsLL.java file and upload just that one file for grading. The other two files are there to help you test. You will want to un-comment the lines from the LLExampleMain.java to have the methods do what they are supposed to do.
LLExampleMain.java
1 public class LLExampleMain 2 { 3 public static void main(String[] args) 4 { 5 6 WoodsLL myLL = new WoodsLL<>(); 7 myLL.add(new Rectangle()); 8 myLL.add(new Rectangle(2,3)); 9 myLL.add(new Rectangle(33,55)); 10 myLL.add(1, new Rectangle(99,99)); 11 myLL.add(new Rectangle(100,100)); 12 myLL.add(new Rectangle(200,200)); 13 14 15 System.out.println(myLL); 16 17 // The code below should work when you are done. 18 // System.out.println("Location 0 = : " + myLL.get(0)); 19 // System.out.println("Location 1 = : " + myLL.get(1)); 20 // System.out.println("Location Last = : " + myLL.get(myLL.size() - 1 )); 21 // try 22 // { 23 // System.out.println("Location 10 = : " + myLL.get(10)); 24 // } 25 // catch (IllegalArgumentException e) 26 // { 27 // System.out.println("Good Job, you caught the ERROR"); 28 // } 29 // try 30 // { 31 // System.out.println("Location -1 = : " + myLL.get(-1)); 32 // } 33 // catch (IllegalArgumentException e) 34 // { 35 // System.out.println("Good Job, you caught the other Error"); 36 // } 37 38 // ***** ROUND 2 PART B **************************************************** 39 40 //while (myLL.size() >0) 41 //{ 42 // myLL.removeLast(); 43 // System.out.println(myLL + " has size " + myLL.size() ); 44 //} 45 46 System.out.println("*** Round 2 ********************************************"); 47 48 49 WoodsLL otherLL = new WoodsLL<>(); 50 otherLL.add(1.1); 51 otherLL.add(2.2); 52 otherLL.add(3.3); 53 otherLL.add(4.4); 54 otherLL.add(5.5); 55 otherLL.add(6.6); 56 57 System.out.println(otherLL); 58 59 // // The code below should work when you are done. 60 // System.out.println("Location 0 = : " + otherLL.get(0)); 61 // System.out.println("Location 1 = : " + otherLL.get(1)); 62 // System.out.println("Location Last = : " + otherLL.get(myLL.size() - 1 )); 63 // try 64 // { 65 // System.out.println("Location 10 = : " + otherLL.get(10)); 66 // } 67 // catch (IllegalArgumentException e) 68 // { 69 // System.out.println("Good Job, you caught the ERROR"); 70 // } 71 // try 72 // { 73 // System.out.println("Location -1 = : " + otherLL.get(-1)); 74 // } 75 // catch (IllegalArgumentException e) 76 // { 77 // System.out.println("Good Job, you caught the other Error"); 78 // } 79 80 } 81 }
Rectangle.java
1 public class Rectangle implements Comparable 2 { 3 private int length; 4 private int width; 5 6 public int compareTo(Rectangle o) 7 { 8 return this.getArea() - o.getArea(); 9 10 } 11 12 public Rectangle() 13 { 14 length = 10; 15 width = 10; 16 } 17 18 public Rectangle(int s) 19 { 20 length = Math.abs(s); 21 width = Math.abs(s); 22 } 23 24 public Rectangle(int l, int w) 25 { 26 length = Math.abs(l); 27 width = Math.abs(w); 28 } 29 30 public String toString() 31 { 32 return "[" + length + "x" + width + "]"; 33 } 34 35 public int getArea() 36 { 37 return length * width; 38 } 39 }
1 public class WoodsLL > 2 { 3 private Node head; 4 private int size; 5 6 public WoodsLL() 7 { 8 head = null; 9 size = 0; 10 } 11 12 public void clear() 13 { 14 head = null; 15 size = 0; 16 } 17 18 19 public void add(int location, E x) 20 { 21 if (location < 0) throw new IllegalArgumentException(); 22 else if (location > size) throw new IllegalArgumentException(); 23 else if (location == size) add(x); 24 else if (location == 0) 25 { 26 size++; 27 Node temp = new Node(); 28 temp.data = x; 29 temp.next = head; 30 head = temp; 31 } 32 else 33 { 34 Node current = head; 35 for (int i = 1; i < location; i++) current = current.next; 36 Node temp = new Node<>(); 37 temp.data = x; 38 temp.next = current.next; 39 current.next = temp; 40 size++; 41 } 42 } 43 44 45 public boolean contains(E x) 46 { 47 Node current = head; 48 while(current != null) 49 { 50 if (current.data.compareTo(x) == 0 ) return true; 51 else 52 current = current.next; 53 } 54 55 return false; 56 } 57 58 public int length() 59 { 60 return size(); 61 } 62 63 public int size() 64 { 65 return size; 66 } 67 68 public void add(E x) 69 { 70 if (head == null) 71 { 72 // no head node yet. 73 head = new Node(); 74 size++; 75 head.data = x; 76 head.next = null; 77 } 78 else 79 { 80 Node temp; 81 temp = head; 82 while (temp.next != null) 83 temp = temp.next; // Move one node forward 84 85 temp.next = new Node(); //Create the new "last" 86 temp.next.data = x; // node and fill it in 87 temp.next.next = null; 88 size++; 89 } 90 91 } 92 93 public boolean remove(E x) 94 { 95 if (head == null) return false; 96 if (head.data.equals(x)) 97 { 98 head = head.next; 99 size--; 100 return true; 101 } 102 Node current = head; 103 Node behind = head; 104 105 while(current != null && !current.data.equals(x)) 106 { 107 behind = current; 108 current = current.next; 109 } 110 if (current != null) 111 { 112 behind.next = current.next; 113 size--; 114 return true; 115 } 116 117 return false; 118 } 119 120 public String toString() 121 { 122 // Empty LL 123 if (head == null) return "[]"; 124 125 // LL has at least ONE item 126 String result = ""; 127 Node temp = head; 128 129 while (temp != null) 130 { 131 result = result + "," + temp.data.toString(); 132 temp = temp.next; // 133 } 134 135 return "[" + result.substring(1) + "]"; 136 137 } 138 159 160 private class Node 161 { 162 public E data; 163 public Node next; 164 } 165 }