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; }


Most Viewed Questions Of Java

Ques 6 Your goal is to navigate a robot out of a maze. The robot starts in the center of the maze facing north. You can turn the robot to face north, east, south, or west. You can direct the robot to move forward a certain distance although it will stop after hitting a wall. a) Formulate this problem. This means you will have to describe initial state, goal test,successor function. and cost function. Successor function is a description of the robots successive actions after the initial state. We’ll define the coordinate system so that the center of the maze is at (0, 0), and the maze itself is a square from (-1,–1) to (1, 1)


5. (P31,In modern packet-switched networks, including the Internet, the source host segments long, application layer messages (for example, an image or a music file) into smaller packets and sends the packets into the network. The receiver then reassembles the packets back into the original message. We refer to this process as message segmentation. The figure below illustrates the end-to-end transport of a message with and without message segmentation. Consider a message that is 8 * 106 bits long that is to be sent from source to destination in the figure. Suppose each link in the figure is 2 Mbps. Ignore propagation, queuing, and processing delays. (a) Consider sending the message from source to destination without message segmentation. How long does it take to move the message from the source host to the first packet switch? Keeping in mind that each switch uses store-and-forward packet switching, what is the total time to move the message from source host to destination host? Now suppose that the message is segmented into 800 packets, with each packet being 10,000 bits long. How long does it take to move the first packet from source host to the first switch? When the first packet is being sent from the first switch to the second switch, the second packet is being sent from the source host to the first switch. At what time will the second packet be fully received at the first switch? c) How long does it take to move the file from source host to destination host when message segmentation is used? Compare this result with your answer in part (a) and comment. ) In addition to reducing delay, what are reasons to use message segmentation? e) Discuss the drawbacks of message segmentation. In addition to reducing delay, what are reasons to use message segmentation? -Discuss the drawbacks of message segmentation.


. [10 points]Suppose a process in the Host C has a UDP socket with port number 6789. Suppose both Host A and Host B each send a UDP segment to Host C with destination port number 6789. Will both of these segments be directed to the same socket at Host C? If so, how will the process at Host C know that these two segments originated from two different hosts?


A particular system is controlled by an operator through commands entered from a keyboard.The average number of commands entered in an 8-hour interval is 60. Show your work toreceive full credit. a. Suppose the CPU scans the keyboard every 100ms. How many times will the keyboard bechecked in an 8-hour period? b. By what fraction would the number of CPU visits to the keyboard be reduced if interrupt-driven 1/0 were used?


22. Many CPU-scheduling algorithms are parameterized. For example, the RR algorithm requires a parameter to indicate the time slice. Multilevel feedback queues require parameters to define the number of queues, the scheduling algorithms for each queue, the criteria used to move processes between queues, and so on. These algorithms are thus really sets of algorithms (for example, the set of RR algorithms for all time slices, and so on). One set of algorithms may include another (for example, the FCFS algorithm is the RR algorithm with an infinite time quantum). What (if any) relation holds between the following pairs of algorithm sets? a. Priority and SJF b. Multilevel feedback queues and FCFS c. Priority and FCFS


23. Suppose that a CPU scheduling algorithm favors those processes that have used the least processor time in the recent past. Why will this algorithm favor I/O-bound programs and yet notpermanently starve CPU-bound programs?


1) In a computer instruction format, the instruction length is 11 bits and the size of an address field is 4 bits. Is it possible to have: 5 two-address instructions 45 one-address instructions 32 zero-address instructions using the specified format? Justify your answer. b) Assume that a computer architect has already designed 6 two-address and 24 zero address instructions using the instruction format above. What is the maximum number of one-address instructions that can be added to the instruction set?


(1) In a computer system, memory operations currently take up 30% of execution time. A new gadget called a cache (i.e. an L1 cache) speeds-up 80% of the memory operations by a factor of 4. What is the speed-up due to the cache? ) [5] A second new gadget called an L2 cache speeds-up half the remaining20% of the memory operations by a factor of 2. What is the total speed-up with the L1 and L2 cache together?


client, and there is no other traffic on this path. Assume each packet of size L bits, and both links have the samepropagation delay dprop- (a) What is the packet inter-arrival time at the destination? That is, how much time elapses from when the lastbit of the first packet arrives until the last bit of the second packet arrives? (b) Now assume that the second link is the bottleneck link (i.e., RcjRs). Is it possible that the second packetqueues at the input queue of the second link? Explain. Now suppose that the server sends the second packetT seconds after sending the first packet. How large must T be to ensure no queuing before the second link?Explain. Consider the figure (a) below. Assume that we know the bottleneck link along the path from the server to theclient is the first link with rate Rs bits/sec. Suppose we send a pair of packets back to back from the server to the


Let a denote the rate of packets arriving at a link in packets/sec, and let µ denote the links transmission rate in packets/sec. Based on the formula for the total delay (i.e., the queuing delay plus the transmission delay) derived in the previous problem, derive a formula for the total delay in terms of a and µ. Note that the link's transmission rate in packet/sec is R/L.