Question: In Java Edit the following code: public class FormulaCalc{ public static void main(String [] Args) { } /** Algorithm *@param a molecular formula *@return Total

In Java

Edit the following code:

public class FormulaCalc{

public static void main(String [] Args)

{

}

/** Algorithm

*@param a molecular formula

*@return Total number of protons

*/

public static int Algorithm(String a)

{

return 1;

}

}

A molecule is made of two or more atoms, where each atom can occur more than once. An atom is represented by an atomic symbol consisting of a single uppercase letter or an uppercase letter followed by a lowercase letter (e.g. H, O, Fl, Na).

An atom also has an atomic number that represents the number of protons in that atom. For example, the chlorine atom (Cl) has an atomic number of 17, which means it has 17 protons.

A molecular formula is a non-empty sequence of atomic symbols (e.g. KBr, CO, LiCl).

Part of the formula may be repeated by enclosing it in parentheses followed by an integer between 2 and 99 (e.g. Ca(OH)2).

The parentheses are omitted if the repeating component is a single atom (e.g. H2O, C6H12O6, Na2SO4, Ni(NO3)2).

A molecular formula can consist of a single atom (e.g. H) or a single atom repeated (e.g. O3)

It can also consist of multiple levels of parentheses (e.g. Co3(Fe(CN)6)2).

Modify the Algorithm method above. It takes a string representing a valid molecular formula and computes and returns the total number of protons in the molecule. The method should use a stack to keep track of partial results as you encounter subexpression in the chemical formula.

You may assume that the atoms in the formula all have atomic numbers no greater than 54 and appear in the truncated periodic table shown below. Each box shows the atomic symbol along with its atomic number.

Use the Java class Stack for your stack class. Do not call any methods except push, pop, empty, and peek.

The formula can potentially have any number of layers of parentheses.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!