In: Computer Science
*******************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****