Solutions to example exam 2 1 1. Describe how to scan a two-dimensional array. Describe the pseudocode and explain as clearly as possible how it works. Solution: See assignment 3. - describe the main idea, which data it can be used on. - Write the pseudo code - Give example of how it works. 2 2. Write the code for two classes that inherit from the class (SuperClass2) below and contains at least one self method. public class SuperClass2 { int number = 2; public int getNumber { return number; } } 3 Question 2 – Solution public class SubClass1 extends SuperClass2 { // constructor public SubClass1() {} // method public void print(){ System.out.println("SubClass1"); } } 4 Question 2 – Solution public class SubClass2 extends SuperClass2 { // constructor public SubClass2() {} // method public void print(){ System.out.println("SubClass2" + number); } } 5 3.Explain the FIFO-structure and the most common methods we can use on the structure. Give an example. Solution: FIFO stands for First In First Out- structure. FIFO means that the data that was added first in the in structure is removed first. Operations: enqueue(data) – adds the data last in the structure Dequeue() - removes the first data in the structure and returns it. peek() - returns the first element in the structure without removing it from the structure isEmpty() - returns true if the structure is empty and false otherwise. 6 3.Explain the FIFO-structure and the most common methods we can use on the structure. Give an example. Example: The number 1 is placed last in the FIFO-structure 2 3 enqueue(1) 2 3 1 7 3.Explain the FIFO-structure and the most common methods we can use on the structure. Give an example. Example: The number 2 is removed from the FIFO-structure 2 3 1 dequeue() 3 1 8 3.Explain the FIFO-structure and the most common methods we can use on the structure. Give an example. Example: Look at the first element in the FIFO-structure and return it 2 3 1 peek() 2 9 3.Explain the FIFO-structure and the most common methods we can use on the structure. Give an example. Example: Test if the FIFO-structure is empty. 2 3 1 isEmpty() false 10 4. What will the program below print out? Explain example 5 and 10 in detail. public class TestRegularExpression { private static void tryReg(String String { if (text.matches(regexp)) { System.out.println(ex + " } else { System.out.println(ex + " } } ex, String regexp, text) matchar."); matchar INTE."); 11 Question 4 public static void main(String[] args) { tryReg("Exempel 1" , "a?b.*", "bbc"); tryReg("Exempel 2" , "(ac)+b", "acacb"); tryReg("Exempel 3" , "ab*c?", "a"); tryReg("Exempel 4" , "ab*c?", "ac"); tryReg("Exempel 5" , "cb*a", "cba"); tryReg("Exempel 6" , "(ba)?b*", "babbbbbb"); tryReg("Exempel 7" , "(ba)?b*", ""); tryReg("Exempel 8" , "((aca)|(bb)).*c.*d?", "acad"); tryReg("Exempel 9" , "((aca)|(bb)).*c.*d?", "bbd"); tryReg("Exempel 10" , "((aca)|(bb)).*c.*d?", "bbacabbccd"); } } 12 Question 4 – Solution tryReg("Exempel 1" , "a?b.*", "bbc"); Printout: Exempel 1 matchar. Explanation: a? b .* - option, a either exists or not - b must exist - something may exist 13 Question 4 – Solution tryReg("Exempel 2" , "(ac)+b", "acacb"); Printout: Exempel 2 matchar. Explanation: (ac)+ b - ac must exist 1 or more times - the string must end with b 14 Question 4 – Solution tryReg("Exempel 3" , "ab*c?", "a"); tryReg("Exempel 4" , "ab*c?", "ac"); Printout: Exempel 3 matchar. Exempel 4 matchar. Explanation: a b* c? - the string must begin with a it must exist 0 or more b option, the string may end with c 15 Question 4 – Solution tryReg("Exempel 5" , "cb*a", "cba"); Printout: Exempel 5 matchar. Explanation: c b* a - the string must begin with c it must exist 0 or more b the string must end with a “cba” starts with c, contains one b and ends with one a → the string matches the regular expression “cb*a” 16 Question 4 – Solution tryReg("Exempel 6" , "(ba)?b*", "babbbbbb"); tryReg("Exempel 7" , "(ba)?b*", ""); Printout: Exempel 6 matchar. Exempel 7 matchar. Explanation: (ba)? b* - option, the string may begin with ba the rest of the string must consist of 0 or more b 17 Question 4 – Solution tryReg("Exempel 8" , "((aca)|(bb)).c*.*d?", "acad"); tryReg("Exempel 9" , "((aca)|(bb)).c*.*d?", "bbd"); tryReg("Exempel 10" , "((aca)|(bb)).c*.*d?", "bbacabbccd"); Printout: Exempel 8 matchar INTE. Exempel 9 matchar INTE. Exempel 10 matchar. Explanation: ((aca) | (bb)) . c* .* d? - the string must begin with aca or bb - the string contains a symbol - the string contains 0 or more c - the string contain may contain something - option, the string may end with d 18 Question 4 – Solution tryReg("Exempel 10" , "((aca)|(bb)).c*.*d?", "bbacabbccd"); Explanation: ((aca) | (bb)) . c* .* d? - the string must begin with aca or bb the string contain a symbol the string contains 0 or more c the string contain may contain something option, the string may end with d The string that matches the regular expression can be split like this: bb-a-c-abbcc-d bb – is fullfilled by ((aca) | (bb)) a – is fullfilled by . c – is fullfilled by c* abbcc – is fullfilled by .* 19 d – is fullfilled by d? 5a. What strings with a length less than 4 match this regular expression: "(ab?)*c*". 5b. Which strings with length less than 4 match this regular expression: "(abc?|ac)*" 5c. Which strings with length less than 4 match this regular expression: "(ad*|b*)++" 20 5a. What strings with a length less than 4 match this regular expression: "(ab?)*c*". Solution: Length 0: “” Length 1: a, c Length 2: aa, ab, ac, cc Length 3: aaa, aab, aba, abc, aac, acc, ccc 21 5b. Which strings with length less than 4 match this regular expression: "(abc?|ac)*" Solution: Length 0: “” Length 1: does not exist Length 2: ab, ac Length 3: abc 22 5c. Which strings with length less than 4 match this regular expression: "(ad*|b*)++" Solution: Length 0: “” Length 1: a, b Length 2: aa, ab, ad, ba, bb Length 3: aaa, aab, aba, aad, ada, adb, bbb, bba, bab, abb, bba, baa, bad 23 6. What do package, public, protected, and private mean? Which visibility has them? Give examples of how they should be used. 24 Question 6 - Solution Solution: package: - The class and its methods and instance variables are only visible to the classes in the same package - Can be used to collect classes which have a common task. Example: The classes Cat and Dog are about animals and can be placed in the package animal if we write this in the beginning of the file: package animal; 25 Question 6 - Solution Solution: Before a class outside the package can gain access to classes in the package, the class must be import the package: import animal.*; 26 Question 6 - Solution Solution: protected The method or the instance variable is available to the suclasses in the same and other packages. protected Should only be used on the methods and instance variables which the classes in the same package and subclasses should have in common. 27 Question 6 - Solution Example: In the package school we have the class Person which contains an instance variable declared with protected. public class Person { protected name = "Kalle"; } 28 Question 6 - Solution Example: In the package school we have the class Person which contains an instance variable declared with protected. public class Person { protected name = "Kalle"; } All classes in the package school and the classes that inherit Person can use name directly without calling getter/setter. 29 Question 6 - Solution Solution: private is used on classes, methods and instance variables that should only be visible to the class itself. 30 Question 6 - Solution Example: Only the class itself should know that the stack is based on a linked list. All other classes should only have to consider how they are going to use the class Stack. public class Stack { private class Node { int data; Node next; Node(E v, Node n) { data = v; next = n; } } } 31 Question 6 - Solution Visibility among members public protected package private Same class YES YES YES YES Class in the same package YES YES YES NO Subclass in a different package YES YES NO NO Not subclass, different package YES NO NO NO 32 7. Write the code for a class that implements the interface(Interface7). The method reverse(int [] array) reverses the array array. } public interface Interface7 { public void reverse(int[] array); 33 Question 7 – Solution public class Uppgift7 implements Inteface7 { // Constructor public Uppgift7(){} // Implement the method public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } 34 Question 7 – Solution – Execution1 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: 4 4 3 1 35 Question 7 – Solution – Execution1 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: 4 i 4 3 1 36 Question 7 – Solution – Execution1 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: swap: 4 4 i 4 3 1 37 Question 7 – Solution – Execution1 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: swap: 4 1 i 4 3 1 Array.length – 1- i 38 Question 7 – Solution – Execution1 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: swap: 4 1 i 4 3 4 Array.length – 1- i 39 Question 7 – Solution – Execution1 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: 1 4 i 3 4 40 Question 7 – Solution – Execution1 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: swap: 4 1 4 i 3 4 41 Question 7 – Solution – Execution1 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: swap: 4 1 3 i 3 4 Array.length – 1- i 42 Question 7 – Solution – Execution1 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: swap: 4 1 3 i 4 4 Array.length – 1- i 43 Question 7 – Solution – Execution1 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: 1 3 4 i 4 44 Question 7 – Solution – Execution1 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: 1 3 4 4 45 Question 7 – Solution – Execution2 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: 1 3 4 5 4 46 Question 7 – Solution – Execution2 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: 1 i 3 4 5 4 47 Question 7 – Solution – Execution2 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: 1 i 3 4 5 4 Array.length – 1- i 48 Question 7 – Solution – Execution2 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: 4 3 i 4 5 1 49 Question 7 – Solution – Execution2 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: 4 3 i 4 5 1 Array.length – 1- i 50 Question 7 – Solution – Execution2 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: 4 5 4 i 3 1 51 Assignment 7 – Solution – Execution 2 public class Uppgift7 implements Inteface7 { public void reverse(int[] array) { for(int i = 0; i < array.length / 2; i++){ int swap = array[i]; array[i] = array[array.length -1 – i]; array[array.length -1 – i] = swap; } } } array: 4 5 4 3 1 52 8. Write the code for the methodpublic boolean isSorted() which returns true if the valuesin a Stack<E> are in an ascending order. The method should also be able to handle the same value in different positions. 53 Uppgift 8 public class Stack<E extends Comparable<E>> { private class Node { E data; Node next; Node(E v, Node n) { data = v; next = n; } } private Node top; 54 Uppgift 8 public void push(E v) { Node newTop = new Node(v,top); top = newTop; } public E pop() { E v = top.data; top = top.next; return v; } } 55 Question 8 – Solution – Execution1 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } } return true; } } 56 Question 8 – Solution – Execution1 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } top data next 3 data next 2 57 Question 8 – Solution – Execution1 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } top data next 3 data next 2 58 Question 8 – Solution – Execution1 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } top data next 3 data next 2 59 Question 8 – Solution – Execution1 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } data return true; top } currentNode: 3 next data next 2 60 Question 8 – Solution – Execution1 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 3 data next 2 61 Question 8 – Solution – Execution1 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 3 data next 2 62 Question 8 – Solution – Execution1 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 3 data next 2 63 Question 8 – Solution – Execution1 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 3 data next 2 64 Question 8 – Solution – Execution1 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 3 data next 2 65 Question 8 – Solution – Execution1 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 3 data next 2 66 Question 8 – Solution – Execution1 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 3 data next 2 67 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } top data next 2 data next 3 68 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } top data next 2 data next 3 69 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } top data next 2 data next 3 70 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 2 data next 3 71 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 2 data next 3 72 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 2 data next 3 73 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 2 data next 3 74 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 2 data next 3 75 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 2 data next 3 76 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 2 data next 3 77 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 2 data next 3 78 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 2 data next 3 79 Question 8 – Solution – Execution2 public boolean isSorted() { if(top == null) return true; else { Node currentNode = top; while(currentNode.next != null) { if(currentNode.data.compareTo( currentNode.next.data) > 0) return false; currentNode = currentNode.next; } return true; } currentNode: top data next 2 data next 3 80