Upload
ruth-willis
View
216
Download
0
Tags:
Embed Size (px)
Citation preview
ChemistryCS1020
Given a set of mappings and chemical formula:
• Calculate the molecule mass of the formula
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
14 + 4 x 1
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
18 * 2
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
36 + CO3
((CH3)CH4C3H8)4
CH4OH
12 + 3 x 16
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
36 + CO3
((CH3)CH4C3H8)4
CH4OH
60
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
36 + 60
((CH3)CH4C3H8)4
CH4OH
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
12 + 4 x 1 + 16 + 1
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
33
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
Mr = 33
Let’s work on a few examples based on the given mapping
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
Mr = 33
12 + 3 x 1
Let’s work on a few examples based on the given mapping
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
Mr = 33
15
Let’s work on a few examples based on the given mapping
C 12H 1N 14O 16
(NH4)2CO3
(15 + CH4C3H8)4
CH4OH
Mr = 96
Mr = 33
12 + 4 x 1 + 3 x 12 + 8 x 1
Let’s work on a few examples based on the given mapping
C 12H 1N 14O 16
(NH4)2CO3
(15 + CH4C3H8)4
CH4OH
Mr = 96
Mr = 33
60
Let’s work on a few examples based on the given mapping
C 12H 1N 14O 16
(NH4)2CO3
(15 + 60)4
CH4OH
Mr = 96
Mr = 33
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
Mr = 33
Mr = 300
?What data structure should I use for:
MassMapping
HashMap FormulaProcessing
Stack
HashMap
https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
HashMap<K, V> hashMapName = new HashMap<K, V>();
HashMap<Character, Integer> massMapping = new HashMap<Character, Integer>();
massMapping.put(‘C’, 12);massMapping.put(‘H’, 1);massMapping.put(‘N’, 14);
massMapping.get(‘C’);
massMapping.get(‘H’);
massMapping.get(‘N’);
Setting Keys and Values Retrieving Values
Returns 12
Returns 1
Returns 14
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Stack
What should my stack contain?
Mass of each Atom The ElementsOR
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Stack
What should my stack contain?
Mass of each Atom The Elements
We will use a stack of integers
CH4
Stack
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Stack
What should my stack contain?
Mass of each Atom The Elements
We will use a stack of integers
CH4
Stack
12
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Stack
What should my stack contain?
Mass of each Atom The Elements
We will use a stack of integers
CH4
Stack
12
1
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Stack
What should my stack contain?
Mass of each Atom The Elements
We will use a stack of integers
CH4
Stack
12
14
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Stack
What should my stack contain?
Mass of each Atom The Elements
We will use a stack of integers
CH4
Stack
12
14
Total = 12 + 4 = 16
How do I store the formula?
?As a character array
String nextLine = sc.nextLine();char[] formula = nextLine.toCharArray();
How do I process the formula?
Loop through all characters
processInput(formula);
public void run() {Scanner sc = new Scanner(System.in);
int n = sc.nextInt();sc.nextLine();
initializeMassMapping(sc, n); //implement it
String nextLine = sc.nextLine();char[] formula = nextLine.toCharArray();
processInput(formula);
int total = accumulate(); //will be defined later
System.out.println(total);
sc.close();}
private void processInput(char[] formula) { for (char c : formula) {
//what should I do??? }}
Next character can be one of these:
(
)
X
n
Open Bracket
Close Bracket
Atom Name
Number of atoms (or molecules)
What you should do if you encounter it:
Start New “Session”
Acummulate Current “Session”
Push Its Mass
Multiply Top of The Stack by n
private void processInput(char[] formula) { for (char c : formula) {
if (c == '(') { //open bracket results.push(-1); //start a new “session”} else if (c == ')') { //close bracket int sum = accumulate(); //acummulate is a “helper” //then push the sum into the stack} else if (Character.isDigit(c)) {//it’s a number //multiply the top of the stack. How? } else { //then c must be a ??? //push the corresponding atom’s mass}
}}
private int accumulate() {
int result = 0; int top = results.pop();
while (/* top is not -1 and stack is not empty */) {//add current top to result//update top to be next element in stack
}
return result;}
Visualization
CH4
Stack
-1
Visualization
CH4
Stack
-1
12
Push The Mass of ‘C’ Into The Stack
Visualization
CH4
Stack
-1
12
Push The Mass of ‘H’ Into The Stack
1
Visualization
CH4
Stack
-1
12
Multiply top of stack by 4 1
Visualization
CH4
Stack
-1
12
Multiply top of stack by 4 4
Visualization
CH4
Stack
-1
12
Accumulate all values 4
4 + 12 = 16
Visualization
N(CH2(CH3)2)3
Stack
-1
Visualization
N(CH2(CH3)2)3
Stack
-1
Push The Mass of ‘N’ Into The Stack
14
Visualization
N(CH2(CH3)2)3
Stack
-1
Start New Session
14
-1
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Push The Mass of ‘C’
Into The Stack
12
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Push The Mass of ‘H’
Into The Stack
12
1
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Multiply top of stack by 2
12
1
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Multiply top of stack by 2
12
2
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Start New Session
12
2
-1
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Push The Mass of ‘C’
Into The Stack
12
2
-1
12
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Push The Mass of ‘H’
Into The Stack
12
2
-1
12
1
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Multiply Top Of Stack by 3
12
2
-1
12
1
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Multiply Top Of Stack by 3
12
2
-1
12
3
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Accumulate
12
2
-1
12
3
3 + 12 = 15
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Push Result
12
2
15
3 + 12 = 15
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Multiply Top of Stack by 2
12
2
15
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Accumulate
12
2
30
30 + 2 + 12 = 44
Visualization
N(CH2(CH3)2)3
Stack
-1
14
Push Result44
30 + 2 + 12 = 44
Visualization
N(CH2(CH3)2)3
Stack
-1
14
Multiply Top of Stack by 344
Visualization
N(CH2(CH3)2)3
Stack
-1
14
Multiply Top of Stack by 3132
Visualization
N(CH2(CH3)2)3
Stack
-1
14
Accumulate all values132
132 + 14 = 146
Summary
• Start a new session by pushing “-1” (or any other invalid mass values that you desire).
• Push the mass of each atom when you encounter them.
• Multiply top of stack each time you encounter a number.
• Accumulate all values (until stack is empty or you pop a “session start” value).
• When done iterating, some all values in the stack (without counting the invalid value, of course).