Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A molecule is a grouping of two or more atoms. A molecule can be written as a sequence of the symbols of its atoms. For

A molecule is a grouping of two or more atoms. A molecule can be written as a sequence of the symbols of its atoms. For example, H denotes an atom of hydrogen, C denotes an atom of carbon, and O denotes an atom of oxygen. Formic Acid is defined by the sequence HCOOH, which is a hydrogen, carbon, oxygen, oxygen, hydrogen atoms. If a digit is placed after a symbol it means there are that many of those atoms in the molecule, such as water, H2O is two hydrogen atoms and an oxygen. Digits can also be placed after groupings such as (CH)3 which is three carbon/hydrogen groupings, (CH)(CH)(CH). The weight of a molecule is simply the weight of all the atoms in it. Given the weight of hydrogen is 1, carbon is 12, and oxygen is 16, Formic Acid has a weight of 46, Water has a weight of 18, and (CH)3 has a weight of 39. More complex molecules sequences can be created by nesting groupings such as H((OH)2C3H)2, which has a weight of 143.

Parenthesis and other types of braces must follow pairing rules in order for an expression to be valid. Stacks are often used for processing of parenthesis in text, such as compilers making sure braces match up correctly. A Stack can also be used to evaluate parenthesized expressions like molecules.

Every opening and closing parenthesis must have a matching of the other. It is also important to note that in valid text, the matching opening parenthesis to a closing one is last one encountered. This is what makes the Stack the perfect structure for such problems. Open parenthesis signify the beginning of a grouping, allowing nested groupings to be added to the stack. Once a closing parenthesis is reached it closes the group at the top of the Stack, which can then be evaluated.

Attempting to create molecule's with invalid sequences will result in one of two Exceptions occuring. Sequences with unpaired parenthesis will result in an InvalidSequenceException. For example, the following are invalid:

)(HOC)2

(OH

(((H))))

Additionally, only letters that represent a known atom should be in the sequence. To make things a bit easier, you will only be using Hydrogen, Carbon, and Oxygen with symbols (upper and lower case allowed) and weights described above. A sequence with any other atoms will cause an InvalidAtomException. Both of these cases can be determined while calculating the weight using the algorithm described below.

A Stack of Integers can be used to compute the weight of a molecule whose sequence is stored in a String. To allow for nested groupings, you will need to add something that represents an open parenthesis to the Integer Stack in addition to weights. Since all atom weights are positive, use a -1 to indicate an open parentheis on the Stack. The step-by-step algorithm is given below:

Create a Stack of Integers

Create a new sequence String with ( ) surrounding it (if original sequence is H20, create new local String (H20), DO NOT change the original sequence data member

For each character in the new sequence String from left to right do the following:

If the character is an open parenthesis push it onto the Stack

If the character is an atom push its weight onto the Stack

If the character is a digit pop the top value off the Stack and multiply this by the digit, push the result onto the Stack

If the character is a close parenthesis pop all values off the Stack, summing them up as you go until an open parenthesis is reached, pop the open parenthesis off the Stack, push the sum onto the Stack

The final weight will be the only item left on the Stack once all characters have been processed IF the sequence was valid. If there are additional items on the Stack then the sequence was improperly formed.

This assignment will have you creating a program that collects molecules and reports their weights. Further, collections of molecules can be stored and restored by the user.

In addition to using Linked Lists and Stacks to solve the problem read the Assignment5Supplemental document. The concepts discussed in this document must be used in your solution

Provided classes:

molecule.exceptions.InvalidAtomException

molecule.exceptions.InvalidSequenceException

molecule.MoleculeDriver

Classes you must write:

molecule.Molecule

molecule.MoleculeCollection

Constructor Detail

Molecule

public Molecule(java.lang.String sequenceIn) 

Creates a new Molecule made up of the H, C, and O atoms in the configuration specified by sequenceIn.

Parameters:

sequenceIn - The sequence of atoms for this Molecule.

Throws:

InvalidAtomException - if any non C, H, O atom exists in sequenceIn

InvalidSequenceException - if unmatched parentheses exist in sequenceIn

Method Detail

setSequence

public void setSequence(java.lang.String sequenceIn) 

Updates the sequence of atoms represented by this Molecule.

Parameters:

sequenceIn - The new molecular sequence to be used for this Molecule.

Throws:

InvalidAtomException - if any non C, H, O atom exists in sequenceIn

InvalidSequenceException - if unmatched parentheses exist in sequenceIn

getSequence

public java.lang.String getSequence() 

Retrieves a String containing this Molecule's sequence of atoms.

Returns:

Molecular sequence of the Molecule.

getWeight

public int getWeight() 

Retrieves this Molecule's weight, which is calculated based on the Molecule's sequence per the algorithm specified.

Returns:

Weight of the Molecule.

toString

public java.lang.String toString() 

Generates and returns a String with the Molecule's sequence and weight. The format of the String is "SEQUENCE : WEIGHT", where SEQUENCE has a field width of 25 and is left justified. WEIGHT should be left-justified.

Overrides:

toString in class java.lang.Object

atomWeight

public static int atomWeight(char atom) throws InvalidAtomException 

Static utility method to return the atomic weight of a given atom. Supported atoms are Carbon (C), Hydrogen (H), and Oxygen (O), and the value of the atom parameter corresponds to the single letter abbreviation for these atoms (case insensitive). Atomic weights are given in their nearest whole number:

Hydrogen - 1 Carbon - 12 Oxygen - 16

Parameters:

atom - Character for atom abbreviation

Returns:

Atomic weight of passed atom

Throws:

InvalidAtomException - Thrown if an unsupported atom is passed

compareTo

public int compareTo(Molecule other) 

Compares this Molecule to a passed Molecule, determining natural order. Molecules with the same weight (regardless of sequence) are considered equal. All others are ordered relative to the magnitude of their weights.

Specified by:

compareTo in interface java.lang.Comparable

Parameters:

other - Incoming Molecule to be compared with this (local) Molecule.

Returns:

Returns an int less than 0 if the local Molecule is less than the passed Molecule, an int greater than 0 if the local Molecule is greater than the passed Molecule, and a 0 if the Molecules are equal.

clone

public java.lang.Object clone() 

Returns a deep copy of the Molecule. The reference returned should refer to a completely separate molecule with no direct or indirect aliasing of any instance data in the original Molecule.

NOTE: This method should NOT throw a CloneNotSupportedException.

Overrides:

clone in class java.lang.Object

Returns:

Deep copy of the calling Molecule.

Constructor Detail

MoleculeCollection

public MoleculeCollection() 

Creates a new MoleculeCollection containing no Molecules yet.

MoleculeCollection

public MoleculeCollection(java.util.LinkedList moleculeListIn) 

Creates a new MoleculeCollection based upon an existing list of Molecules. The newly created MoleculeCollection will store a deep copy of the data in moleculeListIn to enforce encapsulation.

If the passed reference is null, the created MoleculeCollection will be empty.

Parameters:

moleculeListIn - LinkedList of Molecules used to create a new MoleculeCollection.

Method Detail

addMolecule

public void addMolecule(Molecule add) 

Adds a copy of a given Molecule to this MoleculeCollection. Future external changes to the original Molecule will not impact values in the collection. If add is null, this MoleculeCollection is unchanged.

Parameters:

add - Molecule to be added to the Collection

sort

public void sort() 

Reorders the MoleculeCollection based on atomic weight. Molecules with the same weights should appear in their original order of insertion relative to one another (ie., sort() is a stable sorting algorithm).

moleculeWeights

public int moleculeWeights() 

Sums the weights of all Molecules in the MoleculeCollection.

Returns:

The sum of all weights in the collection

getMoleculeList

public java.util.LinkedList getMoleculeList() 

Generates and returns a deep copy of a list containing all of the Molecules in this MoleculeCollection. Modifying the returned list will not impact the contents of this MoleculeCollection in any way.

Returns:

Deep copy of the Molecules

changeSequence

public void changeSequence(int index, java.lang.String newSequence) throws InvalidAtomException, InvalidSequenceException 

Changes the sequence of a Molecule in the collection at the specified index. This does not create a new Molecule, rather modifies an existing Molecule.

If the provided sequence is not valid due to either an invalid sequence or an invalid atom, the original state of the Molecule at the specified index should be unaffected and and the resulting exception will be thrown to the caller.

Parameters:

index - Location of the Molecule to update

newSequence - New sequence of the specified Molecule

Throws:

InvalidAtomException - Thrown if the new sequence is invalid due to unknown atom

InvalidSequenceException - Thrown if the new sequence is invalid due to format

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 2 Lncs 8056

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013th Edition

3642401724, 978-3642401725

More Books

Students also viewed these Databases questions

Question

5. Understand how cultural values influence conflict behavior.

Answered: 1 week ago