Java代写:CS12 Chemical Compound Molar Mass

代写Java基础作业,计算摩尔质量。

Goals

Continue familiarizing yourself with:

  • Arrays
  • Classes and instantiation
  • Text file input
  • Command-line arguments

Prerequisites

This assignment requires knowledge of the material presented in class throughweek 07and/or through the introductory materials on OOP in the textbooks.

Background

Data file

There is a file on the server that is relevant to this assignment:

/srv/datasets/elements

This file contains one line of information about each of all 118 chemical elements. Each line contains the following data, separated by whitespace:

  1. Atomic number (int)
  2. Symbol (String)
  3. Name (String)
  4. Atomic weight (double)
  5. Period (row in periodic table) (int)
  6. Group (column in periodic table) (int)

Chemical Formulas

Quoth thearbiter of all human knowledge:

A chemical formula is a way of expressing information about the proportions of atoms that constitute a particular chemical compound, using a single line of chemical element symbols, numbers, and sometimes also other symbols, such as parentheses, dashes, brackets, commas and plus (+) and minus () signs.

Often, these formulas are written using subscripts and superscripts. In this program, we will simplify the idea of a chemical formula (and thus sacrifice the ability to represent certain formulas, at least in an efficient way). A chemical formula, for the sake of this program, shall be defined as:

  1. One or more whitespace-delimited tokens
  2. Each token consists of either:
    • a. An element symbol, implying one atom of that element in the compound
    • b. An element symbol followed by an underscore and an integer, implying a certain number of atoms of that element in the compound

Examples:

CompoundStandard FormulaOur Formula
carbonCC
dioxygenO2O_2
dialuminum tellurideAl2TeAl_2 Te
lithium metazirconateLi2ZrO3Li_2 Zr O_3
acetylaldehydeCH3CHOC H_3 C H O
methylamineCH5NC H_5 N
paracetamolC8H9NO2C_8 H_9 N O_2
propaneC3H8C_3 H_8

Molar Mass

Molar mass expresses the mass of a given substance (chemical element or chemical compound) divided by its amount of substance. The molar mass of a compound is the sum of the relative atomic masses (AKA atomic weights) of all atoms in the compound.

For example, for acetylaldehyde (CH3CHO/C H_3 C H O) above:

Relative atomic masses:

  • C: 12.011
  • H: 1.008
  • O: 15.999

Thus, the compound’s molar mass is:12.011+3(1.008)+12.011+1.008+15.999=44.05312.011+3(1.008)+12.011+1.008+15.999=44.053

Technically, the units for molar mass aregmolgmolin this case.

Helpful String and parsing methods

In order to separate the element name from the number of atoms, there are a number of methods in theStringclass that you may find useful. Probably the most straightforward isString.split, with which you can create an array ofStrings by splitting one string around a pattern of characters.

In addition,Integer.parseIntwill certainly be useful, as it returns theintvalue represented by the characters in aString.

Command-line arguments in Eclipse

In order to work with command-line arguments from within Eclipse, you need to specify those arguments within either Run Configurations or Debug Configurations, depending on whether you want to run or debug the program.

  1. Select Run->Run Configurations or Run->Debug Configurations as desired.
  2. Select the Arguments tab.
  3. In the Program Arguments section, Enter your arguments.
  4. Click Apply.

Assignment

You shall write a program that:

  1. Expects one or more command-line arguments that constitute a chemical formula, as described above
  2. Prints one line containing the molar mass of the compound described by the formula, to six digits after the decimal point
  3. Does not open theelementsfile more than once, nor read through that file’s contents multiple times.

You should probably define two classes for this assignment, similar to myPeriodicTable3example. As always, feel free to use any of my code from the lecture materials.

Example I/O and Sample Executable

The following examples assume a class named Assignment.

CompoundFormulaCommand LineOutput
carbonCjava Assignment06 C12.011000
dioxygenO_2java Assignment06 O_231.998000
dialuminum tellurideAl_2 Tejava Assignment06 Al_2 Te181.563077
lithium metazirconateLi_2 Zr O_3java Assignment06 Li_2 Zr O_3153.101000
acetylaldehydeC H_3 C H Ojava Assignment06 C H_3 C H O44.053000
methylamineC H_5 Njava Assignment06 C H_5 N31.058000
paracetamolC_8 H_9 N O_2java Assignment06 C_8 H_9 N O_2151.165000