you will be implementing an immutable linked list to store atoms from the period
ID: 3847388 • Letter: Y
Question
you will be implementing an immutable linked list to store atoms from the periodic table. This will require you to create three classes. You may not use any built-in Collections objects.
You will implement all three classes in one Java file (see the file upload section below for instructions on how to do this). When you are finished, upload your .java file using the upload form below.
Flag this Question
Atom
Let’s begin with the Atom class. The atom class should be a simple class containing two piece of data: an atom’s name (a string) and atomic number (an integer). These must both be immutable. It should have a constructor that takes a string and an integer, and a toString method, so that if you create an element and print it out like this:
it should return the following result:
Atom Specifications:
Returns a String representing Atom. Examples:
hydrogen (1)
helium (2)
carbon (12)
Flag this Question
ImmutableListNode:
The immutable list node class should implement a simple linked list node class. It should contain a pointer to the next item in the list, and data (in this case, an Atom object). The data stored by this node should be immutable (once a list node is created, it always points to the same object). This class should also have one constructor that takes an Atom object as an argument.
ImmutableListNode Specifications:
Flag this Question
AtomicList:
This class implements an immutable linked list using the ImmutableListNode class to store underlying Atom objects. The AtomicList should have a pointer to the front of the list, and a default constructor. The AtomicList should also have a toString method that in turn calls the toString method of each Atom in the list. If you create an AtomicList like this:
It should print this:
AtomicList Specifications:
Returns a String representation of the AtomicList object. Examples:
[lithium (3), carbon(12), hydrogen (1), oxygen (16)]
[hydrogen (1), potassium (19), magnesium (12)]
Flag this Question
Define all three classes in a single file, by removing the "public" keyword from all but one of them. Follow this example:
field: name Name of atom (should be immutable) field: atomicNumber Atomic number of atom (should be immutable) constructor: Atom(name, atomicNumber) Atom constructor method: toString()Returns a String representing Atom. Examples:
hydrogen (1)
helium (2)
carbon (12)
Explanation / Answer
import java.util.LinkedList;
class Atom {
String name;
int atno;
Atom(String name,int atno)
{
this.name=name;
this.atno=atno;
}
String tostring()
{
return name+"("+atno+")";
}
}
class ImmutableListNode
{
ImmutableListNode next;
Atom data;
ImmutableListNode(Atom dat)
{
data=dat;
}
}
class AtomicList
{
static ImmutableListNode head;
AtomicList()
{
}
AtomicList(Atom dat)
{
head = new ImmutableListNode(dat);
}
public void add(Atom dat)
{
if(head==null)
{
ImmutableListNode temp = head;
head = new ImmutableListNode(dat);
head.next = temp;
return;
}
ImmutableListNode temp = head;
while(temp.next != null)
{
temp = temp.next;
}
temp.next = new ImmutableListNode(dat);
}
void tostring()
{
ImmutableListNode temp = head;
System.out.println("[");
while(temp != null)
{
System.out.println(temp.data.tostring()+",");
temp = temp.next;
}
System.out.println("]");
}
public static void main(String[] args) {
Atom li = new Atom("lithium",3);
Atom c = new Atom("carbon",12);
Atom h = new Atom("hydrogen",1);
Atom o = new Atom("oxygen",16);
AtomicList alist = new AtomicList();
alist.add(li);
alist.add(c);
alist.add(h);
alist.add(o);
alist.tostring();
}
}
Output:
[lithium(3), carbon(12), hydrogen(1), oxygen(16)]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.