Continue familiarizing yourself with:
- Classes and instantiation
- Text file input
- Command-line arguments
This assignment requires knowledge of the material presented in class throughweek 07and/or through the introductory materials on OOP in the textbooks.
There is a file on the server that is relevant to this assignment:
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)
- Atomic weight (double)
- Period (row in periodic table) (int)
- Group (column in periodic table) (int)
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:
- One or more whitespace-delimited tokens
- 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
|Compound||Standard Formula||Our Formula|
|dialuminum telluride||Al2Te||Al_2 Te|
|lithium metazirconate||Li2ZrO3||Li_2 Zr O_3|
|acetylaldehyde||CH3CHO||C H_3 C H O|
|methylamine||CH5N||C H_5 N|
|paracetamol||C8H9NO2||C_8 H_9 N O_2|
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.
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.
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.
You shall write a program that:
- 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
- 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.
The following examples assume a class named Assignment.
|carbon||C||java Assignment06 C||12.011000|
|dioxygen||O_2||java Assignment06 O_2||31.998000|
|dialuminum telluride||Al_2 Te||java Assignment06 Al_2 Te||181.563077|
|lithium metazirconate||Li_2 Zr O_3||java Assignment06 Li_2 Zr O_3||153.101000|
|acetylaldehyde||C H_3 C H O||java Assignment06 C H_3 C H O||44.053000|
|methylamine||C H_5 N||java Assignment06 C H_5 N||31.058000|
|paracetamol||C_8 H_9 N O_2||java Assignment06 C_8 H_9 N O_2||151.165000|