Professional Documents
Culture Documents
CS141-Assignment - 2-Question-File3
CS141-Assignment - 2-Question-File3
Computer Programming - II
CS141
Assignment 2
Deadline: Sunday 04/04/2021 @ 23:59
[Total Mark for this Assignment is 5]
Student Details:
Name: ID:
CRN:
Instructions:
You must submit two separate copies (one Word file and one PDF file) using the Assignment Template on
Blackboard via the allocated folder. These files must not be in compressed format.
It is your responsibility to check and make sure that you have uploaded both the correct files.
Zero mark will be given if you try to bypass the SafeAssign (e.g. misspell words, remove spaces between words,
hide characters, use different character sets or languages other than English or any kind of manipulation).
Email submission will not be accepted.
You are advised to make your work clear and well-presented. This includes filling your information on the cover
page.
You must use this template, failing which will result in zero mark.
You MUST show all your work, and text must not be converted into an image, unless specified otherwise by the
question.
Late submission will result in ZERO mark.
The work should be your own, copying from students or other resources will result in ZERO mark.
Use Times New Roman font for all your answers.
Pg. 1 Question FourQuestion Four
Learning 2 Marks
Outcome(s): Question One
CLO3
Use the following method to generate an array (arr1) of 5000 randomly
Instructors: generated elements.
Design and
implement public static int[] RandomArrayGenerator(int n)
{
programs using
int[] Array = new int[n];
object oriented Random r = new Random();
programming for(int i=0;i<n;i++)
concepts such as {
encapsulation, Array[i] = r.nextInt(1000);
inheritance, }
return Array;
polymorphism,
}
abstract classes
and methods.
[Note that by using nextInt(100) you will generate an integer between 0 and
100.]
solution:
//Sorting.java
import java.util.Arrays;
import java.util.Random;
output
Pg. 4 Question FourQuestion Four
1 Mark
Learning
Outcome(s): Question Two
CLO2
Instructors:
Suppose you have the following array: {11, 22, 33, 44, 55, 66, 77}. Write a java
Apply recursion program to find the sum of elements of the array recursively.
concept in
programming. Solution:
class Test {
static int arr[] = { 1, 2, 3, 4, 5 };
// Return sum of elements in A[0..N-1]
// using recursion.
static int findSum(int A[], int N)
{
if (N <= 0)
return 0;
return (findSum(A, N - 1) + A[N - 1]);
}
// Driver method
public static void main(String[] args)
{
System.out.println(findSum(arr, arr.length));
}
}
Pg. 6 Question FourQuestion Four
0.5 Marks
Learning
Outcome(s): Question Three
CLO4
Instructors:
Suppose a program builds and manipulates a linked list:
Demonstrate
dynamic data What two special nodes would the program typically keep track of? Describe
structures such
two common uses for the null reference in the node of the linked lists.
us linked lists,
stacks and Answer:
queues, and
binary trees. Step1:
A linked List is a linear data structure where the elements are not stored in a contiguous
location and every element is a separate object with a data part and address part .
Here the two special nodes that the program keep the track of are
Without a dummy head (refer to the top singly linked list in the following diagram), or
With a dummy head (bottom one). Dummy headers are often used because they help
with the implementation.
Pg. 8 Question FourQuestion Four
1.5 Marks
Learning
Outcome(s): Question Four
CLO4
Instructors:
Consider the following Java Code:
Demonstrate
What is the output when this code is executed?
dynamic data
structures such import java.util.LinkedList;
us linked lists, import java.util.ListIterator;
class Main {
stacks and
public static void main(String[] args) {
queues, and LinkedList<String> myLList = new LinkedList<String>();
binary trees. myLList.addFirst("Ali");
myLList.addFirst("Omar");
myLList.addLast("Sara");
ListIterator<String> myiterator = myLList.listIterator();
myiterator.next();
myiterator.next();
myiterator.add("Reem");
myiterator.previous();
myiterator.add("Ahmad");
myiterator.add("Khaled");
myiterator.previous();
myiterator.remove();
System.out.println(myLList);
myiterator.next();
myiterator.next();
System.out.println("MyList Backward iteration :");
while(myiterator.hasPrevious())
System.out.println(myiterator.previous());
System.out.println("---------------------");
}
}
Solution
This will be the result
Pg. 9 Question FourQuestion Four