Question

In: Computer Science

ASAP PLEASE... USE PYTHON Design and implement in a language of your choice, i.e. a C++...

ASAP PLEASE... USE PYTHON

Design and implement in a language of your choice, i.e. a C++ , Java, or Python as you prefer, program that calculates the molecular weight of a formula. Your program should accept a formula in whatever form you choose as input from the user, and should display the molecular weight for that formula. Your program must be able to handle formulas that:

  • consist of a single element, e.g., He    (worth 40 points)
  • consist of a sequence of elements, e.g., H H O    (worth 20 points)
  • contain numeric constants, e.g., H2O    (worth 20 points)
  • contain nested formulas, e.g., (NH4)2SO4    (worth 20 points)

The representation of data and the algorithm you choose for your implementation are entirely up to you. The interface for your program should clearly describe the expected input form so the user can interact effectively. Grading emphasis for this assignment is on correctness only -- inefficiency and inelegance may be commented on but will not adversely affect your grade except in extreme cases.

The periodic table of elements can be found in many sources, incuding numerous Web pages (e.g., www.chemicalelements.com/show/mass.html). A list of elements with their atomic weights is written as raw text below:

    H    1.0080    He   4.0026    Li   6.9410    Be   9.0122    B   10.8100

    C   12.0110    N   14.0067    O   15.9994    F   18.9984    Ne  20.1790

    Na  22.9898    Mg  24.3050    Al  26.9815    Si  28.0860    P   30.9738

    S   32.0600    Cl  35.4530    Ar  39.9480    K   39.1020    Ca  40.0800

    Sc  44.9559    Ti  47.9000    V   50.9414    Cr  51.9960    Mn  54.9380

    Fe  55.8470    Co  58.9332    Ni  58.7100    Cu  63.5460    Zn  65.3700

    Ga  69.7200    Ge  72.5900    As  74.9216    Se  78.9600    Br  79.9040

    Kr  83.8000    Rb  85.4678    Sr  87.6200    Y   88.9059    Zr  91.2200

    Nb  92.9064    Mo  95.9400    Tc  98.9062    Ru 101.0700    Rh 102.9055

    Pd 106.4000    Ag 107.8680    Cd 112.4000    In 114.8200    Sn 118.6900

    Sb 121.7500    Te 127.6000    I  126.9045    Xe 131.3000    Cs 132.9055

    Ba 137.3400    La 138.9055    Ce 140.1200    Pr 140.9077    Nd 144.2400

    Pm 145.0000    Sm 150.4000    Eu 151.9600    Gd 157.2500    Tb 158.9254

    Dy 162.5000    Ho 164.9303    Er 167.2600    Tm 168.9342    Yb 173.0400

    Lu 174.9700    Hf 178.4900    Ta 180.9479    W  183.8500    Re 186.2000

    Os 190.2000    Ir 192.2200    Pt 195.0900    Au 196.9665    Hg 200.5900

    Tl 204.3700    Pb 207.2000    Bi 208.9806    Po 210.0000    At 210.0000

    Rn 222.0000    Fr 223.0000    Ra 226.0254    Ac 227.0000    Th 232.0381

    Pa 231.0359    U  238.02900   Np 237.0482    Pu 242.0000    Am 243.0000

    Cm 247.0000    Bk 249.0000    Cf 251.0000    Es 254.0000    Fm 253.0000

    Md 256.0000    No 256.0000    Lr 257.00

Solutions

Expert Solution

public class MolecularWeight {

    public static void main(String[] args) {

        // symbol table whose key = element symbol and value = molecular weight
        ST<String, Double> st = new ST<String, Double>();

        // read in elements symbols and their atomic weights
        In in = new In("elements.csv");
        while (in.hasNextLine()) {
            String line = in.readLine();
            String[] fields = line.split(",");
            String symbol = fields[2];
            double weight = Double.parseDouble(fields[3]);
            st.put(symbol, weight);
        }

        // read in chemical formulas from stdin, and print out molecular weight
        while (StdIn.hasNextLine()) {
            double total = 0.0;
            String line = StdIn.readLine();
            String[] fields = line.split("\\.");   // period is a special regexp char
            for (int i = 0; i < fields.length; i++) {

                // parse Si5 into element and number of atoms
                int j;
                for (j = 0; j < fields[i].length(); j++) {
                    if (Character.isDigit(fields[i].charAt(j))) break;
                }
                String symbol = fields[i].substring(0, j);
                double weight = st.get(symbol);

                // add weight to running total
                String atoms = fields[i].substring(j, fields[i].length());
                if (atoms.length() == 0) total += weight;
                else                     total += Integer.parseInt(atoms) * weight;
            }


            StdOut.printf("Molecular weight of %s = %f\n", line, total);
        }


    }
}

Related Solutions

IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display the area and perimeter of a rectangle with width = 4 and height = 8.
IN C++ LANGUAGE PLEASE::: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source...
IN C++ LANGUAGE PLEASE::: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2]
Implement the MSI cache coherence protocol in your favorite programming language (C, C++, Java, python, etc.)....
Implement the MSI cache coherence protocol in your favorite programming language (C, C++, Java, python, etc.). Wikipedia has a nice high level description of the protocol. Consider only one level of cache which is a write back cache. Moreover, assume that there are 4 processing cores working on a single shared memory. To simplify, assume that you are writing the code for only one block of cache and that block can hold 4 different memory locations.
Assemby language - MIPS Your assembly should implement the C code directly – i.e.,do not ’optimize’...
Assemby language - MIPS Your assembly should implement the C code directly – i.e.,do not ’optimize’ the C code to change the order of operations or reduce computations. Write MIPS assembly code implementing the following C/C++ statement: y = 13 - 11*x; One way of doing the multiply without a multiply instruction is by using many add instructions (x+x+...+x). For this problem you should do it with fewer additions. Hint: We know how to express any integer as a sum...
(20 pts) Using your programming language of choice (from C++, Java, or Python) , also drawing...
(20 pts) Using your programming language of choice (from C++, Java, or Python) , also drawing on your experience from program 1, read an integer, n from keyboard (standard input). This integer represents the number of integers under consideration. After reading that initial integer in, read n integers in, and print the minimum and maximum of all the integers that are read in. Example: Input Output 7 1 5 3 6 9 22 2 Min: 1 Max: 22 C++ preferred
asap please python Using block diagram, design the architectural layout of the simplified model of a...
asap please python Using block diagram, design the architectural layout of the simplified model of a symmetric Cryptosystem. Implement a cryptosystem columnar transposition technique with a 4 x 4 matrix arrangement. Test your code with ‘cryptography’ as the plaintext.
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm to sort the digits: Use Counting Sort or Bucket Sort. • Assume the numbers are maximum 4-digit numbers. • If using Counting Sort, you can see that your digit range is between 0 and 9 ([0…9]). • If using Bucket Sort, you will have ten buckets labeled from 0 to 9. Please add comments and explain every step carefully.
use python3 language and need answer ASAP please thank you. The primary sources of calories in...
use python3 language and need answer ASAP please thank you. The primary sources of calories in food are fat, carbohydrate, and protein. Proteins and carbohydrates have four calories per gram, and fat has nine calories per gram. For example, on a box of Honey Nut Cheerios, it says that a serving has 1.5 grams of fat, 22 grams of carbohydrates, and 2 grams of protein. That works out to: 1.5 * 9 + 22 * 4 + 2 * 4...
IN C++ PLEASE!!! Design and implement a program (name it Coins) that determines the values of...
IN C++ PLEASE!!! Design and implement a program (name it Coins) that determines the values of coins in a jar. The program prints out the total dollars and cents in the jar. The program prompts the user to enter the number of coins (quarters, dimes, nickels, and pennies). Print out the number of coins entered for each coin type on separate lines followed by the total amount of money in the jar as dollars and cents as shown below.
Design and implement a relational database application of your choice using MS Workbench on MySQL a)...
Design and implement a relational database application of your choice using MS Workbench on MySQL a) Declare two relations (tables) using the SQL DDL. To each relation name, add the last 4 digits of your Student-ID. Each relation (table) should have at least 4 attributes. Insert data to both relations (tables); (15%) b) Based on your expected use of the database, choose some of the attributes of each relation as your primary keys (indexes). To each Primary Key name, add...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT