Question

In: Computer Science

JAVA Program: Computes The Molar Mass of a chemical Formula. You shall write a program that:...

JAVA Program: Computes The Molar Mass of a chemical Formula.

You shall write a program that:
-Loads chemical-element data by scanning through the file (accessed via file path or URL) exactly once.
FILENAME: Elements.txt
-Expects one or more command-line arguments that constitute a chemical formula, as described below.
*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.
Example:
C_4
C O_2
C_8 H_9 N O_2

-Prints one line containing the molar mass of the compound described by the formula, to six digits after the decimal point.

*Must define two classes(ChemicalElement and MolarMass).
Make use of string.split and Integer.parseInt to seperate the element name from the number of atoms.

-The 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)
File(Elements.txt)

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

Solutions

Expert Solution

*******************Summary**********************

The program for above question is given below with comments and output ...using the concepts mentioned....

However, the program could be completed without using the class...but it was mentioned to use the class so i have used it in the program....

I hope it works for you :) !!!!!!!!!!

*******************Program*********************

import java.util.*;
import java.io.File; 
import java.io.FileNotFoundException; 

//atom class
class Atom
{  //variables for storing atom details
     private int AtomicNo;
     private String Symbol;
     private String Name;
     private Double AtomicMass;
     private int Period;
     private int Group;
     
     //constructor for atom class
     public Atom(int AtomicNo,String Symbol,String Name,Double AtomicMass,int Period,int Group)
     {
          this.AtomicNo=AtomicNo;
          this.Symbol=Symbol;
          this.Name=Name;
          this.AtomicMass=AtomicMass;
          this.Period=Period;
          this.Group=Group;
     }
     
     //getters for the values of variables of atom
     public int getAtomicNo(){return AtomicNo;}
     public String getSymbol(){return Symbol;}
     public String getName(){return Name;}
     public Double getAtomicMass(){return AtomicMass;}
     public int getPeriod(){return Period;}
     public int getGroup(){return Group;}

}

public class Main
{
public static void main(String[] args) 
{
    int i=0;  
    double mass=0;    //variable for storing mass
    String[] atom=new String[args.length];    //storing the atoms of command line in array
    int[] noAtom=new int[args.length];   //array for storing number of molecules of atom
          
    while(i<args.length)
    {
        try
        {
          atom[i]=args[i].substring( 0, args[i].indexOf("_"));  //spliting the atom symbol part of args[i] and storing in string array
          noAtom[i]=Integer.parseInt(args[i].substring(args[i].indexOf("_")+1, args[i].length()));
          //splitting the no. of molecule part and storing in int array
        }catch(Exception e)    //if exception occurs..that means if no.of molecules is not mentuioned then
        {
          atom[i]=args[i];  //store args[i] in string array
          noAtom[i]=1;  //store no. of molecules as 1 in int array
        }
            i++;
    }
          
    try {

File file = new File("Elements.txt");    //opening file
          Scanner fileReader = new Scanner(file);    //setting reader for file 
          ArrayList<Atom> atoms=new ArrayList<>();  //creating array list for storing all atoms 
          
          while (fileReader.hasNextLine())  //read file until the file has next line
          {
              //reading the atom info from file and storing in variables
               int AtomicNo=fileReader.nextInt();
               String Symbol=fileReader.next();
               String Name=fileReader.next();
               Double AtomicMass=fileReader.nextDouble();
               int Period=fileReader.nextInt();
               int Group=fileReader.nextInt();
                
               //creating object of Atom and instantiating it and calling constructor with the read values from file
               Atom a=new Atom(AtomicNo,Symbol,Name,AtomicMass,Period,Group);
               //adding atom in arraylist of atoms
               atoms.add(a);
          }
            fileReader.close();  //closing file
          
          
          i=0;
          while(i<atoms.size())  //iterate through all the atoms in atoms arraylist
          {
               for(int j=0;j<args.length;j++)    //iterate through command line atoms
               if(atoms.get(i).getSymbol().equals(atom[j]))  
               //check if arraylist atoms[i] is one of the items in command line
               {
                 //if atoms[i] is in command line then add the molecular mass of atom to mass
                    mass=mass+(atoms.get(i).getAtomicMass()/noAtom[j]);
               }
               i++;
          }
          } catch (FileNotFoundException e) {
               System.out.println("File not found");
               e.printStackTrace();
          }
          
          //Printing molar mass
          System.out.print("Molar mass of ");
          
          //printing the compound in command line
          for (String arg : args) 
               System. out.print(arg);
               
          System.out.print(" : ");
           System.out.format("%.6f", mass);  //printing molar mass upto 6 decimals

}
}

*****************Output******************

****PLEASE UPVOTE****


Related Solutions

Write a Java program for a simple bank account. You shall define a Customer class: A...
Write a Java program for a simple bank account. You shall define a Customer class: A customer has a first name, last name, and social security number. The social security number is a String variable and must comply with this format: xxx-xx-xxxx where 'x' is a digit between 0-9. If a customer is supplied with an invalid SSN, a message must be printed stating SSN of the customer is invalid; however, the account still is created. You shall define a...
Assignment You shall write a program that: Loads chemical-element data by scanning through the file (accessed...
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...
Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the...
Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the largest and smallest of three integer values. The three values are –50, 53, and 78. Instructions Two variables named largest and smallest are declared for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate. Write the rest of the program using assignment statements, if...
You have to write a program that computes the area of a triangle. Input consists of...
You have to write a program that computes the area of a triangle. Input consists of the three points that represent the vertices of the triangle. Points are represented as Cartesian units i.e. X,Y coordinates as in (3,5). Output must be the three points, the three distances between vertices, and the area of the triangle formed by these points. The program must read the coordinates of each point, compute the distances between each pair of them and print these values....
If you know the formula of a substance, how can you determine its molar mass? Note:...
If you know the formula of a substance, how can you determine its molar mass? Note: Chemists often use the term weight for mass.
Write the chemical formula of the following compounds
Write the chemical formula of the following compounds Name of the compound Individual ions Chemical formula Calcium iodide Copper(II) carbonate Potassium sulfite Lead(IV) phosphide Tin(IV) dichromate
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
Write the correct chemical formula for each of the following.
Write the correct chemical formula for each of the following. a) Calcium sulfate trihydrate b) Sulfide ion c) Nitrogen dioxide d) Lead (IV) hypochlorite e) Ammonium sulfite f) Barium chloride g) sodium hydride h) sulfur trioxide i) sulfite ion
java programing project Implement a program that computes the Fibonacci of a specified number, the factorial...
java programing project Implement a program that computes the Fibonacci of a specified number, the factorial of a specified number, or estimates the value of 'e' using the specified number of iterations (of a Taylor series). Please feel free to use the Internet to find resources that explain how to estimate the value of 'e' using a Taylor series. In the case of no or invalid number of parameters, the program should show help instructions that looks like: --- Assign...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT