Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the following atomic weight carbon 12.0110 hydrogen 1.0079 oxygen 15.9994

ID: 3819216 • Letter: U

Question

Using the following atomic weight carbon 12.0110 hydrogen 1.0079 oxygen 15.9994 compute the molecular weight of a compound of carbon, hydrogen, and oxygen. Ask the user the number of atoms of each element with the following prompts in the order below, with a space after each colon: Enter the number of carbon atoms: Enter the number of hydrogen atoms: Enter the number of oxygen atoms: Compute the molecular weight by adding together the products of each atom type's molecular weight times its atom count, and print the result in a form as in the case for aspirin below: Enter the number of carbon atoms: 9 Enter the number of hydrogen atoms: 8 Enter the number of oxygen atoms: 4 The molecular weight is 180.1598 Note as usual that in the output used for comparison for the first test case above, the user input and the newline character after it will not be visible. There is a second test case with hidden input.

Explanation / Answer

public class Mol_weight {
Mol_weight(){
int c,h,o; // c no. of carbon ,h no. of hydrogen, o no. of oxygen
float cwt=12.0110f; // carbon atomic weight
float hwt=1.0079f; // hydrogen atomic weight
float owt=15.994f; // oxygen atomic weight
float mwt; // molecular weight
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of carbon atoms : ");
c=sc.nextInt();
System.err.print("Enter the number of hydrogen atoms : ");
h=sc.nextInt();
System.err.print("Enter the number of oxygen atoms : ");
o=sc.nextInt();
mwt=c*cwt+h*hwt+o*owt;
System.err.print("The molecular weight is : "+mwt);
}
public static void main(String[] args) {
new Mol_weight();
}
}

In this program the user input the no. of Carbon, Hydrogen & Oxygen atoms according to molecule, then it output the molecular weight of that molecule.