Answered step by step
Verified Expert Solution
Link Copied!

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
pub1ic static int
Stack IInteger> stack = new Stack (+-;
int sun =e.
return nasultit:
private static int getheowebeotarststring symbol)(
switch (symbol){
case "H": return 1.
case ""he-:
return 2
case "cm : return 6 :
return 7413
return
case "P"
return 2 :
return 1O.
return 11:
return 12:ing
return 13.
case "S1": return 14"
case p": return 15.
case "S":
case "Cl16:
return 17:
case "K":
case "Ca2 : return 2a
case "Sc":
return 21"T":
return 23:
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
H2O
NaCl
C6H12O6
Example output:
1
54
10
28
96
(Extra Credit)
Ni(NO3)2
Co3(Fe(CN)6)2
Example output:
90
289
package org.assignment2;
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)
{
//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 4 options, you need to perform a decision//
//If is a capital letter
{
//if the stack is not empty
{
//pop 1:
//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 1:
//multiply times the number
//add it to a cumulative sum
}
//else if parenthesis
{
}
return 1;
}
private static int getElementProtons(String c)
{
switch (c)
{
case "H": return 1;
case "He": return 2;
case "Li": return 3;
case "Be": return 4;
case "B": return 5;
case "C": return 6;
case "N": return 7;
case "O": return 8;
case "F": return 9;
case "Ne": return 10;
case "Na": return 11;
case "Mg": return 12;
case "Al": return 13;
case "Si": return 14;
case "P": return 15;
case "S": return 16;
case "Cl": return 17;
case "K": return 19;
case "Ar": return 18;
case "Ca": return 20;
case "Sc": return 21;
case "Ti": return 22;
case "V": return 23;
case "Cr": return 24;
case "Mn": return 25;
case "Fe": return 26;
case "Ni": return 28;
case "Co": return 27;
case "Cu": return 29;
case "Zn": return 30;
case "Ga": return 31;
case "Ge": return 32;
case "As": return 33;
case "Se": return 34;
case "Br": return 35;
case "Kr": return 36;
case "Rb": return 37;
case "Sr": return 38;
case "Y": return 39;
case "Zr": return 40;
case "Nb": return 41;
case "Mo": return 42;
case "Tc": return 43;
case "Ru": return 44;
case "Rh": return 45;
case "Pd": return 46;
case "Ag": return 47;
case "Cd": return 48;
case "In": return 49;
case "Sn": return 50;
case "Sb": return 51;
case "I": return 53;
case "Te": return 52;
case "Xe": return 54;
default: return 0;
}
}
}
--------------------------------------
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
image text in transcribed

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions