ECM1406. Answer Sheet Lists

Size: px
Start display at page:

Download "ECM1406. Answer Sheet Lists"

Transcription

1 ECM1406 Answer Sheet Lists These questions are based on those found in Chapters 3 and 4 of the core text Data Structures Using Java by Malik and Nair. The source code for the ArrayListClass, UnorderedArrayList, DataElement and IntElement class are available on the Study Resources page for this module. Exercise 1: The method removeat of the class ArrayListClass removes an element from the list by shifting the elements of the list. However, if the element to be removed is at the beginning of a fairly large list, it could take a long time. Because the list elements are in no particular order, it would be possible to swap the last element of the list with the item to be removed and reduce the length of the list. Rewrite the method removeat to use this technique. Write a program to test the revised method removeat. Exercise 2: The method remove of the class ArrayListClass removes only the first occurrence of an element. Add a method removeall as an abstract method to the ArrayListClass that would remove all occurrences of a given element. Write the definition of the removeall method for the UnorderedArrayList class and a program to test this method. Exercise 3: Develop a LinkedListClass class from the code fragments given in the notes, using the class ArrayListClass as your template. Derive a class UnorderedLinkedList which provides a concrete implementation. Write a program to test the insertion, deletion, search, copylist and copy constructor operations for your UnorderedLinkedList class. Exercise 4: Rework the method removeall from exercise 2 for use with the LinkedListClass and UnorderedLinkList classes. Write a program to test your method.

2 Answer 1: ArrayListClassEx1.java 1 /** 2 * The method removeat of the class ArrayListClass removes an element 3 * from the list by shifting the elements of the list. However, if 4 * the element to be removed is towards the beginning of a fairly 5 * large list, it could take a long time. Because the list elements 6 * are in no particular order, it is possible to swap the last element 7 * of the list with the item to be removed and reduce the length of 8 * the list. 9 * 10 * Rather than rewriting the whole class, we simply provide a 11 * new class which overrides the removeat method as described. 12 * The new test program simply needs to use the new class. 13 */ public class ArrayListClassEx1 extends UnorderedArrayListEx2 { 16 /** 17 * Parametric Constructor. Unfortunately we still have to provide 18 * a constructor for the new class * 20 size an int given the maximum size of the list. 21 */ public ArrayListClassEx1(int size) { 24 super(size); 25 } /** 28 * Remove an item at a given position from the list. 29 * If the position is out of range, an appropriate message 30 * is printed. This is a revised removeat as indicated in 31 * the exercise. 32 * 33 location an int giving the position of the item to 34 * be removed. 35 */ public void removeat(int location) { 38 if (location < 0 location >= length) { 39 System.err.println("The location of the item to be " 40 + "removed is out of range."); 41 } else { 42 // Copy this element a copy of the last element 43 list[location] = list[length - 1]; // Remove the last element 46 length--; 47 list[length] = null; // Having just removed an object, we should call the 50 // garbage collector. 51 System.gc(); 52 } 53 } 54 } 2

3 TestEx1.java 1 /** 2 * Program to test the removeat method of the ArrayListClass. 3 */ 4 5 import java.io.*; 6 import java.util.*; 7 8 public class TestEx1 { 9 10 static BufferedReader keyboard = new 11 BufferedReader(new InputStreamReader(System.in)); public static void main(string[] args) throws IOException { 14 //* A list of up to 50 elements 15 ArrayListClassEx1 intlist = new ArrayListClassEx1(50); //* A temporary element to hold input values 18 IntElement num = new IntElement(); //* A counter so we know how many elements we have input 21 int counter; //* The position of a given element 24 int position; //* The keyboard input buffer 27 StringTokenizer tokenizer; // Tell the user what we are up to. 30 System.out.println("Processing the integer list."); 31 System.out.print("Enter 8 integers in the same line: "); 32 System.out.flush(); tokenizer = new StringTokenizer(keyboard.readLine()); // Read 8 numbers from the user input and place them into 37 // the ArrayList 38 for ( counter = 0; counter < 8; counter++ ) { 39 num.setnum(integer.parseint(tokenizer.nexttoken())); 40 intlist.insert(num); 41 } // Allow the user to confirm the input 44 System.out.println(); 45 System.out.print("The list you entered is: "); 46 intlist.print(); 47 System.out.println(); // Now ask which number they wish to remove 50 System.out.print("Enter the position of the num to be deleted: "); 51 System.out.flush(); 52 position = Integer.parseInt(keyboard.readLine()); 53 System.out.println(); // Remove it 56 intlist.removeat(position); 57 System.out.println("After removing the element at position " 58 + position + ", intlist:"); intlist.print(); 61 System.out.println(); 62 } 63 } 3

4 Answer 2: 1. Copy ArrayListClass.java to ArrayListClassEx2.java, adding the following: 284 /** 285 * Remove all occurrences of an item from the list. 286 * 287 removeitem 288 */ public abstract void removeall(dataelement removeitem); 2. Copy UnorderedArrayList.java to UnorderedArrayListEx2.java adding the following: 122 /** 123 * Remove all of the occurrences of an element from the list. 124 * Scan the whole list and remove all occurrences of an element 125 * from the list. 126 * 127 removeitem the DataElement to remove from the list. 128 */ public void removeall(dataelement removeitem) { 131 int current; if ( length == 0 ) { 134 System.err.println("Cannot delete from an empty list."); 135 } else { 136 current = 0; while ( current < length ) { 139 if ( list[current].equals(removeitem)) { 140 removeat(current); 141 } else { 142 current++; 143 } 144 } 145 } 146 } TestEx2.java 1 /** 2 * Program to test the removeall method of the UnorderedArrayList 3 * as extended by the second exercise. 4 */ 5 6 import java.io.*; 7 import java.util.*; public class TestEx2 { 11 static BufferedReader keyboard = new 12 BufferedReader(new InputStreamReader(System.in)); public static void main(string[] args) throws IOException { 4

5 15 //* A list of up to 50 elements (with the removeall method) 16 UnorderedArrayListEx2 intlist = new UnorderedArrayListEx2(50); //* A temporary element to hold input values 19 IntElement num = new IntElement(); //* A counter so we know how many elements we have input 22 int counter; //* The keyboard input buffer 25 StringTokenizer tokenizer; // Tell the user what we are up to. 28 System.out.println("Processing the integer list."); 29 System.out.print("Enter 8 integers in the same line: "); 30 System.out.flush(); tokenizer = new StringTokenizer(keyboard.readLine()); // Read 8 numbers from the user input and place them into 35 // the ArrayList 36 for ( counter = 0; counter < 8; counter++ ) { 37 num.setnum(integer.parseint(tokenizer.nexttoken())); 38 intlist.insertat(counter,num); 39 } // Allow the user to confirm the input 42 System.out.println(); 43 System.out.print("The list you entered is: "); 44 intlist.print(); 45 System.out.println(); // Now ask which number they wish to remove 48 System.out.print("Enter the num to be deleted: "); 49 System.out.flush(); 50 num.setnum(integer.parseint(keyboard.readline())); 51 System.out.println(); // Remove all instances of the number 54 intlist.removeall(num); 55 System.out.println("After removing " + num.getnum() + ", intlist:"); intlist.print(); 58 System.out.println(); 59 } 60 } 5

6 Answer 3: LinkedListClassEx3.java 1 /** 2 * An abstract class that allows for an unbounded list of data elements. 3 */ 4 5 public abstract class LinkedListClassEx3 { 6 //* A reference to the first node in the list. 7 protected LinkedListNode first; 8 9 //* A reference to the last node in the list. 10 protected LinkedListNode last; //** An int to keep track of the number of nodes in the list. 13 protected int count; /** 16 * Default Constructor. 17 * Initialises the list to an empty list. As this is a linked 18 * list implementation there is no maximum size to set. 19 */ public LinkedListClassEx3() { 22 first = null; 23 last = null; 24 count = 0; 25 } /** 28 * Determine if the list is empty. 29 * The list is empty only when there are no element in the list, 30 * i.e., the head of the list is still referring to null. 31 * 32 true if the list is empty; false otherwise. 33 */ public boolean isempty() { 36 return (first == null); 37 } /** 40 * Clears the list of all contents. 41 * Resets the list to the empty state. 42 */ public void clear() { 45 first = null; 46 last = null; 47 count = 0; 48 System.gc(); 49 } /** 52 * Print the contents of the list to the standard output 53 * device. 54 */ public void print() { 57 // Start with the first node in the list 58 LinkedListNode current = first; 59 6

7 60 // While there are more nodes to be displayed 61 while ( current!= null ) { 62 System.out.print(current.data + " "); 63 current = current.link; 64 } 65 } /** 68 * Return the number of elements in the list. 69 * 70 a count of the number of elements in the list. 71 */ public int length() { 74 return count; 75 } /** 78 * Return a copy of the first element in the list. 79 * 80 a copy of the DataElement at the start of the list, 81 * or null if there is no list. 82 */ public DataElement front() { 85 DataElement temp = null; 86 if ( first!= null ) { 87 // Avoid a null pointer exception 88 temp = first.data.getcopy(); 89 } 90 return temp; 91 } /** 94 * Return a copy last element in the list. 95 * 96 a copy of the last DataElement in the list or a 97 * null if there is no list. 98 */ public DataElement back() { 101 DataElement temp = null; 102 if ( last!= null ) { 103 // Avoid a null pointer exception 104 temp = last.data.getcopy(); 105 } 106 return temp; 107 } // It might be better to let the null pointer exception occur 110 // and catch it, rather than preventing it in the first place /** 113 * Insert a new element at the start of the list. 114 * 115 newitem the DataElement to insert into the list. 116 */ public void insertfirst(dataelement newitem) { 119 LinkedListNode newnode; // Create a new node with newitem as the payload 122 newnode = new LinkedListNode(); 123 newnode.data = newitem.getcopy(); // Link the new node into the front of the list 7

8 126 newnode.link = first; 127 first = newnode; if ( last == null ) { 130 // if the list was empty, the new node is now 131 // the last in the list as well as the first 132 last = newnode; 133 } 134 count++; 135 } /** 138 * Insert a copy of newitem at the end of the list. 139 * 140 newitem the DataElement to add onto the end of the list. 141 */ public void insertlast(dataelement newitem) { 144 LinkedListNode newnode; // Create a new node to become the new end node 147 newnode = new LinkedListNode(); 148 newnode.data = newitem.getcopy(); 149 newnode.link = null; // If the list is empty, add the newnode to the front 152 // of the list. 153 if ( first == null ) { 154 first = newnode; 155 last = newnode; 156 } else { 157 // List not empty, insert newnode after last 158 last.link = newnode; 159 last = newnode; 160 } 161 count++; 162 } // end insertlast /**************************************************************/ 165 /* Abstract Methods to be supplied by implementations of this */ 166 class. */ 167 /**************************************************************/ /** 170 * Search for a specific element in the list. 171 * 172 target the DataElement to search for. 173 true if the target element is found in the 174 * list; false otherwise. 175 */ public abstract boolean search(dataelement target); /** 180 * Remove a specific element from the list. 181 * 182 deleteitem the DataElement to be removed. 183 */ public abstract void deletenode(dataelement deleteitem); /**************************************************************/ /** 190 * Make the current list a copy of another list. 191 * Performs a deep copy, that is it makes a new copies of each of 8

9 192 * the DataElements within the other list. 193 * 194 otherlist the LinkedListClass to duplicate. 195 */ private void copy(linkedlistclassex3 otherlist) { 198 LinkedListNode newnode; // variable to create a node 199 LinkedListNode current; // variable to traverse the list first = null; //make this list empty if ( otherlist.first == null ) { 204 // otherlist is empty 205 first = null; 206 last = null; 207 count = 0; 208 } else { 209 // Copy other list into this list 210 // Start with the first element of the other list 211 count = otherlist.count; 212 current = otherlist.first; // Copy the first element 215 first = new LinkedListNode(); 216 first.data = current.data.getcopy(); 217 first.link = null; // We now have one node in out list 220 last = first; // Start to traverse the other list 223 current = current.link; // While there are nodes in the other list, 226 // copy them into this list 227 while ( current!= null ) { 228 // Make copy of node in other list 229 newnode = new LinkedListNode(); 230 newnode.data = current.data.getcopy(); // Add new node to end of this list 233 newnode.link = null; 234 last.link = newnode; 235 last = newnode; // Move on to next node in other list 238 current = current.link; 239 } 240 } 241 } /** 244 * Make this list a copy of another list. 245 * This is simply another name for the copy() method. 246 * 247 otherlist the other LinkedListClass to duplicate. 248 */ public void copylist(linkedlistclassex3 otherlist) { 251 if ( this!= otherlist ) { 252 //avoid self-copy 253 copy(otherlist); 254 } 255 } /** 9

10 258 * Copy constructor, makes a new list which is a direct copy 259 * of another list. This list is a deep copy of the other 260 * list. 261 * 262 otherlist the other LinkedListClass to copy. 263 */ public LinkedListClassEx3(LinkedListClassEx3 otherlist) { 266 copy(otherlist); 267 } 268 } UnorderedLinkedListEx3.java 1 /** 2 * The UnorderedLinkedList provides a concrete implementation of the 3 * abstract LinkedListClass. This is an unordered list, so we don t 4 * need to worry about keeping the elements of the list in order. 5 */ 6 7 public class UnorderedLinkedListEx3 extends LinkedListClassEx3 { 8 9 //* The default constructor simply create an empty list. 10 public UnorderedLinkedListEx3() { 11 super(); 12 } /** 15 * The copy constructor will make a new list which is a duplicate 16 * of another list, duplicating all of the elements with the list 17 * so that operations on the original list will not effect elements 18 * in the new list. 19 * 20 otherlist the UnorderedLinkedList to duplicate. 21 */ public UnorderedLinkedListEx3(UnorderedLinkedListEx3 otherlist) { 24 // All that really means is that we call the copy 25 // constructor or the abstract LinkedListClass. 26 super(otherlist); 27 } /************************************************************/ 30 /* Concrete implementations of the abstract methods defined */ 31 by the abstract class LinkedListClass */ 32 /************************************************************/ /** 35 * Search for a specific element in the list. 36 * 37 target the DataElement to search for. 38 true if the target element is found in the 39 * list; false otherwise. 40 */ public boolean search(dataelement target) { 43 LinkedListNode current; // variable to traverse the list 44 boolean found = false; // default to the worst case // Start the search as the front of the list 47 current = first; // While there are nodes in the list 10

11 50 while ( current!= null &&!found ) { 51 if ( current.data.equals(target) ) { 52 // we have found our target 53 found = true; 54 } else { 55 // Not found, keep looking 56 current = current.link; 57 } 58 } 59 return found; 60 } /** 63 * Remove a specific element from the list. 64 * 65 deleteitem the DataElement to be removed. 66 */ public void deletenode(dataelement deleteitem) { 69 if ( first == null ) { 70 // List empty, report it as an error 71 System.err.println("Cannot delete from an empty list."); 72 } else { 73 // So we know the list is not empty. 74 if ( first.data.equals(deleteitem) ) { 75 // The item we wish to delete is the first element 76 // of the list, simply move head of the list to the 77 // next element in the list. 78 first = first.link; if ( first == null ) { 81 // There was only one element in the list, 82 // so make the list empty 83 last = null; 84 } count--; 87 } else { 88 // Okay so we have to search the list for the item 89 // we wish to delete. 90 boolean found = false; // assume worst case // We know it s not the first element of the list, 93 // so lets start looking from the second element. 94 LinkedListNode current = first.link; // In order to remove the node we need to keep track 97 // of the node which proceeds the current node so 98 // we can modify its link correctly. 99 // As we are staring with the second node, 100 // that must be the first node. 101 LinkedListNode previous = first; // While we have not found the node, and we have not 104 // reached the end of the list. 105 while ( current!= null &&! found ) { 106 if ( current.data.equals(deleteitem) ) { 107 // we have found our target 108 found = true; 109 } else { 110 // keep looking 111 previous = current; 112 current = current.link; 113 } 114 }

12 116 // Did we find the blasted thing 117 if ( found ) { 118 // Yes, the remove it! 119 // Modify the link on the previous node to refer 120 // to the next node, skipping the current node 121 // all together. 122 previous.link = current.link; // If it was the last node, make the previous 125 // node the last node. 126 if ( last == current ) { 127 last = previous; 128 } count--; 131 } else { 132 // Target node was not found! 133 // Report an error to the standard error output 134 System.out.println("Item to be deleted is " "not in the list."); 136 } 137 } 138 // We really should invoke the garbage collector, 139 // but it s not worth it for a single object. 140 } 141 } 142 } TestEx3.java 1 /** 2 * Program to test the UnorderedLinkedList Class. 3 */ 4 5 import java.io.*; 6 import java.util.*; 7 8 public class TestEx3 { 9 static BufferedReader keyboard = new 10 BufferedReader(new InputStreamReader(System.in)); public static void main(string[] args) throws IOException { 13 UnorderedLinkedListEx3 list1 = new UnorderedLinkedListEx3(); 14 UnorderedLinkedListEx3 list2 = new UnorderedLinkedListEx3(); //* A temporary element to hold input values 17 IntElement num = new IntElement(); // Tell the user what we are up to 20 System.out.println("Enter integers on one line."); 21 StringTokenizer tokenizer = new StringTokenizer(keyboard.readLine()); // Read a number of item from the input string adding it to the list 24 while ( tokenizer.hasmoretokens() ) { 25 num.setnum( Integer.parseInt(tokenizer.nextToken()) ); 26 list1.insertlast(num); 27 } // Allow the user to confirm the input 30 System.out.println(); 31 System.out.print("List 1: "); 32 list1.print(); System.out.println(); 12

13 35 System.out.println("Length of list 1: " + list1.length()); // Test 1: Search the list for a specific value 38 System.out.print("\nTest 1: Enter the number to be searched: "); 39 System.out.flush(); 40 num.setnum(integer.parseint(keyboard.readline())); 41 System.out.println(); if (list1.search(num) ) { 44 System.out.println("Test 1: " + num + " found in list 1."); 45 } else { 46 System.out.println("Test 1: " + num + " is not in list 1."); 47 } // Test 2: Make a copy of the list 50 list2.copylist(list1); System.out.println("\nTest 2: After copying list 1 into list 2"); 53 System.out.print("list 2: "); 54 list2.print(); 55 System.out.println(); 56 System.out.println("Test 2: Length of list 2: " + list2.length()); // Test 3: Delete a node from the list 59 System.out.print("\nTest 3: Enter the number to be deleted: "); 60 System.out.flush(); 61 num.setnum(integer.parseint(keyboard.readline())); list2.deletenode(num); System.out.print("Test 3: After deleting " + num + " list 2: "); 66 list2.print(); 67 System.out.println(); 68 System.out.println("Test 3: Length of list 2: " + list2.length()); // Test 4: Use the copy constructor to duplicate a list 71 UnorderedLinkedListEx3 list3 = new UnorderedLinkedListEx3(list1); // If the list was correctly copied, with a deep copy this 74 // should be fine. If it was only cloned, copied with a 75 // shallow copy, this will cause a null pointer exception 76 // when we access the original list. 77 destroylist(list3); System.out.print("Test 4: list 1: "); 80 list1.print(); 81 System.out.println(); 82 System.out.println("Test 4: Length of list 1: " + list1.length()); 83 } /** 86 * Destroy a list and all the elements within the list. 87 * 88 list the list to be destroyed. 89 */ 90 private static void destroylist(unorderedlinkedlistex3 list) { 91 System.out.println("In the method destroylist."); 92 list.print(); 93 System.out.println(); // Clear the actual list. 96 list.clear(); System.out.println("Exiting the method destroylist."); 99 } 100 } 13

14 Answer 4: 1. Copy LinkedListClassEx3.java to LinkedListClassEx4.java adding the following to the abstract methods section: 187 /** 188 * Remove all occurrences of an item from the list. 189 * 190 removeitem 191 */ public abstract void removeall(dataelement removeitem); 2. Copy UnorderedLinkedListEx3.java to UnorderedLinkedListEx4.java adding the following: 143 /** 144 * Remove all of the occurrences of an element from the list. 145 * Scan the whole list and remove all occurrences of an element 146 * from the list. 147 * 148 removeitem the DataElement to remove from the list. 149 */ public void removeall(dataelement removeitem) { 152 if ( first == null ) { 153 // List empty, report it as an error 154 System.err.println("Cannot delete from an empty list."); 155 } else { 156 // Start at the beginning, always a good place to start 157 LinkedListNode current = first; 158 LinkedListNode previous = null; // While there are nodes in the list 161 while ( current!= null ) { 162 // Should we remove it? 163 if ( current.data.equals(removeitem) ) { 164 // Is it the head of the list? 165 if ( current == first ) { 166 first = first.link; 167 current = first; // Was it the only item in the list? 170 if ( first == null ) { 171 last = null; 172 } 173 } else { 174 // Not first element 175 previous.link = current.link; // Is the last element? 178 if ( current == last ) { 179 last = previous; 180 } else { 181 current = previous.link; 182 } 183 } // Decrement cashed list length 186 count--; 187 } else { 188 // Not for removal, move on to next node 189 previous = current; 190 current = current.link; 191 } 192 } 14

15 // We will call up the garbage collector this time, as 195 // we could have removed a large number of elements 196 System.gc(); 197 } 198 } TestEx4.java 1 /** 2 * Program to test the removeall method of the UnorderedLinkedList 3 * as extended by the second exercise. 4 */ 5 6 import java.io.*; 7 import java.util.*; public class TestEx4 { 11 static BufferedReader keyboard = new 12 BufferedReader(new InputStreamReader(System.in)); public static void main(string[] args) throws IOException { 15 //* An unbounded list of elements (with the removeall method) 16 UnorderedLinkedListEx4 list = new UnorderedLinkedListEx4(); //* A temporary element to hold input values 19 IntElement num = new IntElement(); //* The keyboard input buffer 22 StringTokenizer tokenizer; // Tell the user what we are up to 25 System.out.println("Enter integers on one line."); 26 tokenizer = new StringTokenizer(keyboard.readLine()); // Read a number of item from the input string adding it to the list 29 while ( tokenizer.hasmoretokens() ) { 30 num.setnum( Integer.parseInt(tokenizer.nextToken()) ); 31 list.insertlast(num); 32 } // Allow the user to confirm the input 35 System.out.println(); 36 System.out.println("list: "); 37 list.print(); System.out.println(); 40 System.out.println("Length of list: " + list.length()); // Test 1: Remove All 43 System.out.print("Enter the number to be deleted: "); 44 System.out.flush(); 45 num.setnum(integer.parseint(keyboard.readline())); // Remove all instances of the number 48 list.removeall(num); 49 System.out.println("After removing all occurrences of " + num + " list: "); 50 list.print(); 51 System.out.println(); 52 System.out.println("Length of list: " + list.length()); 53 } 54 } 15

ECM1406:DataStructuresandTeamProject. ArrayBasedLists. Chapter 3

ECM1406:DataStructuresandTeamProject. ArrayBasedLists. Chapter 3 ECM1406:DataStructuresandTeamProject ArrayBasedLists Chapter 3 Array Based Lists 1/17 TheList Lists A collection of elements of the same type Length of list is number of elements in list Operations Create

More information

Data Structures & Algorithms

Data Structures & Algorithms A 1. What are the basic components of a linked list? A. Head and tail are the only important components B. Data members for the information to be stored and a link to the next item C. Generic class because

More information

Lab 11. A sample of the class is:

Lab 11. A sample of the class is: Lab 11 Lesson 11-2: Exercise 1 Exercise 2 A sample of the class is: public class List // Methods public void store(int item) values[length] = item; length++; public void printlist() // Post: If the list

More information

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15)

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15) CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15) A data structure is a software construct used to organize our data in a particular way. Some common data structures include lists,

More information

Full file at Chapter 2 - Inheritance and Exception Handling

Full file at   Chapter 2 - Inheritance and Exception Handling Chapter 2 - Inheritance and Exception Handling TRUE/FALSE 1. The superclass inherits all its properties from the subclass. ANS: F PTS: 1 REF: 76 2. Private members of a superclass can be accessed by a

More information

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list } Linked Lists Since a variable referencing an object just holds the address of the object in memory, we can link multiple objects together to form dynamic lists or other structures. In our case we will

More information

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371 Class IX HY 2013 Revision Guidelines Page 1 Section A (Power Point) Q1.What is PowerPoint? How are PowerPoint files named? Q2. Describe the 4 different ways of creating a presentation? (2 lines each) Q3.

More information

A sample print out is: is is -11 key entered was: w

A sample print out is: is is -11 key entered was: w Lab 9 Lesson 9-2: Exercise 1, 2 and 3: Note: when you run this you may need to maximize the window. The modified buttonhandler is: private static class ButtonListener implements ActionListener public void

More information

Spring 2010 Java Programming

Spring 2010 Java Programming Java Programming: Guided Learning with Early Objects Chapter 7 - Objectives Learn about arrays Explore how to declare and manipulate data in arrays Learn about the instance variable length Understand the

More information

Week 14 Lab A Linked List of Integers Maximum Points = 10

Week 14 Lab A Linked List of Integers Maximum Points = 10 Week 14 Lab A Linked List of Integers Maximum Points = 10 File IntList.java contains definitions for a linked list of integers. The class contains an inner class IntNode that holds information for a single

More information

Input from Files. Buffered Reader

Input from Files. Buffered Reader Input from Files Buffered Reader Input from files is always text. You can convert it to ints using Integer.parseInt() We use BufferedReaders to minimize the number of reads to the file. The Buffer reads

More information

Classes Basic Overview

Classes Basic Overview Final Review!!! Classes and Objects Program Statements (Arithmetic Operations) Program Flow String In-depth java.io (Input/Output) java.util (Utilities) Exceptions Classes Basic Overview A class is a container

More information

Exercise 4: Loops, Arrays and Files

Exercise 4: Loops, Arrays and Files Exercise 4: Loops, Arrays and Files worth 24% of the final mark November 4, 2004 Instructions Submit your programs in a floppy disk. Deliver the disk to Michele Zito at the 12noon lecture on Tuesday November

More information

Sorting and Searching

Sorting and Searching CHAPTER 13 Sorting and Searching The exercises in this chapter are a framework for comparing algorithms empirically. This approach provides students with a means for understanding the finer details of

More information

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods

More information

Java Input/Output. 11 April 2013 OSU CSE 1

Java Input/Output. 11 April 2013 OSU CSE 1 Java Input/Output 11 April 2013 OSU CSE 1 Overview The Java I/O (Input/Output) package java.io contains a group of interfaces and classes similar to the OSU CSE components SimpleReader and SimpleWriter

More information

4. Finding & Displaying Record of Salesman with minimum net income. 5. Finding & Displaying Record of Salesman with maximum net income.

4. Finding & Displaying Record of Salesman with minimum net income. 5. Finding & Displaying Record of Salesman with maximum net income. Solution of problem#55 of Lab Assignment Problem Statement: Design & Implement a java program that can handle salesmen records of ABC Company. Each salesman has unique 4 digit id #, name, salary, monthly

More information

Selected Questions from by Nageshwara Rao

Selected Questions from  by Nageshwara Rao Selected Questions from http://way2java.com by Nageshwara Rao Swaminathan J Amrita University swaminathanj@am.amrita.edu November 24, 2016 Swaminathan J (Amrita University) way2java.com (Nageshwara Rao)

More information

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O Week 12 Streams and File I/O Overview of Streams and File I/O Text File I/O 1 I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or a file

More information

Introduction to Linked Data Structures

Introduction to Linked Data Structures Introduction to Linked Data Structures A linked data structure consists of capsules of data known as nodes that are connected via links Links can be viewed as arrows and thought of as one way passages

More information

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive Solutions Exercises Unit 19 1 General Algorithm for Recursion

More information

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution Ch 5 Arrays Multiple Choice Test 01. An array is a ** (A) data structure with one, or more, elements of the same type. (B) data structure with LIFO access. (C) data structure, which allows transfer between

More information

FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO {

FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO { FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO public static void main(string[] args) throws IOException BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int frames,

More information

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods. Inheritance Inheritance is the act of deriving a new class from an existing one. Inheritance allows us to extend the functionality of the object. The new class automatically contains some or all methods

More information

Stacks Goodrich, Tamassia

Stacks Goodrich, Tamassia Stacks Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error conditions associated with operations Example:

More information

Case Study: Savings Account Interest

Case Study: Savings Account Interest ecture 8 Loops: recap + example. Files: abstracting from a specific devise. Streams and Tokens. Examples. Material from the second half of Holmes Chapter 4. 1 w do you add up a sequence of numbers? = 1;

More information

HST 952. Computing for Biomedical Scientists Lecture 8

HST 952. Computing for Biomedical Scientists Lecture 8 Harvard-MIT Division of Health Sciences and Technology HST.952: Computing for Biomedical Scientists HST 952 Computing for Biomedical Scientists Lecture 8 Outline Vectors Streams, Input, and Output in Java

More information

Introduction to Computer Science II CS S-18 Linked Lists

Introduction to Computer Science II CS S-18 Linked Lists Introduction to Computer Science II CS112-2012S-18 Linked Lists David Galles Department of Computer Science University of San Francisco 18-0: Linked Lists Linked List node Data Pointer to the next element

More information

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution Ch 5 Arrays Multiple Choice 01. An array is a (A) (B) (C) (D) data structure with one, or more, elements of the same type. data structure with LIFO access. data structure, which allows transfer between

More information

Algorithms and Problem Solving

Algorithms and Problem Solving Algorithms and Problem Solving Introduction What is an Algorithm? Algorithm Properties Example Exercises Unit 16 1 What is an Algorithm? What is an Algorithm? An algorithm is a precisely defined and ordered

More information

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013 CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013 Name: This exam consists of 5 problems on the following 6 pages. You may use your double-sided hand-written 8 ½ x 11 note sheet

More information

i219 Software Design Methodology 8. Dynamic modeling 1 Kazuhiro Ogata (JAIST) Outline of lecture

i219 Software Design Methodology 8. Dynamic modeling 1 Kazuhiro Ogata (JAIST) Outline of lecture i219 Software Design Methodology 8. Dynamic modeling 1 Kazuhiro Ogata (JAIST) Outline of lecture 2 Use case Use case diagram State (machine) diagram Sequence diagram Class diagram of vending machine Vending

More information

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request. University of Toronto CSC148 Introduction to Computer Science Fall 2001 Mid Term Test Section L5101 Duration: 50 minutes Aids allowed: none Make sure that your examination booklet has 8 pages (including

More information

Suppose that we have linked list of integers where each node is represented as: // An item in the list.

Suppose that we have linked list of integers where each node is represented as: // An item in the list. Linked List Suppose that we have linked list of integers where each node is represented as: class ListNode { int item; // An item in the list. ListNode next; // Pointer to next item in the list. This node

More information

inside: THE MAGAZINE OF USENIX & SAGE June 2001 Volume 26 Number 3 PROGRAMMING Using CORBA with Java by Prithvi Rao

inside: THE MAGAZINE OF USENIX & SAGE June 2001 Volume 26 Number 3 PROGRAMMING Using CORBA with Java by Prithvi Rao THE MAGAZINE OF USENIX & SAGE June 2001 Volume 26 Number 3 inside: PROGRAMMING Using CORBA with Java by Prithvi Rao # & The Advanced Computing Systems Association & The System Administrators Guild using

More information

CMSC 331 Second Midterm Exam

CMSC 331 Second Midterm Exam 1 20/ 2 80/ 331 First Midterm Exam 11 November 2003 3 20/ 4 40/ 5 10/ CMSC 331 Second Midterm Exam 6 15/ 7 15/ Name: Student ID#: 200/ You will have seventy-five (75) minutes to complete this closed book

More information

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages References Exceptions "Handling Errors with Exceptions", Java tutorial http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html CSE 413, Autumn 2005 Programming Languages http://www.cs.washington.edu/education/courses/413/05au/

More information

CN208 Introduction to Computer Programming

CN208 Introduction to Computer Programming CN208 Introduction to Computer Programming Lecture #11 Streams (Continued) Pimarn Apipattanamontre Email: pimarn@pimarn.com 1 The Object Class The Object class is the direct or indirect superclass of every

More information

Full file at

Full file at Chapter 1 Primitive Java Weiss 4 th Edition Solutions to Exercises (US Version) 1.1 Key Concepts and How To Teach Them This chapter introduces primitive features of Java found in all languages such as

More information

Timing ListOperations

Timing ListOperations Timing ListOperations Michael Brockway November 13, 2017 These slides are to give you a quick start with timing operations in Java and with making sensible use of the command-line. Java on a command-line

More information

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004 1.00/1.001 Introduction to Computers and Engineering Problem Solving Final / December 13, 2004 Name: Email Address: TA: Section: You have 180 minutes to complete this exam. For coding questions, you do

More information

Lex and Yacc. More Details

Lex and Yacc. More Details Lex and Yacc More Details Calculator example From http://byaccj.sourceforge.net/ %{ import java.lang.math; import java.io.*; import java.util.stringtokenizer; %} /* YACC Declarations; mainly op prec &

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 15 Lecture 15-1: Implementing ArrayIntList reading: 15.1-15.3 Recall: classes and objects class: A program entity that represents: A complete program or module, or A template

More information

Dynamic Data Structures

Dynamic Data Structures Chapter 10 Dynamic Data Structures Vectors Linked Data Structures Chapter 10 Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Overview This chapter is about data structures that

More information

GlobalLogic Technical Question Paper

GlobalLogic Technical Question Paper GlobalLogic Technical Question Paper What is the output of the following code when compiled and run? Select two correct answers. public class Question01 { public static void main(string[] args){ int y=0;

More information

CS S-20 Linked Lists III 1. We can then use the next pointer of the previous node to do removal (example on board)

CS S-20 Linked Lists III 1. We can then use the next pointer of the previous node to do removal (example on board) CS112-2012S-20 Linked Lists III 1 20-0: Linked List ious Practical Example: removeat(int index) remove( o) 20-1: removeat First need to get to node before the one we want to remove We can then use the

More information

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Queues. CITS2200 Data Structures and Algorithms. Topic 5 CITS2200 Data Structures and Algorithms Topic 5 Queues Implementations of the Queue ADT Queue specification Queue interface Block (array) representations of queues Recursive (linked) representations of

More information

Data Abstraction and Specification of ADTs

Data Abstraction and Specification of ADTs CITS2200 Data Structures and Algorithms Topic 4 Data Abstraction and Specification of ADTs Example The Reversal Problem and a non-adt solution Data abstraction Specifying ADTs Interfaces javadoc documentation

More information

A Guide Illustrating the Core Java Equivalent to Selected Tasks Done Using the HSA Class Library

A Guide Illustrating the Core Java Equivalent to Selected Tasks Done Using the HSA Class Library HSA to Core Java A Guide Illustrating the Core Java Equivalent to Selected Tasks Done Using the HSA Class Library The examples below compare how tasks are done using the hsa Console class with how they

More information

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015 LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 5 due tonight at midnight -assignment 6 is out -YOU WILL BE SWITCHING PARTNERS! 3 assignment

More information

Assignment 8B SOLUTIONS

Assignment 8B SOLUTIONS CSIS 10A Assignment 8B SOLUTIONS Read: Chapter 8 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment,

More information

Byte and Character Streams. Reading and Writing Console input and output

Byte and Character Streams. Reading and Writing Console input and output Byte and Character Streams Reading and Writing Console input and output 1 I/O basics The io package supports Java s basic I/O (input/output) Java does provide strong, flexible support for I/O as it relates

More information

CSCD 326 Data Structures I Queues

CSCD 326 Data Structures I Queues CSCD 326 Data Structures I Queues 1 Linked List Queue Implementation Diagram Front Back A B C D 0 6 public interface QueueInterface { Queue Interface public boolean isempty(); // Determines whether

More information

Text User Interfaces. Keyboard IO plus

Text User Interfaces. Keyboard IO plus Text User Interfaces Keyboard IO plus User Interface and Model Model: objects that solve problem at hand. User interface: interacts with user getting input from user giving output to user reporting on

More information

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20 File IO Computer Science and Engineering College of Engineering The Ohio State University Lecture 20 I/O Package Overview Package java.io Core concept: streams Ordered sequences of data that have a source

More information

List ADT. B/W Confirming Pages

List ADT. B/W Confirming Pages wu3399_ch8.qxd //7 :37 Page 98 8 List ADT O b j e c t i v e s After you have read and studied this chapter, you should be able to Describe the key features of the List ADT. the List ADT using an array

More information

Review: Array Initializer Lists

Review: Array Initializer Lists More on Arrays Review of Arrays of ints, doubles, chars Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 7.2 Reading for this lecture: L&L 7.3 7.7,

More information

Lecture 35. Threads. Reading for next time: Big Java What is a Thread?

Lecture 35. Threads. Reading for next time: Big Java What is a Thread? Lecture 35 Threads Reading for next time: Big Java 21.4 What is a Thread? Imagine a Java program that is reading large files over the Internet from several different servers (or getting data from several

More information

CSCD 326 Data Structures I Stacks

CSCD 326 Data Structures I Stacks CSCD 326 Data Structures I Stacks 1 Stack Interface public interface StackInterface { public boolean isempty(); // Determines whether the stack is empty. // Precondition: None. // Postcondition: Returns

More information

Recursive Problem Solving

Recursive Problem Solving Recursive Problem Solving Objectives Students should: Be able to explain the concept of recursive definition. Be able to use recursion in Java to solve problems. 2 Recursive Problem Solving How to solve

More information

Chapter 10 Linked Lists A

Chapter 10 Linked Lists A Data Structures for Java William H. Ford William R. Topp Chapter 10 Linked Lists A Bret Ford 2005, Prentice Hall Introducing Linked Lists To insert or remove an element at an interior location in an ArrayList

More information

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters Overloaded Methods Sending Messages Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Initialization & Cleanup 2 Overloaded Constructors Sending Parameters accessor method 3 4 Sending Parameters

More information

CS165 Practice Final Exam

CS165 Practice Final Exam CS165 Practice Final Exam I, the undersigned, do hereby affirm that the work contained in this exam is solely my own, and that none of the results were achieved by cheating. This includes using automated

More information

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Tools : The Java Compiler. The Java Interpreter. The Java Debugger Tools : The Java Compiler javac [ options ] filename.java... -depend: Causes recompilation of class files on which the source files given as command line arguments recursively depend. -O: Optimizes code,

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

Java Magistère BFA

Java Magistère BFA Java 101 - Magistère BFA Lesson 3: Object Oriented Programming in Java Stéphane Airiau Université Paris-Dauphine Lesson 3: Object Oriented Programming in Java (Stéphane Airiau) Java 1 Goal : Thou Shalt

More information

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. CSE 142, Summer 2002 Computer Programming 1. Exceptions CSE 142, Summer 2002 Computer Programming 1 http://www.cs.washington.edu/education/courses/142/02su/ 12-Aug-2002 cse142-19-exceptions 2002 University of Washington 1 Reading Readings and References»

More information

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1. Readings and References Exceptions CSE 142, Summer 2002 Computer Programming 1 http://www.cs.washington.edu/education/courses/142/02su/ Reading» Chapter 18, An Introduction to Programming and Object Oriented

More information

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade; Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

More information

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Creating

More information

1.00/ Introduction to Computers and Engineering Problem Solving. Final Exam / December 21, 2005

1.00/ Introduction to Computers and Engineering Problem Solving. Final Exam / December 21, 2005 1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Exam / December 21, 2005 Name: Email Address: TA: Section: a You have 180 minutes to complete this exam. For coding questions,

More information

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while CSCI 150 Fall 2007 Java Syntax The following notes are meant to be a quick cheat sheet for Java. It is not meant to be a means on its own to learn Java or this course. For that you should look at your

More information

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class

More information

1. Download the JDK 6, from

1. Download the JDK 6, from 1. Install the JDK 1. Download the JDK 6, from http://java.sun.com/javase/downloads/widget/jdk6.jsp. 2. Once the file is completed downloaded, execute it and accept the license agreement. 3. Select the

More information

1. An operation in which an overall value is computed incrementally, often using a loop.

1. An operation in which an overall value is computed incrementally, often using a loop. Practice Exam 2 Part I: Vocabulary (10 points) Write the terms defined by the statements below. 1. An operation in which an overall value is computed incrementally, often using a loop. 2. The < (less than)

More information

Lecture 5: Implementing Lists, Version 1

Lecture 5: Implementing Lists, Version 1 CS18 Integrated Introduction to Computer Science Fisler, Nelson Lecture 5: Implementing Lists, Version 1 Contents 1 Implementing Lists 1 2 Methods 2 2.1 isempty...........................................

More information

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse Object-Oriented Design Object Oriented Design Goals Robustness Adaptability Flexible code reuse Principles Abstraction Encapsulation Modularity March 2005 Object Oriented Design 1 March 2005 Object Oriented

More information

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15 Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15 Title: Demonstrate implementation of data structure in Java Objectives: To learn implementation of data structure

More information

CSIC Computing Project

CSIC Computing Project CSIC Computing Project Vigenere Cipher Final Report December 2005 Project co-ordinator: Dr. Naqvi Team members: Maryam Salar Kalantari, Masood Khosroshahy, Yong Wu Table of Contents: 1) Introduction to

More information

Introduction to Computer Science II CS S-20 Linked Lists III

Introduction to Computer Science II CS S-20 Linked Lists III Introduction to Computer Science II CS112-2012S-20 Linked Lists III David Galles Department of Computer Science University of San Francisco 20-0: Linked List Previous Practical Example: removeat(int index)

More information

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O I/O 10-7-2013 I/O in Java I/O streams vs. Reader/Writer HW#3 due today Reading Assignment: Java tutorial on Basic I/O public class Swimmer implements Cloneable { public Date geteventdate() { return (Date)

More information

1.00/ Introduction to Computers and Engineering Problem Solving. Final Exam / December 21, 2005

1.00/ Introduction to Computers and Engineering Problem Solving. Final Exam / December 21, 2005 1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Exam / December 21, 2005 Name: Email Address: TA: Section: You have 180 minutes to complete this exam. For coding questions, you

More information

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018 CSC 172 Data Structures and Algorithms Lecture #9 Spring 2018 SINGLY LINKED LIST 3.1.3 Linked lists We will consider these for Singly linked lists Doubly linked lists Basic Singly Linked List class Node

More information

3. Design by Contract

3. Design by Contract 3. Design by Contract Oscar Nierstrasz Design by Contract Bertrand Meyer, Touch of Class Learning to Program Well with Objects and Contracts, Springer, 2009. 2 Roadmap > Contracts > Stacks > Design by

More information

Error Handling. public int divide(double a, double b) { if (b==0) return -1; // error double result = a/b; return 0; // success }

Error Handling. public int divide(double a, double b) { if (b==0) return -1; // error double result = a/b; return 0; // success } Exceptions Error Handling You do a lot on this in your practicals, so we'll just touch on it here The traditional way of handling errors is to return a value that indicates success/failure/error public

More information

Oct Decision Structures cont d

Oct Decision Structures cont d Oct. 29 - Decision Structures cont d Programming Style and the if Statement Even though an if statement usually spans more than one line, it is really one statement. For instance, the following if statements

More information

Ashish Jamuda CS 331 DATA STRUCTURES & ALGORITHMS COURSE FINAL EXAM

Ashish Jamuda CS 331 DATA STRUCTURES & ALGORITHMS COURSE FINAL EXAM Ashish Jamuda CS 331 DATA STRUCTURES & ALGORITHMS COURSE FINAL EXAM Question 1: Given a directed graph, design an algorithm to find out whether there is a route between two nodes. This problem can be solved

More information

Object Oriented Software Design

Object Oriented Software Design Object Oriented Software Design I/O subsystem API Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 10, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction to Java

More information

Object-Oriented Design. March 2005 Object Oriented Design 1

Object-Oriented Design. March 2005 Object Oriented Design 1 Object-Oriented Design March 2005 Object Oriented Design 1 Object Oriented Design Goals Robustness Adaptability Flexible code reuse Principles Abstraction Encapsulation Modularity March 2005 Object Oriented

More information

Object Oriented Software Design

Object Oriented Software Design Object Oriented Software Design I/O subsystem API Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 10, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction to Java

More information

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Readings List Implementations Chapter 20.2 Objectives Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Additional references:

More information

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists. Announcements MODULE WEB-SITE: http://www.csc.liv.ac.uk/ michele/teaching/comp102/comp102.html FIRST ASSIGNMENT DEADLINE: Wednesday, February 1st, 14.30, LAB 7 Boxes (late submissions to be left in the

More information

Example Program. public class ComputeArea {

Example Program. public class ComputeArea { COMMENTS While most people think of computer programs as a tool for telling computers what to do, programs are actually much more than that. Computer programs are written in human readable language for

More information

Lesson 3: Accepting User Input and Using Different Methods for Output

Lesson 3: Accepting User Input and Using Different Methods for Output Lesson 3: Accepting User Input and Using Different Methods for Output Introduction So far, you have had an overview of the basics in Java. This document will discuss how to put some power in your program

More information

Class, Variable, Constructor, Object, Method Questions

Class, Variable, Constructor, Object, Method Questions Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx

More information

Data Types and the while Statement

Data Types and the while Statement Session 2 Student Name Other Identification Data Types and the while Statement In this laboratory you will: 1. Learn about three of the primitive data types in Java, int, double, char. 2. Learn about the

More information

This Week s Agenda (Part A) CS121: Computer Programming I. The Games People Play. Data Types & Structures. The Array in Java.

This Week s Agenda (Part A) CS121: Computer Programming I. The Games People Play. Data Types & Structures. The Array in Java. CS121: Computer Programming I A) Collections B) File I/O & Error Handling Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use

More information

Lecture 7: Implementing Lists, Version 2

Lecture 7: Implementing Lists, Version 2 CS18 Integrated Introduction to Computer Science Fisler, Nelson Lecture 7: Implementing Lists, Version 2 Contents 1 The Impact of addfirst on Lists 1 2 Mutating List Contents 2 2.1 The Basic List Classes...................................

More information

????? An Exercise. Suppose that we have written a class named Play that has a String instance variable named filename.

????? An Exercise. Suppose that we have written a class named Play that has a String instance variable named filename. An Exercise Suppose that we have written a class named Play that has a String instance variable named filename. A Plays respond to a startswith( char initial ) message by returning the number of Strings

More information

Streams and File I/O

Streams and File I/O Chapter 9 Streams and File I/O Overview of Streams and File I/O Text File I/O Binary File I/O File Objects and File Names Chapter 9 Java: an Introduction to Computer Science & Programming - Walter Savitch

More information