M180: Data Structure and Algorithms in Java

1 M180: Data Structure and Algorithms in Java Tutor-Marked Assignment (Fall 2014/2015) Cut-Off Date: 23-11-2014 Total Marks: 40 Contents Warnings and Declaration…………………………………………………………………………..……..1 Question 1……………..……………………….………………………………………………………….2 Question 2 ………………..…………………………………………………………………………….….2 Question 3 ………….………..………………………………………………………………………….….3 Question 4 ………….…………..……………………………………………………………………….….4 Question 5 ………….…………..……………………………………………………………………….….4 Plagiarism Warning: As per AOU rules and regulations, all students are required to submit their own TMA work and avoid plagiarism. The AOU has implemented sophisticated techniques for plagiarism detection. You must provide all references in case you use and quote another person's work in your TMA. You will be penalized for any act of plagiarism as per the AOU's rules and regulations. Declaration of No Plagiarism by Student (to be signed and submitted by student with TMA work): I hereby declare that this submitted TMA work is a result of my own efforts and I have not plagiarized any other person's work. I have provided all references of information that I have used and quoted in my TMA work. Name of Student:……………………………….. Signature:…………………………………………... Date:…………………………………………………… Arab Open University

2 Question 1: (6 marks) Write the Java code of the method Multiply2D that takes two 2-dimentional arrays (square matrices of same dimensions) A and B and returns another 2-dimentional array (matrix) C that contains the multiplication of the two matrices A and B. For example, if A and B are as follows: 1 2 3 1 2 3 1 2 3 A B C Question 2: (8 marks) You are given a Stack class. The following functions are available for use: public class Stack { public boolean isEmpty(){}; public void push(int n){}; public int peek(){}; public int pop(){}; } Write a method MaxStack that takes a (Stack S) of integers, as input and returns the largest element in the stack, and the stack should finally contains the same initial elements of the stack and in the same order except removing the largest element only. You are only allowed to use Stack object in your method For example, if the stack has the following items Then, it returns 66 and the stack should contain after calling the method 4 5 5 2 3 4 1 2 2 11 17 19 11 17 19 11 17 19 5 7 2 66 23 7 4 5 7 2 23 7 4

4 Question 4: (8 marks) Given a Node P in a list of nodes (linked list), as shown in the figure below. Draw the results of the following statements, applied in the same order below; be sure to show the position of P in each step: