java. ( The driver class id just to test the methods are working ) you will be i
ID: 3831735 • Letter: J
Question
java. ( The driver class id just to test the methods are working )
you will be implementing an Atom class to represent an atom from the periodic table. The Atom class should have two properties - name and atomic weight - that should be immutable (real atoms can't change their atomic weight!).
You will also implement a driver class that will create a LinkedList and populate it with Atom objects. Implement all of this in a single 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.
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)
Atom LinkedList:
This class implements a linked list using the Atom class to store Atom objects. The LinkedList should use the Atom's toString method to print each Atom in the list. If you create an AtomicList like this:
It should print this:
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
AtomTest.java
import java.util.LinkedList;
public class AtomTest {
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);
// (this is where you create the LinkedList called alist)
LinkedList<Atom> alist = new LinkedList<Atom>();
alist.add(li);
alist.add(c);
alist.add(h);
alist.add(o);
System.out.println(alist);
}
}
Atom.java
public class Atom {
private String name;
private int atomicNumber;
public Atom(String name, int atomicNumber) {
this.name = name;
this.atomicNumber=atomicNumber;
}
public String toString(){
return name+"("+atomicNumber+")";
}
}
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.