Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Module 3 - Lists Lists are one of the most fundamental of the data types. Your a

ID: 669667 • Letter: M

Question

Module 3 - Lists

Lists are one of the most fundamental of the data types. Your ability to understand lists as an Abstract Data Type (ADT) and implement lists is key to your understanding of all the more complex data structures. In this module you should first completely review Topic - Basic Data structures which covers lists. Once complete you should be ready to tackle some of the challenging assignments available in this module.

Assignment - In Javascript we are going to create a Class called Chain. If you need a little primer on creating objects and classes in Javascript seehttp://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.

Explanation / Answer

public class Currency
{
private int amount;
public Currency(int amt)
{
this.amount=amt;
}
public int getAmount()
{
return this.amount;
}
}
public interface DispenseChain
{
void setNextChain(DispenseChain nextChain);
void dispense(Currency cur);
}
public class Dollar50Dispenser implements DispenseChain
{
private DispenseChain chain;
public void setNextChain(DispenseChain nextChain)
{
this.chain=nextChain;
}
public void dispense(Currency cur)
{
if(cur.getAmount() >= 50)
{
int num = cur.getAmount()/50;
int remainder = cur.getAmount() % 50;
System.out.println("Dispensing "+num+" 50$ note");
if(remainder !=0) this.chain.dispense(new Currency(remainder));
}
else
{
this.chain.dispense(cur);
}
}
}
Dollar20Dispenser.java
public class Dollar20Dispenser implements DispenseChain
{
private DispenseChain chain;
public void setNextChain(DispenseChain nextChain)
{
this.chain=nextChain;
}
public void dispense(Currency cur)
{
if(cur.getAmount() >= 20)
{
int num = cur.getAmount()/20;
int remainder = cur.getAmount() % 20;
System.out.println("Dispensing "+num+" 20$ note");
if(remainder !=0) this.chain.dispense(new Currency(remainder));
}
else
{
this.chain.dispense(cur);
}
}
}
Dollar10Dispenser.java
public class Dollar10Dispenser implements DispenseChain
{
private DispenseChain chain;
public void setNextChain(DispenseChain nextChain)
{
this.chain=nextChain;
}
public void dispense(Currency cur)
{
if(cur.getAmount() >= 10){
int num = cur.getAmount()/10;
int remainder = cur.getAmount() % 10;
System.out.println("Dispensing "+num+" 10$ note");
if(remainder !=0) this.chain.dispense(new Currency(remainder));
}
else
{
this.chain.dispense(cur);
}
}
}
import java.util.Scanner;
public class ATMDispenseChain
{
private DispenseChain c1;
public ATMDispenseChain()
{
this.c1 = new Dollar50Dispenser();
DispenseChain c2 = new Dollar20Dispenser();
DispenseChain c3 = new Dollar10Dispenser();
c1.setNextChain(c2);
c2.setNextChain(c3);
}
public static void main(String[] args)
{
ATMDispenseChain atmDispenser = new ATMDispenseChain();
while (true)
{
int amount = 0;
System.out.println("Enter amount to dispense");
Scanner input = new Scanner(System.in);
amount = input.nextInt();
if (amount % 10 != 0)
{
System.out.println("Amount should be in multiple of 10s.");
return;
}
atmDispenser.c1.dispense(new Currency(amount));
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote