chemistry cs1020. given a set of mappings and chemical formula: calculate the molecule mass of the...

58
Chemistry CS1020

Upload: ruth-willis

Post on 21-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

ChemistryCS1020

Page 2: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Given a set of mappings and chemical formula:

• Calculate the molecule mass of the formula

Problem Description

C 12H 1N 14O 16

(NH4)2CO3

Page 3: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 4: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 5: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 6: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 7: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 8: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 9: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 10: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 11: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 12: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 13: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 14: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 15: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 16: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 17: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 18: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 19: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

?What data structure should I use for:

MassMapping

HashMap FormulaProcessing

Stack

Page 20: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 21: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html

Stack

What should my stack contain?

Mass of each Atom The ElementsOR

Page 22: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 23: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 24: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 25: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 26: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 27: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 28: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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();}

Page 29: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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

Page 30: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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}

}}

Page 31: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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;}

Page 32: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

CH4

Stack

-1

Page 33: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

CH4

Stack

-1

12

Push The Mass of ‘C’ Into The Stack

Page 34: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

CH4

Stack

-1

12

Push The Mass of ‘H’ Into The Stack

1

Page 35: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

CH4

Stack

-1

12

Multiply top of stack by 4 1

Page 36: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

CH4

Stack

-1

12

Multiply top of stack by 4 4

Page 37: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

CH4

Stack

-1

12

Accumulate all values 4

4 + 12 = 16

Page 38: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

Page 39: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

Push The Mass of ‘N’ Into The Stack

14

Page 40: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

Start New Session

14

-1

Page 41: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Push The Mass of ‘C’

Into The Stack

12

Page 42: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Push The Mass of ‘H’

Into The Stack

12

1

Page 43: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Multiply top of stack by 2

12

1

Page 44: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Multiply top of stack by 2

12

2

Page 45: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Start New Session

12

2

-1

Page 46: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Push The Mass of ‘C’

Into The Stack

12

2

-1

12

Page 47: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Push The Mass of ‘H’

Into The Stack

12

2

-1

12

1

Page 48: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Multiply Top Of Stack by 3

12

2

-1

12

1

Page 49: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Multiply Top Of Stack by 3

12

2

-1

12

3

Page 50: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Accumulate

12

2

-1

12

3

3 + 12 = 15

Page 51: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Push Result

12

2

15

3 + 12 = 15

Page 52: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Multiply Top of Stack by 2

12

2

15

Page 53: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

-1Accumulate

12

2

30

30 + 2 + 12 = 44

Page 54: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

Push Result44

30 + 2 + 12 = 44

Page 55: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

Multiply Top of Stack by 344

Page 56: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

Multiply Top of Stack by 3132

Page 57: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

Visualization

N(CH2(CH3)2)3

Stack

-1

14

Accumulate all values132

132 + 14 = 146

Page 58: Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4

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