Assignment 1 - In Javascript we are going to create a Class called Chain. If you
ID: 668077 • Letter: A
Question
Assignment 1 - In Javascript we are going to create a Class called Chain. If you need a little primer on creating objects and classes in Javascript see http://www.w3schools.com/js/js_object_definition.asp - if you know your object oriented code this will be relatively straightforward. Your Chain will be made up of Links. Each link will have an id, a value (String) and a pointer to the next link ( NextLink ) - which is simply a pointer to the id of the next link in the Chain. Please note that this is a very inefficient representation of the Chain. Here is a simple Javascript code definition of a Link for you to use. https://jsfiddle.net/reaglin/q46obht6/
You will make a Chain consisting of 5 links. You will also create a print function for the chain that will print the links in link order. You may need to create some functions to support this functionality.
Assignment 2 - Implement a Stack computer in Javascript (you will turn in a link to your program in JSFiddle). This is a simple computer that keeps a stack, when a number is entered it goes onto the top of the stack. When an operation is entered, the previous 2 numbers are operated on by the operation.
For example
2 [enter] 2
5 [enter] 5 2
* [enter] * 5 2 -> collapses to 10
would leave at 10 at the top of the stack.
The program should use a simple input box, either a text field or prompt and display the contents of the Stack.
Assignment 3
You are going to create a List. You will then fill it with numbers consecutively numbered from 2 to n where n is entered by the user.
Create a second List - this one should be a Queue - it is empty.
Once you have the first List filled we are going to use a technique called Sieve of Eratosthenes which uses first queue to fill the second queue. You will need to look at the algorithm for this https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Here is the simple method to do this - L1 is the first list, Q1 is the Queue.
1. Go to 1st element in L1 (which will be 2).
2. Because it exists - Push this into Q1 and remove it from L1
3. Iterate through each element of L1 and if the value is divisible by 2 remove it.
4. When done go back to the beginning of the list (the value will be 3 the second time around)
5. Print Iteration 1, Value of L1 (all elements) and Q1 (all elements)
6. Repeat steps 2-5 with this new element - repeat until L1 is empty.
Sample output with input 10
Iteration 0: L1 = 2 3 4 5 6 7 8 9 10, Q1 = ,
Iteration 1: L1 = 3 5 7 9, Q1 = 2
Iteration 2: L1 = 5 7, Q1 = 2 3
Iteration 3: L1 = 7, Q1 = 2 3 5
Iteration 4: L1 = , Q1 = 2 3 5 7
Explanation / Answer
Assignment 1
Class
It defines the object's characteristics. A class is a template definition of an object's properties and methods.
Object
An instance of a class.
JavaScript Object constructor property, our Class() function can be considered to be a Class constructor in that it literally constructs a class from the passed object argument. In a nutshell, the function creates an inner object called "class". It has one method called initialize(), which acts like a typical OOp constructor method. you pass it arguments and it assigns those values to its properties. The for loop iterates through the passed methods object and adds them to the class's prototype so that they will be shared by every instance of the class. Finally, a default constructor is created if there are none defined within the methods object . The class constructor function is returned to the caller
Our class defines a two argument initialize constructor as well as an overridden toString(), inherited from the base Object. After creating a new instance, we can access its properties:
Assignment :
A stack is an ordered collection of items that follows the LIFO (short for Last In First Out) principle. The addition of new items or the removal of existing items takes place at the same end. The end of the stack is known as the top and the opposite is known as the base. The newest elements are near the top, and the oldest elements are near the base.
Creating a stack
We are going to create our own class to represent a stack. Let's start from the basics and declare our class:
First, we need a data structure that will store the elements of the stack. We can use an array to do this:
Next, we need to declare the methods available for our stack:
The first method we will implement is the push method. This method will be responsible for adding new elements to the stack with one very important detail: we can only add new items to the top of the stack, meaning at the end of the stack. The push method is represented as follows:
As we are using an array to store the elements of the stack, we can use the push method from the JavaScript array class.
Next, we are going to implement the pop method. This method will be responsible for removing the items from the stack. As the stack uses the LIFO principle, the last item that we added is the one that is removed. For this reason, we can use the pop method from the JavaScript array class. The pop method is represented as follows:
With the push and pop methods being the only methods available for adding and removing items from the stack, the LIFO principle will apply to our own Stack class.
Now, let's implement some additional helper methods for our class. If we would like to know what the last item added to our stack was, we can use the peek method. This method will return the item from the top of the stack: