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
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
Lets begin with the Atom class. The atom class should be a simple class containing two piece of data: an atoms 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:
Atom he = new Atom(helium,2); System.out.println(he);
it should return the following result:
helium (2)
Atom Specifications:
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) |
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:
field: next | Points to the next item in the list |
field: data | Atom object (should be immutable) |
constructor: ImmutableListNode(data) | Pass an Atom object as data to store |
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:
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); System.out.println(alist);
It should print this:
[lithium (3), carbon (12), hydrogen (1), oxygen (16)]
AtomicList Specifications:
field: front | Pointer to the front node of the linked list |
constructor: AtomicList | Initializes the linked list |
method: add(a) | Adds an Atom a to the linked list |
method: toString() | 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:
class Atom { // define your Atom class here } class ImmutableListNode { // define your ImmutableListNode here } public class AtomicList { public static void main(String[] args) { // Test your Atom class: Atom a = new Atom(); Atom li = new Atom("lithium",3); // Test your list class AtomicList alist = new AtomicList(); alist.add(li); System.out.println(alist); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started