Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please read the questions carefully and consider the sample solution provided at the end of my questions , I Put it so you can run
Please read the questions carefully and consider the sample solution provided at the end of my questions I Put it so you can run it and see how it should work you might need to fix only the expected output please note no entry input needed in the code just to have the expected outcome once I run it import jakarythrstaks:
II Test the Algoritenm net hod with als
pubic static int
Stack IInteger stack new Stack ;
int sun e
return nasultit:
private static int getheowebeotarststring symbol
switch symbol
case : return
case he:
return
case : return :
return
return
case P
return :
return
return :
return
return
case S: return
case : return
case S:
case
return :
case K:
case : return
case Sc:
return :
return :
You can 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
HO
NaCl
CHO
Example output:
Extra Credit
NiNO
CoFeCN
Example output:
package org.assignment;
public class FormulaCalc
public static void mainString args
Algorithm
@param a Molecular formula
@return Total number of protons
public static int AlgorithmString a
You can see how to parse an array in there.
Once you are dealing character by character, you can set a multiple if statement to check if that character is a capital letter, a small case letter, a number, or a parenthesis.
For each of these options, you need to perform a decision
If is a capital letter
if the stack is not empty
pop :
add it to cumulative sum
request the string value and add it to stack.
else if is a lowercase letter
pop stack to clear it
grab the prior letter and request the string value and add it to stack
else if it is a number
pop :
multiply times the number
add it to a cumulative sum
else if parenthesis
return ;
private static int getElementProtonsString c
switch c
case H: return ;
case He: return ;
case Li: return ;
case Be: return ;
case B: return ;
case C: return ;
case N: return ;
case O: return ;
case F: return ;
case Ne: return ;
case Na: return ;
case Mg: return ;
case Al: return ;
case Si: return ;
case P: return ;
case S: return ;
case Cl: return ;
case K: return ;
case Ar: return ;
case Ca: return ;
case Sc: return ;
case Ti: return ;
case V: return ;
case Cr: return ;
case Mn: return ;
case Fe: return ;
case Ni: return ;
case Co: return ;
case Cu: return ;
case Zn: return ;
case Ga: return ;
case Ge: return ;
case As: return ;
case Se: return ;
case Br: return ;
case Kr: return ;
case Rb: return ;
case Sr: return ;
case Y: return ;
case Zr: return ;
case Nb: return ;
case Mo: return ;
case Tc: return ;
case Ru: return ;
case Rh: return ;
case Pd: return ;
case Ag: return ;
case Cd: return ;
case In: return ;
case Sn: return ;
case Sb: return ;
case "I": return ;
case Te: return ;
case Xe: return ;
default: return ;
This is an example of solution, however it give wrong output not matching the expected output of each chemical formula stated in at the beginning of the code please consider this however fix the output to the right expected one also please note there is no input entry no need I need to run and have the expected outcome with no input

Step by Step Solution
There are 3 Steps involved in it
Step: 1

Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2

Step: 3

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