Buy here:
http://student.land/college-of-computing-and-informatics-
assignment-4/
COLLEGE OF COMPUTING AND INFORMATICS
Assignment – 4
Course Title : Computer programming II Course Code : CS141
Important Notes: Submission Deadline: 28/04/2016, 23:59 Marks: 5
Copied Work: ZERO Late Submission: ZERO Question 1: (1 Mark)
Read about the following real life situations and decide whether
you will use a stack or a queue.
Provide valid reason of your choice.
1)
2)
3)
4)
5) A car wash facility.
Batteries in Flashlight.
Pringles (pack of chips).
Customers at Al-Baik.
Execution of a recursive method. Question 2: (1 Mark)
What is Set? Write Java code for the following operations:
1) Construct the Set 2) Add and remove an element from a HashSet
3) Test whether an element is contained in the set 4) Visiting all
Elements with an Iterator Page 1 of 3 Question 3: (1 Mark)
Suppose you have the following information about courses and the
number of students in each
course.
CS140
CS141
IT407
IT101
IT244
1234- 549
256
120
636
210 Write a program that store these information in a map
Remove the course IT407 with its associated value
Edit the number of students in CS140 to 433
Print all keys with their values as the following format: Question 4:
(2 Marks)
Create a class called StringHashCode which besides a main
method provides the following
method:
public static int strHashCode(String str)
This method should return the integer hash code of the string
parameter str using the formula
given below:
Hashcode = str[0]*31^(n-1) + str[1]*31^(n-2) + ... + str[n-2] *31^(1) +
str[n-1]
Here str[0] means the first character in str and str[1] means second
character of str and so on.
With n we denote the length of the string str, * means multiplication
and ^ represents the power
(exponent).
For example if str = “abc”, then we have: hashcode = ‘a’*31^2 +
‘b’*31^1 + ‘c’
Note: Using some character such as ‘b’ in arithmetic calculations
automatically replaces it with
its ASCII code. That is, you don’t need to worry about it, java will
handle it automatically.
In the main method of your class, write code to ask the user to
enter a string, then pass this string
input to the method strHashCode and print the hash code returned
by the method on the monitor
screen. Then, your program should print to the user: Do you want
to enter a new string (Y/N).
Based on the use choice, program should proceed.
Page 2 of 3 Following is an example run of the program:
Enter a string: Adam
Hash code for Adam is: 2035631
Do you want to enter a new string (Y/N): y
Enter a string: Eve
Hash code for Eve is: 70068
Do you want to enter a new string (Y/N): Y
Enter a string: SEU
Hash code for SEU is: 81987
Do you want to enter a new string (Y/N): n
Thank you. Exiting program execution... Page 3 of 3

COLLEGE OF COMPUTING AND INFORMATICS Assignment – 4

  • 1.
    Buy here: http://student.land/college-of-computing-and-informatics- assignment-4/ COLLEGE OFCOMPUTING AND INFORMATICS Assignment – 4 Course Title : Computer programming II Course Code : CS141 Important Notes: Submission Deadline: 28/04/2016, 23:59 Marks: 5 Copied Work: ZERO Late Submission: ZERO Question 1: (1 Mark) Read about the following real life situations and decide whether you will use a stack or a queue. Provide valid reason of your choice. 1) 2) 3) 4) 5) A car wash facility. Batteries in Flashlight. Pringles (pack of chips). Customers at Al-Baik. Execution of a recursive method. Question 2: (1 Mark) What is Set? Write Java code for the following operations: 1) Construct the Set 2) Add and remove an element from a HashSet 3) Test whether an element is contained in the set 4) Visiting all Elements with an Iterator Page 1 of 3 Question 3: (1 Mark) Suppose you have the following information about courses and the number of students in each course. CS140 CS141 IT407
  • 2.
    IT101 IT244 1234- 549 256 120 636 210 Writea program that store these information in a map Remove the course IT407 with its associated value Edit the number of students in CS140 to 433 Print all keys with their values as the following format: Question 4: (2 Marks) Create a class called StringHashCode which besides a main method provides the following method: public static int strHashCode(String str) This method should return the integer hash code of the string parameter str using the formula given below: Hashcode = str[0]*31^(n-1) + str[1]*31^(n-2) + ... + str[n-2] *31^(1) + str[n-1] Here str[0] means the first character in str and str[1] means second character of str and so on. With n we denote the length of the string str, * means multiplication and ^ represents the power (exponent). For example if str = “abc”, then we have: hashcode = ‘a’*31^2 + ‘b’*31^1 + ‘c’ Note: Using some character such as ‘b’ in arithmetic calculations automatically replaces it with its ASCII code. That is, you don’t need to worry about it, java will handle it automatically. In the main method of your class, write code to ask the user to enter a string, then pass this string input to the method strHashCode and print the hash code returned by the method on the monitor
  • 3.
    screen. Then, yourprogram should print to the user: Do you want to enter a new string (Y/N). Based on the use choice, program should proceed. Page 2 of 3 Following is an example run of the program: Enter a string: Adam Hash code for Adam is: 2035631 Do you want to enter a new string (Y/N): y Enter a string: Eve Hash code for Eve is: 70068 Do you want to enter a new string (Y/N): Y Enter a string: SEU Hash code for SEU is: 81987 Do you want to enter a new string (Y/N): n Thank you. Exiting program execution... Page 3 of 3