Search for question
Question

COM139: Software Development II CRN: 44949 (AY 2023/2024 Semester 2) Module Coordinator: Dr Dermot Kerr Assessment Overview and Schedule: This coursework is Class Exercise 2. Description Implement your own solutions to programming problems in the following pages. There are 10 questions in total worth 20 marks. You can develop your solutions using IntelliJ project provided which contains the unit tests required to ensure your solution is correct or you can build your own project in your preferred IDE using the method headers provided below. Submission of solution Note you must submit your IntelliJ project files as a zip archive to the submission area on BBL. Class Exercise 2 (20 marks) This coursework will demonstrate the application of your programming knowledge to date using methods, arrays, sorting, searching and other techniques. Using the starter code complete the methods so that each method produces the desired functionality as specified in the method description. 1. Complete the method int[] arrayMerge(int[] 11, int [] 12). This method takes as input 2 arrays which are concatenated (joined) and returns a new array. Your solution should work for any size of arrays. (2 marks for correctly functioning method). E.g., using these arrays [1,2,3,4,5] and [-6,7,8,9,0,3] the resulting array should be [1,2,3,4,5,-6,7,8,9,0,3] public static int[] arrayMerge (int[] l1, int [] 12) 2. Complete the method swap(int[] array, int posA, int posB). This method swaps two values in an int array at index posA and posB. Input parameters are int array of values and positions of elements to be swapped. Method returns void. (2 marks for correctly functioning method). public static void swap(int[] array, int posA, int posB) 3. Complete the method bubbleSort (int[] array). This method sorts an integer array using the bubblesort algorithm. Input parameter is an int array of values to be sorted. Method returns void. (2 marks for correctly functioning method). public static void bubbleSort(int[] array) 4. Complete the method selectionSort(int[] array). This method sorts an integer array using the selection sort algorithm. Input parameter is an int array of values to be sorted. Method returns void. (2 marks for correctly functioning method). public static void selectionSort(int array[]) 5. Complete the method binarySearch (int[] array, int key). This method searches an integer array using the provided key. Input parameter is an int array of values to be sorted, and int key value to be searched. Method returns an int indicating the index of the item or -1 if the item is not found. Add any helper methods you need to achieve the desired result. Remember, you will need to sort the array first using the selection sort or bubble sort. You can add the sort call to the main method if you want to sort the array. (2 marks for correctly functioning method). public static int binarySearch(int array[], int key) { 6. Complete the method linearSearch(int[] array, int key). This method searches an integer array using the provided key. Input parameter is an int array of values to be sorted, and int key value to be searched. Method returns an int indicating the index of the item or -1 if the item is not found. (2 marks for correctly functioning method). public static int linearSearch(int array[], int key) { 7. Complete the method meanArrayValue(int[] array). This method computes the mean (average) from an array. Input is an int array, output is a double value representing the computed mean (average). (2 marks for correctly functioning method). public static double meanArrayValue(int[] array) { 8. Complete the method medianArrayValue(int[] array). This method computes the median (middle value) from an (ordered) array. Input is an int array, output is a double value representing the computed median. Hint: Sort the array first, the median is then the middle value. If the middle value is 2 numbers then you should average them. Add any helper methods you need to achieve the desired result. You can add the sort call to the main method if you want to sort the array. (2 marks for correctly functioning method). public static double medianArrayValue(int[] array) { 9. Complete the method modeArrayValue(int[] array). This method computes the mode (most occurring value) from an array. Input is an int array, output is an int value representing the mode value. Add any helper methods you need to achieve the desired result. Do not modify any existing code in the program. (2 marks for correctly functioning method). public static int modeArrayValue(int[] array) { 10. The code provided below contains several errors including, 1 compile time error, 1 runtime error and 2 out of bounds exceptions. Run the code, read the console output and use the supplied unit test to help fix the errors - note code must compile for unit test to pass. (2 marks for correctly passing unit test). class Main { public static void main(String[] args) { } int[] [] grid = createBoard(5); // create 5x5 game board initaliseBoard(grid); // initialise board addShip(grid, 1, 1); // add ship at position 1,1 addShip(grid, 2, 3); // add ship at position 2,3 addShip (grid, 4, 3); // add ship at position 4,3 System.out.println("Starting board"); printBoard(grid); // display current game board // Launch torpedo at ship position 4,3 System.out.println("Attack 4,3:" + sinkShip(grid, 4, 3)); printBoard(g); // display updated game board public static int[] [] createBoard (int boardSize) { int[] [] grid = new int[boardSize+1] [boardSize+1]; return grid; } public static void initaliseBoard(int[] [] grid) { for (int row = 0; row < grid.length+1; row++) for (int col = 0; col < grid[0].length+l; col++) { grid[row][col] = 0; // initialise board } } public static void printBoard (int[] [] grid) { for (int r = 0; r < grid.length; r++) { for (int c = 0; c < grid[0].length; c++) { if (grid[r] [c] 0) // no ship System.out.print(" ~"); == else if (grid[r] [c] else == 1) // ship found System.out.print(" V"); } } System.out.print(" x"); System.out.println(); //Print new line at end of row public static boolean shipAt (int[] [] grid, int r, int c) { return (grid[r][c] } == 1); public static void addShip (int[] [] grid, int r, int c) { if (!shipAt(grid, r, c)) { grid[r] [c] = 1; } } public static boolean sinkShip (int[] [] grid, int r, int c) { if (shipAt (grid, r, c)) { } grid[r] [c] = −1; // −1 indicates sunken ship return true; else { } return false; }