0

I'm trying to figure out how to get the relativeAtomicMass from one class named ChemicalElement onto another called MolarMass in which I try to figure out the atomicmass of an element given it's symbol then find out a compound mass. I'm not sure how to return a double, I tried using a toString to do it since that's what a tutor taught me to do but I'm not sure if it's the correct way that my teacher wants me to do it. I know I need to get the relativeAtomicMass onto MolarMass to finish the assignment but i'm stuck at the moment. Heres the code for my assignment:

package chemical;
public class ChemicalElement {

  // Properties (fields/variables): This time we leave give them default visibility, which gives
  // other classes in the same package (i.e. the same directory) full access to them, for brevity.

  /** The number of protons */
  int atomicNumber;
  /** 1-3 letter IUPAC symbol */
  String symbol;
  /** Full name */
  String name;
  /** Relative atomic mass () */
  double relativeAtomicMass;
  /** Row in the periodic table */
  int period;
  /** Column in the periodic table */
  int group;

  // Actions (methods): For now we provide a constructor to initialize all fields.

  /**
   * Constructs a new ChemicalElement object with the given properties.
   * 
   * @param atomicNumber the number of protons
   * @param symbol the IUPAC symbol
   * @param name the full name
   * @param relativeAtomicMass the average atomic mass relative to carbon-12
   * @param period the row in the periodic table
   * @param group the column in the periodic table
   */
  public ChemicalElement(int atomicNumber, String symbol, String name, double relativeAtomicMass,
      int period, int group) {
    this.atomicNumber = atomicNumber;
    this.symbol = symbol;
    this.name = name;
    this.relativeAtomicMass = relativeAtomicMass;
    this.period = period;
    this.group = group;
  }
  public String toString()
  {
      return name;
  }
}
package chemical;
import java.net.URL;
import java.util.Scanner;
import java.util.Arrays;


public class MolarMass {

  public static void main(String[] compoundTokens) throws Exception {
    ChemicalElement[] elements = readElementData();
    //System.out.printf("%.6f%n", compoundmass(elements, compoundTokens));
   System.out.println(Arrays.toString(elements));
  }

  /**
   * Loads data about all chemical elements into an array.
   * 
   * @return an array containing references to objects describing all 118 chemical elements
   * @throws Exception if anything goes awry (we only know how to pass the buck)
   */
  private static ChemicalElement[] readElementData() throws Exception {
      ChemicalElement[] elements = new ChemicalElement[119];

        Scanner dataFile =
            new Scanner(new URL("http://jeff.cis.cabrillo.edu/datasets/elements").openStream());
        while (dataFile.hasNext()) {
          // Example line: 1 H Hydrogen 1.008 1 1
          int atomicNumber = dataFile.nextInt();
          elements[atomicNumber] = new ChemicalElement(atomicNumber, dataFile.next(), dataFile.next(),
              dataFile.nextDouble(), dataFile.nextInt(), dataFile.nextInt());
        }
         return elements;

  }

  /**
   * Determines the molar mass of a compound.
   * 
   * @param elements an array containing references to objects describing all 118 chemical elements
   * @param tokens a compound formula
   * @return the molar mass of the given compound
   */
 // private static double compoundMass(ChemicalElement[] elements, String[] tokens) {
    // TODO


  /**
   * Determines the relative atomic mass of an element, given its symbol.
   * 
   * @param elements an array containing references to objects describing all 118 chemical elements
   * @param symbol the symbol of the element in question
   * @return the relative atomic mass of the given element
   */
//  private static double relativeAtomicMass(ChemicalElement[] elements, String symbol) {
    // TODO
  }

Also i'd imagine that once I get the relative atomic mass onto molarmass it would be easy to fill out the code for relativeatomicmass but how would I use that to create compounds? Thanks any help would be appreciated.

carlos
  • 17
  • 4
  • For the first part, you can read it straight from the object `element.relativeAtomicMass`. That's what the comment "This time we leave give them default visibility..." meant. In general you can create a [getter method](https://stackoverflow.com/q/2036970) to expose a given property in a read-only way. – Rup Nov 03 '19 at 23:03
  • Would I use a system.out.println(element.relativeAtomicMass) in MolarMass class? thanks – carlos Nov 03 '19 at 23:08
  • Well both of the TODO methods accept an array of elements so you'll have to `for (ChemicalElement element : elements) { ... }` first, to get the individual elements to work with. Then the println would work inside that loop, yes, but you're supposed to be summing the weights I think not just printing them. – Rup Nov 03 '19 at 23:52

0 Answers0