In: Computer Science
Assignment
You shall write a program that:
Loads chemical-element data by scanning through the
file (accessed via file path or URL) exactly once.
Expects one or more command-line arguments that
constitute a chemical formula, as described above.
Prints one line containing the molar mass of the
compound described by the formula, to six digits after the decimal
point.
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:
Atomic number (int)
Symbol (String)
Name (String)
Relative atomic mass (double)
Period (row in periodic table) (int)
Group (column in periodic table) (int)
FYI, this file is also available via URL
http://jeff.cis.cabrillo.edu/datasets/elements
Chemical Formulas
Quoth the arbiter 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:
One or more whitespace-delimited tokens
Each token consists of either:
An element symbol, implying one atom of that element
in the compound
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
FormulacarbonCCdioxygenO2O_2dialuminum
tellurideAl2TeAl_2 Telithium
metazirconateLi2ZrO3Li_2 Zr
O_3acetaldehydeCH3CHOC H_3 C H
OmethylamineCH5NC H_5
NparacetamolC8H9NO2C_8 H_9 N
O_2propaneC3H8C_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 acetaldehyde (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 are gmolgmol in
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 the String class that
you may find useful. Probably the most straightforward is
String.split, with which you can create an array of Strings by
splitting one string around a pattern of characters.
In addition, Integer.parseInt will certainly be
useful, as it returns the int value represented by the characters
in a String.
For example:
String compound = "O_2"; String[] components = compound.split("_");
// Value assigned is a reference to a two-element array: { "O", "2"
} String element = components[0]; // Value assigned is a reference
to a string containing "O" int atoms =
Integer.parseInt(components[1]); // Value assigned is 2
Also, don't forget that if you want to determine
whether two Strings contain the same sequence of
characters:
oneString == anotherString // Do oneString and anotherString refer
to the same String object? oneString.equals(anotherString) // Do
oneString and anotherString contain the same character
sequences?
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.
Select Run–>Run Configurations or Run–>Debug
Configurations as desired.
Select the Arguments tab.
In the Program Arguments section , Enter your
arguments.
Click Apply.
this is the contacts of file needed
1 H Hydrogen 1.008 1 1 2 He Helium 4.002602 1 18 3 Li Lithium 6.94
2 1 4 Be Beryllium 9.0121831 2 2 5 B Boron 10.81 2 13 6 C Carbon
12.011 2 14 7 N Nitrogen 14.007 2 15 8 O Oxygen 15.999 2 16 9 F
Fluorine 18.998403163 2 17 10 Ne Neon 20.1797 2 18 11 Na Sodium
22.98976928 3 1 12 Mg Magnesium 24.305 3 2 13 Al Aluminium
26.9815385 3 13 14 Si Silicon 28.085 3 14 15 P Phosphorus
30.973761998 3 15 16 S Sulfur 32.06 3 16 17 Cl Chlorine 35.45 3 17
18 Ar Argon 39.948 3 18 19 K Potassium 39.0983 4 1 20 Ca Calcium
40.078 4 2 21 Sc Scandium 44.955908 4 3 22 Ti Titanium 47.867 4 4
23 V Vanadium 50.9415 4 5 24 Cr Chromium 51.9961 4 6 25 Mn
Manganese 54.938044 4 7 26 Fe Iron 55.845 4 8 27 Co Cobalt
58.933194 4 9 28 Ni Nickel 58.6934 4 10 29 Cu Copper 63.546 4 11 30
Zn Zinc 65.38 4 12 31 Ga Gallium 69.723 4 13 32 Ge Germanium 72.63
4 14 33 As Arsenic 74.921595 4 15 34 Se Selenium 78.971 4 16 35 Br
Bromine 79.904 4 17 36 Kr Krypton 83.798 4 18 37 Rb Rubidium
85.4678 5 1 38 Sr Strontium 87.62 5 2 39 Y Yttrium 88.90584 5 3 40
Zr Zirconium 91.224 5 4 41 Nb Niobium 92.90637 5 5 42 Mo Molybdenum
95.95 5 6 43 Tc Technetium 98 5 7 44 Ru Ruthenium 101.07 5 8 45 Rh
Rhodium 102.9055 5 9 46 Pd Palladium 106.42 5 10 47 Ag Silver
107.8682 5 11 48 Cd Cadmium 112.414 5 12 49 In Indium 114.818 5 13
50 Sn Tin 118.71 5 14 51 Sb Antimony 121.76 5 15 52 Te Tellurium
127.6 5 16 53 I Iodine 126.90447 5 17 54 Xe Xenon 131.293 5 18 55
Cs Caesium 132.90545196 6 1 56 Ba Barium 137.327 6 2 57 La
Lanthanum 138.90547 6 0 58 Ce Cerium 140.116 6 0 59 Pr Praseodymium
140.90766 6 0 60 Nd Neodymium 144.242 6 0 61 Pm Promethium 145 6 0
62 Sm Samarium 150.36 6 0 63 Eu Europium 151.964 6 0 64 Gd
Gadolinium 157.25 6 0 65 Tb Terbium 158.92535 6 0 66 Dy Dysprosium
162.5 6 0 67 Ho Holmium 164.93033 6 0 68 Er Erbium 167.259 6 0 69
Tm Thulium 168.93422 6 0 70 Yb Ytterbium 173.045 6 0 71 Lu Lutetium
174.9668 6 3 72 Hf Hafnium 178.49 6 4 73 Ta Tantalum 180.94788 6 5
74 W Tungsten 183.84 6 6 75 Re Rhenium 186.207 6 7 76 Os Osmium
190.23 6 8 77 Ir Iridium 192.217 6 9 78 Pt Platinum 195.084 6 10 79
Au Gold 196.966569 6 11 80 Hg Mercury 200.592 6 12 81 Tl Thallium
204.38 6 13 82 Pb Lead 207.2 6 14 83 Bi Bismuth 208.9804 6 15 84 Po
Polonium 209 6 16 85 At Astatine 210 6 17 86 Rn Radon 222 6 18 87
Fr Francium 223 7 1 88 Ra Radium 226 7 2 89 Ac Actinium 227 7 0 90
Th Thorium 232.0377 7 0 91 Pa Protactinium 231.03588 7 0 92 U
Uranium 238.02891 7 0 93 Np Neptunium 237 7 0 94 Pu Plutonium 244 7
0 95 Am Americium 243 7 0 96 Cm Curium 247 7 0 97 Bk Berkelium 247
7 0 98 Cf Californium 251 7 0 99 Es Einsteinium 252 7 0 100 Fm
Fermium 257 7 0 101 Md Mendelevium 258 7 0 102 No Nobelium 259 7 0
103 Lr Lawrencium 266 7 3 104 Rf Rutherfordium 267 7 4 105 Db
Dubnium 268 7 5 106 Sg Seaborgium 269 7 6 107 Bh Bohrium 270 7 7
108 Hs Hassium 269 7 8 109 Mt Meitnerium 278 7 9 110 Ds
Darmstadtium 281 7 10 111 Rg Roentgenium 282 7 11 112 Cn
Copernicium 285 7 12 113 Nh Nihonium 286 7 13 114 Fl Flerovium 289
7 14 115 Mc Moscovium 290 7 15 116 Lv Livermorium 293 7 16 117 Ts
Tennessine 294 7 17 118 Og Oganesson 294 7 18