Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Assignment 3: Hashing COMP 2140, Fall 2021 Due: Tuesday 2 November 2021 at 5:00 p.m Instructions • You must complete the “Honesty Declaration” checklist on the course website before you can submit any...

1 answer below »
Assignment 3: Hashing
COMP 2140, Fall 2021
Due: Tuesday 2 November 2021 at 5:00 p.m
Instructions
• You must complete the “Honesty Declaration” checklist on the course website before you can submit
any assignment.
• Assignments must follow the programming standards document published on UM Learn.
• After the due date and time, assignments may be submitted, but will be subject to a late penalty. Please
see the course outline document published on UM Learn for the course policy for late submissions.
• If you make multiple submissions, only the most recent version (the last submission) will be marked.
We strongly encourage you to submit early!
• These assignments are your chance to learn the material for the exams. Code your assignments inde-
pendently. We use software to compare all submitted assignments to each other, and pursue academic
dishonesty vigorously.
• You can get help from the T.A. during your lab section and from the instructors and from the course
Assignment 3 discussion forum on UM Learn. You can discuss general topics related to the assignment
with fellow students or other people, but you cannot discuss the solution to the assignment with them.
You cannot copy code from anywhere except COMP 2140 class notes, unless we tell you otherwise. For a
full discussion of our expectations for individual work on this assignment, see the Department of Compute
Science’s expectations webpage.
• Your Java program must compile and run upon download, without requiring any modifica-
tions. The marker will not fix your program’s problems.
See also the “Hand in” instructions at the end of this assignment
Objective
To execute “programs” in a mini “programming language” (Karon) that has only integer variables, assignment
statements and simple integer expressions.
The Programming Language Karon
Here’s an example Karon program
length = 13
width = 10
area = length * width
extension = 14
fullarea = area + extension
t1 = XXXXXXXXXX
t1 = t1 / 4
t1 = 65 - t1
1
https:
sci.umanitoba.ca/cs/expectations
A Karon program consists of a sequence of assignment statements. Each assignment statement takes up one
line — there are no multi-line assignment statements in Karon. An assignment statement consists of a variable
name (described below), followed by an equals sign (=), followed by the right side of the assignment statement,
which can have one of the following two forms:
1. It can consist solely of one operand, which can be either an integer constant or a variable name.
2. It can consist of an operand, followed by an operator (’+’, ’-’, ’*’, or ’/’ ), followed by a second operand,
where each operand is either an integer constant or a variable name.
As you can see, variable names are strings of length at least one beginning with a letter and containing only
letters and numbers (all letters are lower case). All variables are integer variables.
A variable is “declared” when it first appears on the left side of an assignment statement. In other words,
ecause there are no separate variable declarations, only assignment statements, a variable declaration is an
assignment statement that assigns a value to the variable for the first time. So, the first time a variable appears
should be on the left side of an assignment statement. If a variable appears on the right side of an assignment
statement before it has had a value assigned to it, then we say that the variable is being used before it was
declared and that assignment statement contains an e
or.
For example, consider the following Karon program:
x = 3
= x
x = x + t
t = 36
Variables x and r are properly declared before they are used, but variable t is used before it is declared. Thus,
the assignment statement
x = x + t
is e
oneous.
The purpose of a Karon program is to compute values and store the values in variables. The output of a
Karon program is the values in the variables at the end of the program.
Your Program
You must write a program that reads in and executes Karon programs.
Your program will use a Dictionary to store “variable records”. Each variable record contains a variable
name and its associated value, with the variable name being the key. You will implement the Dictionary as a
hash table. (More information on the hash table below.)
For each assignment statement in a Karon program:
• Your program will read in an assignment statement as a String.
• You must first compute the value of the right side.
– There may be none, one, or two variables on the right side. For example, x = 3 has none, x = 32 +
t has one, and x = t * y has two.
– For each of the variables on the right side, you must find the variable in the Dictionary to retrieve
its value.
– For an integer constant, you can use Integer.parseInt() to retrieve its value.
– If there’s an operator on the right side, then, once you have the values of its operands, you must
perform the operation on the values of the two operands, to get the value of the right side.
• Then you must search the Dictionary for the variable on the left side of the assignment statement.
– If you find the variable, then you change its value to the value you computed for the right side of this
assignment statement. For example, consider the third assignment statement in the following:
2
x1 = 23
y = x1 + 2
y = y * 5
The variable on the left side (y) has already been declared in the previous statement, so it will already
e in the Dictionary. Thus, you will find it in the Dictionary, and simply change the value stored
with it to 125 (the value of the right side).
– If you don’t find the variable in the Dictionary, then this assignment statement is this variable’s
declaration. You must add the new variable (as a variable record) to the Dictionary along with the
value you computed for it on the right side of the assignment statement. For example, consider the
third assignment statement in the following:
x2 = 23
y2 = x2 + 2
z = y2 * 5
The variable on the left side (z) has not yet been declared, so this assignment statement is its
declaration. You will not find it in the Dictionary, so you will have to insert it along with its value
given by the right side (125).
• If a variable on the right side is not in the Dictionary, then it has not been declared before being used, so
you should print an e
or message, abandon this assignment statement, and go on to the next assignment
statement.
When you have executed all the assignment statements in a Karon program, you should print out all the variable
names and their values.
Hash Table:
You must create a Dictionary class that is implemented as a hash table that uses separate chaining to resolve
collisions. The dictionary (an a
ay of Nodes — as described in the pre-recorded videos) should have size N =
79 (which is a prime number). You should use the polynomial hash code for the variable name keys (and use
Horner’s method to compute the polynomial hash code efficiently). You should use the small prime constant
a = 23 for computing the polynomial hash code. Furthermore, for each step of Horner’s method, compute the
value modulo the table size N.
Dictionary Operations:
You must implement the following methods in your Dictionary implementation (hash table):
• A constructor, which creates a new, empty Dictionary (for this hash table implementation, the empty
Dictionary should have size 79).
• search(String variableName), which returns the variable record containing the variable’s name and its
associated value, or null if the variable is not in the Dictionary.
• insert(String variableName, int variableValue), which inserts the given variable and its associated
value into the Dictionary.
• updateValue(String variableName, int newValue), which attempts to find the variable in the Dictionary
and change its value to the given new value. This method should return true if it succeeds in finding the
variable and changing its value, and false if it fails.
• printVariables(), which simply goes through the Dictionary and prints out the variables stored in it.
Each variable should be printed on a separate line containing the variable name followed by its value.
In this hash table implementation, it should simply do a scan through the table, and for each a
ay entry
that is not an empty list, it should print out all the variables and their associated values that are stored
in the linked list.
You may, of course, create other helper methods as needed.
3
The Input:
The input file will contain multiple Karon programs.
The first line of the input file will contain the number of Karon programs contained in the file.
Each Karon program will contain a number of assignment statements, one assignment statement per line.
(There may also be blank lines anywhere in a Karon program; simply ignore them.) You may assume that the
integer constants, variable names, operator and equals sign within an assignment statement are separated from
each other by at least one blank. (There may be multiple blanks between them.)
The end of a Karon program will be indicated by a line containing a ‘Q’ (and no other letters, numbers,
operators, or equals signs). Notice that the ‘Q’ is uppercase, which differentiates it from a Karon variable, which
contains only lowercase letters or numbers.
We will provide a larger input file, called “A3bigData.txt”, a few days before the due date. Your program
must run on that input file. In the meantime, you can use the smaller input file “A3smallData.txt” to get you
code working. Write your own test files, too! (You can find both of these files in the “Assignments” section of
the course web site.)
Your program should prompt the user for the file to use as input — don’t hard-code file names! The markers
should be able to use whatever file they want as input.
The Output:
The output should begin with an appropriate opening message.
Then, for each Karon program, the output should contain an appropriate heading (number the programs),
followed by any e
or messages generated by variables being used before they were declared, followed by a
print-out of the final values of the variables after the program has been executed.
Finally, the output should end with an appropriate closing message.
For example, the output might look like the following:
COMP 2140 Assignment 4: Executing Karon Programs
Karon Program 0
----------------
E
or messages:
Final values of the variables:
temp = 43
answer = 166
Karon Program 1
----------------
E
or messages:
E
or in statement x = t1 * 13: t1 was used before being declared.
Final values of the
Answered Same Day Oct 30, 2021

Solution

Shubham Kumar answered on Oct 31 2021
125 Votes
AkashMehidi/.idea/.gitignore
# Default ignored files
shelf
workspace.xml
# Editor-based HTTP Client requests
httpRequests
# Datasource local storage ignored files
dataSources
dataSources.local.xml
AkashMehidi/.idea/misc.xml




AkashMehidi/.idea/modules.xml






AkashMehidi/.idea
unConfigurations.xml








AkashMehidi/.idea/workspace.xml

















































1635668664664


1635668664664








AkashMehidi/AkashMehidi.iml









AkashMehidi/out/production/AkashMehidi/A3/A3AKASHMEHIDI.class
AkashMehidi/out/production/AkashMehidi/A3/BigData.txt
4
zu = 0
wk = zu
ta = wk
sx = zu * wk
pn = ta - sx
md = sx + pn
iq = zu - md
fg = iq + 0
t = 5 - 5
zv = 1
wl = zv + zu
tb = zv * wl
sy = wl / t
po = sy - bt
me = po * sy
ir = me - pn
fh = ir * me
u = fh * i
zw = 2
wm = 78 - 76
tc = bt + wm
sz = tc / fh
pp = sz
mf = pp - iq
is = mf / zv
fi = mf + ta
v = zw * bu
zx = 3
wn = zx * t
td = wn + pn
pq = td
mg = 79 - 76
it = pp + md
fj = it / sy
w = fj + fg
zt = 78
wj = 78
sw = 52 + 26
pm = 156 / zw
mc = pm / wl
lz = wj
ip = mg * 16
ff = 78
s = ff + bt
zs = 77
wi = bs - bu
sv = 7 * 11
pl = wi / fh
mb = pl + zu
ly = 77
io = ly - zu
fe = io
= fe * zv
zr = 76
wh = 76
su = 76
pk = 38 * zw
ma = ff - zw
pk = 75 + wl
lx = tb + 75
in = lx
fd = z
q = fd
Q
helen = cameron
mike = domaratzki
domaratzki = mike + mike
cameron = helen * helen
onlyone = 1
Q
abcdefghijklmnopqrstuvwxyz = 26
a0123456789 = 11
a0b1c2d3e4f5g6h7i8j9 = 20
aaaaaaaaaa = 1001
zzzzzzzzzz = 1026
pneumonoultramicroscopicsilicovolcanoconiosis = 45
pseudopseudohypoparathyroidism = 30
floccinaucinihilipilification = 29
antidisestablishmentarianism = 28
a = 2001
z = 2026
uncopyrightable = pneumonoultramicroscopicsilicovolcanoconiosis / 2
milen = aaaaaaaaaa + zzzzzzzzzz
Q
lotsofblanks = 66 * 3
gap2 = 2 / 1
Q
AkashMehidi/out/production/AkashMehidi/A3/Dictionary.class
AkashMehidi/out/production/AkashMehidi/A3/Dictionary.txt
package A3;
import java.util.A
ayList;
class Node {
String key;
int value;
public Node(String key, int value) {
this.key = key;
this.value = value;
}
public String print() {
String str = "Key : " + key + "\tValue : " + value;
return str;
}
}
public class Dictionary {
A
ayListayList data;
public Dictionary() {
data = new A
ayList
();
while(data.size()<79)
data.add(new A
ayList());
}
public Node search(String str) {
int hashValue = calculateHash(str);
A
ayList cu
List = data.get(hashValue);
for(int i=0; iList.size(); i++) {
if(cu
List.get(i).key.equals(str))
return cu
List.get(i);
}
return null;
}
public void insert(String str, int value) {
int hashValue = calculateHash(str);
data.get(hashValue).add(new Node(str, value));
}
public void update(String str, int value) {
int hashValue = calculateHash(str);
for(int i=0; i if(data.get(hashValue).get(i).key.equals(str)) {
data.get(hashValue).get(i).value = value;
}
}
}
public void print() {
for(int i=0; i for(int j=0; j System.out.println(data.get(i).get(j));
}
}
}
public int calculateHash(String str) {
int p = 23;
int m = 79;
long power_of_p = 1;
long hash_val = 0;
for(int i=0; i hash_val = (hash_val + (str.charAt(i) - 'a' + 1) * power_of_p) % m;
power_of_p = (power_of_p * p) % m;
}
return (int)hash_val;
}
}
AkashMehidi/out/production/AkashMehidi/A3/Node.class
AkashMehidi/out/production/AkashMehidi/A3/SmallData.txt
4
length = 13
width = 10
area = length * width
extension = 14
fullarea = area + extension
t1 = 13 + 63
t1 = t1 / 4
t1 = 65 - t1
Q
x = 3
= x
x = x + t
t = 36
Q
x = 23
y = x + 2
y = y * 5
Q
x2 = 23
y2 = x2 + 2
z = y2 * 5
Q
AkashMehidi/out/production/AkashMehidi/A3/tempData.txt
1
x = 3
= x
x = x +...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here