Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

!!IN JAVA PLEASE!! :) A molecule is made of two or more atoms, where each atom can occur more than once. An atom is represented

!!IN JAVA PLEASE!! :)

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).

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

(Extra Credit)

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).

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

Assignment

Modify the Java program named FormulaCalc.java that has a method that takes a string representing a valid molecular formula and computes and returns the total number of protons in the molecule. Your 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.

You may assume that if an integer is given in the formula it will be in a valid location and its value will be between 2 and 99, inclusive.

You may assume that the given formula is valid, including only atoms shown above and proper layering of parentheses.

Your class should also have a main method that tests your formula computation method. See the Testing section for the format for the input and output for testing.

(Extra Credit)

The program will process parentheses in the formulas.

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

Testing

You should write your main method so that it reads input from the keyboard (standard input) and outputs its results to the console (standard output) for testing. The first line of input should be the number of test cases to be executed. Subsequent input will be one chemical formula per line. For each formula, output the number of protons in the formula. Each output should be on a separate line. You do not need any additional prompts or output text.

Or you can use the formulas.txt file in the folder.

Example input:

H KBr H2O C6H12O6 

Example output:

1 54 10 96 

(Extra Credit)

Ca(OH)2 Ni(NO3)2 Co3(Fe(CN)6)2 

Example output:

38 90 289

JAVA CODE PROVIDED: (PLEASE USE THIS)

package org.assignment2; import java.util.Stack; 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; } }

|----------------------------THIS IS A DIFFERENT CLASS, PLEASE USE IT-----------------------------------------------|

import org.junit.jupiter.api.Test; import org.assignment2.FormulaCalc; import org.junit.jupiter.api.Assertions; public class FormulaCalcTest { @Test public void test_with_H() { Assertions.assertEquals(1, FormulaCalc.Algorithm("H")); } @Test public void test_with_KBr() { Assertions.assertEquals(54, FormulaCalc.Algorithm("KBr")); } // The parentheses are replaced with underscores @Test public void test_with_Ca_OH_2() { Assertions.assertEquals(38, FormulaCalc.Algorithm("Ca(OH)2")); } @Test public void test_with_H2O() { Assertions.assertEquals(10, FormulaCalc.Algorithm("H2O")); } @Test public void test_with_C6H12O6() { Assertions.assertEquals(96, FormulaCalc.Algorithm("C6H12O6")); } @Test public void test_with_Ni_NO3_2() { Assertions.assertEquals(90, FormulaCalc.Algorithm("Ni(NO3)2")); } @Test public void test_with_Co3_Fe_CN_6_2() { Assertions.assertEquals(90, FormulaCalc.Algorithm("Co3(Fe(CN)6)2")); } }

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_2

Step: 3

blur-text-image_3

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

Data Visualization A Practical Introduction

Authors: Kieran Healy

1st Edition

0691181624, 978-0691181622

More Books

Students also viewed these Databases questions

Question

i. Experience a. Wh ere Do Con11nUJ1ication Researc hers

Answered: 1 week ago

Question

1. Does your voice project confidence? Authority?

Answered: 1 week ago