Need this program in Java Make a program that calculates the grams of a molecule
ID: 3802599 • Letter: N
Question
Need this program in Java
Make a program that calculates the grams of a molecule according to the given moles. They will have a private attribute called molecule which will be String. Tip Use the List <E> class. Assume that your molecule has only one-letter elements (eg, H, C, O, and not Fe, Ca, etc.). Output: -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --- Which molecule do you want to look for the grams? CO2 How many moles does the CO2 molecule have? 4.55 Enter the atomic mass for each element: Atomic mass for C: 12 Atomic mass for O: 16 Molecular mass of CO2 is 44 grams. There are 200.2 grams of CO2 in 4.55 moles.
Explanation / Answer
//Molecule class containg molecule,atomicwt and its getters and setters methods
package molecule;
public class Molecule
{
public String molecule;
public double atomicwt;
public molecule(String molecule,double atomicwt )
{
this.molecule=molecule;
this.atomicwt=atomicwt;
}
public String getMolecule() {
return molecule;
}
public void setMolecule(String molecule) {
this.molecule = molecule;
}
public double getAtomicwt() {
return atomicwt;
}
public void setAtomicwt(double atomicwt) {
this.atomicwt = atomicwt;
}
}//End of Molecule Class
//MoleculeDB class for Saving The data of molecules
package molecule;
import java.util.ArrayList;
public class MoleculeDB
{
private static ArrayList<molecule> moles = new ArrayList<>(100);
public static ArrayList<molecule> getmoles() //saving data to an arraylist
{
moles.add(new molecule("C",12));
moles.add(new molecule("O",16));
return moles;
}
public static double getAtomic(char mole) //to return the atomic value
{
String mol=Character.toString(mole);
double Atomic=0;
for(molecule m : moles)
{
if(m.molecule.equals(mol))
{
Atomic=m.getAtomicwt();
}
}
return Atomic;
}
}//end of MoleculeDB class
//MoleculeAct class from where Actual program is run
package molecule;
import java.util.Scanner;
public class MoleculeAct {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
moleculeDB.getmoles();
String smolecule;
int length;
double gmoles;
System.out.println("Enter the molecule u want chech for gram(USE CAPITAL LETTERS):");
smolecule=sc.nextLine();
System.out.println("Enter How Many Moles does "+smolecule+" has:");
gmoles=sc.nextDouble();
length=smolecule.length();
calculate(smolecule,gmoles,length);
}
private static void calculate(String smolecule, double gmoles,int length) //calculation of molecular mass and its value grams
{
char m;
double moleculadd=0,add=0;
for(int iloop=0;iloop<length;iloop++)
{
int n;
m=smolecule.charAt(iloop);
n=Character.getNumericValue(m);
if(n>1 && n<10) //if it contains more then one molecule here O has two molecule
{
for(iloop=1;iloop<n;iloop++)
{
moleculadd=moleculadd+add;
}
}
else
{
add=moleculeDB.getAtomic(m); //here add contains atomic value
moleculadd=moleculadd+add; //addition of molecules
}
}
System.out.println("Molecular Mass of "+smolecule+" is "+moleculadd);
moleculadd=moleculadd*gmoles;
System.out.println("There are "+moleculadd+" grams of "+smolecule+" in "+gmoles+" moles.");
}
}//end of MoleculeAct class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.