Download presentation
Presentation is loading. Please wait.
Published byRuth Willis Modified over 9 years ago
1
Chemistry CS1020
2
Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4 ) 2 CO 3
3
Let’s work on a few examples based on the given mapping Problem Description C12 H1 N14 O16 (NH 4 ) 2 CO 3 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH
4
Let’s work on a few examples based on the given mapping Problem Description C12 H1 N14 O16 (NH 4 ) 2 CO 3 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH 14 + 4 x 1
5
Let’s work on a few examples based on the given mapping Problem Description C12 H1 N14 O16 (NH 4 ) 2 CO 3 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH 18 * 2
6
Let’s work on a few examples based on the given mapping Problem Description C12 H1 N14 O16 36 + CO 3 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH 12 + 3 x 16
7
Let’s work on a few examples based on the given mapping Problem Description C12 H1 N14 O16 36 + CO 3 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH 60
8
Let’s work on a few examples based on the given mapping Problem Description C12 H1 N14 O16 36 + 60 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH
9
Let’s work on a few examples based on the given mapping Problem Description C12 H1 N14 O16 (NH 4 ) 2 CO 3 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH M r = 96
10
Let’s work on a few examples based on the given mapping Problem Description C12 H1 N14 O16 (NH 4 ) 2 CO 3 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH M r = 96 12 + 4 x 1 + 16 + 1
11
Let’s work on a few examples based on the given mapping Problem Description C12 H1 N14 O16 (NH 4 ) 2 CO 3 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH M r = 96 33
12
Let’s work on a few examples based on the given mapping Problem Description C12 H1 N14 O16 (NH 4 ) 2 CO 3 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH M r = 96 M r = 33
13
Let’s work on a few examples based on the given mapping C12 H1 N14 O16 (NH 4 ) 2 CO 3 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH M r = 96 M r = 33 12 + 3 x 1
14
Let’s work on a few examples based on the given mapping C12 H1 N14 O16 (NH 4 ) 2 CO 3 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH M r = 96 M r = 33 15
15
Let’s work on a few examples based on the given mapping C12 H1 N14 O16 (NH 4 ) 2 CO 3 (15 + CH 4 C 3 H 8 ) 4 CH 4 OH M r = 96 M r = 33 12 + 4 x 1 + 3 x 12 + 8 x 1
16
Let’s work on a few examples based on the given mapping C12 H1 N14 O16 (NH 4 ) 2 CO 3 (15 + CH 4 C 3 H 8 ) 4 CH 4 OH M r = 96 M r = 33 60
17
Let’s work on a few examples based on the given mapping C12 H1 N14 O16 (NH 4 ) 2 CO 3 (15 + 60) 4 CH 4 OH M r = 96 M r = 33
18
Let’s work on a few examples based on the given mapping Problem Description C12 H1 N14 O16 (NH 4 ) 2 CO 3 ((CH 3 )CH 4 C 3 H 8 ) 4 CH 4 OH M r = 96 M r = 33 M r = 300
19
? What data structure should I use for: Mass Mapping HashMap Formula Processing Stack
20
HashMap https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html HashMap hashMapName = new HashMap (); HashMap massMapping = new HashMap (); massMapping.put(‘C’, 12); massMapping.put(‘H’, 1); massMapping.put(‘N’, 14); massMapping.get(‘C’); massMapping.get(‘H’); massMapping.get(‘N’); Setting Keys and ValuesRetrieving Values Returns 12 Returns 1 Returns 14
21
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html Stack What should my stack contain? Mass of each AtomThe Elements OR
22
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html Stack What should my stack contain? Mass of each AtomThe Elements We will use a stack of integers CH 4 Stack
23
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html Stack What should my stack contain? Mass of each AtomThe Elements We will use a stack of integers CH4CH4 Stack 12
24
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html Stack What should my stack contain? Mass of each AtomThe Elements We will use a stack of integers CH4CH4 Stack 12 1
25
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html Stack What should my stack contain? Mass of each AtomThe Elements We will use a stack of integers CH 4 Stack 12 14
26
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html Stack What should my stack contain? Mass of each AtomThe Elements We will use a stack of integers CH 4 Stack 12 14 Total = 12 + 4 = 16
27
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);
28
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(); }
29
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
30
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 } } }
31
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; }
32
Visualization CH 4 Stack
33
Visualization CH4CH4 Stack 12 Push The Mass of ‘C’ Into The Stack
34
Visualization CH4CH4 Stack 12 Push The Mass of ‘H’ Into The Stack 1
35
Visualization CH 4 Stack 12 Multiply top of stack by 4 1
36
Visualization CH 4 Stack 12 Multiply top of stack by 4 4
37
Visualization CH 4 Stack 12 Accumulate all values 4 4 + 12 = 16
38
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack
39
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack Push The Mass of ‘N’ Into The Stack 14
40
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack Start New Session 14
41
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Push The Mass of ‘C’ Into The Stack 12
42
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Push The Mass of ‘H’ Into The Stack 12 1
43
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Multiply top of stack by 2 12 1
44
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Multiply top of stack by 2 12 2
45
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Start New Session 12 2
46
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Push The Mass of ‘C’ Into The Stack 12 2 12
47
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Push The Mass of ‘H’ Into The Stack 12 2 12 1
48
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Multiply Top Of Stack by 3 12 2 12 1
49
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Multiply Top Of Stack by 3 12 2 12 3
50
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Accumulate 12 2 12 3 3 + 12 = 15
51
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Push Result 12 2 15 3 + 12 = 15
52
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Multiply Top of Stack by 2 12 2 15
53
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Accumulate 12 2 30 30 + 2 + 12 = 44
54
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Push Result 44 30 + 2 + 12 = 44
55
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Multiply Top of Stack by 3 44
56
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Multiply Top of Stack by 3 132
57
Visualization N(CH 2 (CH 3 ) 2 ) 3 Stack 14 Accumulate all values 132 132 + 14 = 146
58
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).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.