CS 2102 Homework Assignment #3 []

Assigned: Tuesday November 07, 2006, 10:00 AM
Due: Tuesday November 14, 2006, 10:00 AM

Guidelines:

To become available. Now that we are moving away from rigid, manual test cases, we are introducing JUnit.

The context for this homework assignment is General Chemistry. For more details, read here.

This is the first homework assignment that is scheduled for a full week. Please pay attention to the order of the questions in this assignment, because I believe that you should attempt to solve these problems in the order that I have suggested.

Please note that during this week-long assignment, the readings for the course [pp. 164-238,251-268] will form the basis for each answer.

Clarifications [OPTIONAL]

These clarifications are presented here to point out things that are missing from the assignment specification. You could try to include these for the homework.

  1. As several people have pointed out, it would be quite useful for Element to also record the 'name' of the Element, rather than just its Symbol. To do so, provide the extra instance variable and instance accessor methods for this information. The toString() method for Element would also change.

Description

  1. [20 pts.] You are asked to design a Java class to represent a Chemical Element. Call this class Element. An Element is defined by (a) its atomic number (currently an integer in the range 1 .. 118); (b) its atomic symbol, which is a String containing either one, two, or three characters. Note that all three-character symbols start with a capital 'U'; (c) its atomic mass, represented as a float that may be accurate to up to 5 decimal points. An element may be unstable, that is, it will spontaneously decay, making it impossible to properly evaluate its atomic mass. In these cases, the atomic mass is simply a positive integer that represents the "mass number of the longest-lived isotope of the element"; (d) You must represent as a boolean whether an element is unstable, since that is needed to properly understand the atomic mass; (e) finally, you must store the full name of the Element as a String.

    (a) Define instance variables to store these four pieces of information (number, symbol, mass, stability).
    (b) Define constructor to properly construct an Element object.
    (c) Define read-only accessor [pp. 202-206] methods to retrieve information about an element, without allowing any external changes. These include getName(), getNumber(), getSymbol(), getMass(), and isUnstable().
    (d) Define the public String toString() method [pp. 194-197] to return a String representation of an Element object as "Symbol mass". If the element is unstable, then the String value must be "Symbol [mass]" where mass is in brackets.
    (e) Define the public boolean equals (Object o) method [pp. 194-197] to properly determine whether two Element objects are the same by simply comparing the atomic symbols of the two Element objects.
    (f) Define the public int hashCode() method [described in class]. Base your implementation on the requirements for this method.

     
  2. [20 pts.] Design a set of JUnit test cases to properly validate the design/implementation of the Element class.
     
  3. [20 pts.] Design a class PeriodicTable which loads up information from an elements.txt file to populate an array of elements. You can assume that you are aware of the total number of elements in advance, before you open the file. Note that the format of each line of the elements.txt file is "atomicNumber AtomicSymbol AtomicName AtomicMass"

    (a) Define a static array of Element objects [pp. 251-268].
    (b) Define a static method public static void initialize(File f) throws FileNotFoundException that properly instantiates this array with Element objects.

    You will likely find the Float.parseFloat(String s) method useful.

    (c) Define a static method public static Element getElement(String symbol) that returns the Element object associated with the given atomic symbol (or null if none exists).
    (d) Define a static method public void outputElements() that outputs all elements, one per line, to the console.
    (e) Define a JUnit test case to properly validate the design/implementation of PeriodicTable.
     
  4. [20 pts.] Design a Molecule class that can be used to represent basic molecular compounds, such as Water (H2O), Methanol (CH3OH), salt (NaCl), Sulphuric Acid (H2SO4). Your Molecule class should be able to handle molecules constructed by the combination of up to ten Element objects (with varying repetitions). For example, Methanol is constructed from: a Carbon (C) Element, a Hydrogen (H) Element (repeated three times), an Oxygen (O) and finally one more Hydrogen (H). A Molecule object is uniquely identified by the ordering (left-to-right) of its constituent Element objects.

    Unlike an Element, a Molecule object can be modified.

    (a) Define an instance array variable to store up to ten Element objects. Define instance array variable to store up to ten int values that represent the number of corresponding Elements.

    The reason for defining TWO arrays is to enable you to store a molecule with up to 10 elements. Note that in the model proposed for this homework Sulphuric Acid (H2SO4) is viewed as a molecule with three elements: H (repeated twice), Sulfur (only once), and Oxygen (repeated four times).

    (b) Define constructor to properly construct a Molecule object.
    (c) Define the public String toString() method [pp. 194-197] to return a String representation of a Molecule object as "A#B#C#D#" where 'A' represents the atomic symbol of the relevant. Note that it is not possible to represent 'subscript' information within simple text strings, so the output for Sulphuric Acid would be "H2SO4".
    (d) Define a public boolean add (Element e, int count) method that adds to a Molecule object the given Element object with desired count. If the Molecule is "full", then return false; otherwise return true.

    Assuming you had three element objects h (for Hydrogen), o (for Oxygen), and s (for Sulfur), then you would construct a Sulphuric Acid (H2SO4) molecule as follows:

    Molecule m = new Molecule (h, 2);     // Use constructor to construct H2 molecule
    m.add (s, 1);                                  // Now H2S molecule
    m.add (o, 4);                                  // Now H2SO4 molecule

    (e) Define a public float getMass () method that returns the proper mass of the molecule by summing the individual masses of its constituent Element objects. Note that it is hard to agree on masses for molecules because of the variability of the elements. For our purposes, this returns a calculated mass, which is only meant as an approximation to the mass.

    (f) Define the public boolean equals (Object o) method [pp. 194-197] to properly determine whether two Molecule objects are the same by simply comparing (left-to-right) the Elements (and their counts). Thus Water (H20) is not the same as Hydrogen Peroxide (H202).

    Note that you must be careful with how you construct molecules. The following will not be equal.

    Molecule m = new Molecule (h, 1);     // Use constructor to construct H molecule
    m.add (s, 1);                                  // Now H2S molecule
    m.add (o, 4);                                  // Now H2SO4 molecule


    (f) Define the public int hashCode() method [described in class]. Base your implementation on the requirements for this method.
     
  5. [20 pts.] Design a set of JUnit test cases to properly validate the design/implementation of the Molecule class.

All classes must be fully documented, according to the JavaDoc standard introduced in class and used throughout all class examples.

Advanced

As ungraded extensions to these assignments, consider the following two possible functions.

  1. Write a public static Molecule parse(String s) method for the Molecule class that takes a String value such as "CH3CH2CH2CH2OH" (the representation for the chemical n-butanol) and produces the corresponding molecule. For sample compounds, there are numerous lists (such as this one).
  2. Extend the add method for molecule so attempts to add the same element at the end of the molecule (from left-to-right) results in incrementing the count. Thus:

    Molecule m = new Molecule (h, 1);     // Use constructor to construct H molecule
    m.add (h, 1);                                  // Now H2 molecule
     

Deliverables

Your goal is to turnin the Project files by Tuesday November 14th at 10:00 AM  Further details will be posted HERE showing the preferred means of uploading your solution to the TAs.

Please be aware that no late homeworks will be accepted. This means that we will grade as zero any homework not submitted by the above turnin means.


Notes

  1. [11/12/06 10:24 PM] Added clarification to the Homework. These are optional, given the lateness of their posting.
  2. [11/08/06 12:03 AM] Clarified use of getMass() method in Molecule as an approximation.
  3. [11/07/06 10:55 PM] Clarified that Periodic Table must have its own JUnit test cases.
  4. [11/07/06 10:43 PM] Clarified name of method to use as PeriodicTable initializer
  5. [11/07/06 09:00 PM] Clarification: replaced inappropriate use of 'weight' with 'mass'.
  6. [11/07/06 1:52 AM] Draft HW ready to go, pending final review.
  7. [11/06/06 11:54 PM] Homework3 to be posted here.

©2006 George T. Heineman