如何将相对原子质量分配到另一个类中?以及使用它来制造化合物

我试图弄清楚如何从一个名为ChemicalElement的类中获取relativeAtomicMass到另一个名为MolarMass的类中,在该类中,我试图找出给定元素符号的元素的原子质量,然后找出复合质量。我不确定如何返回双精度数,我尝试使用toString来做到这一点,因为那是导师教我做的事情,但是我不确定这是否是我老师想要我做的正确方法。我知道我需要将relativeAtomicMass放入MolarMass才能完成任务,但此刻我陷入了困境。这是我分配的代码:

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.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
  }

我也想像过,一旦我将相对原子质量加到摩尔质量上,就很容易填写相对原子质量的代码,但是我将如何使用它来创建化合物呢? 谢谢您的帮助。

wei988168 回答:如何将相对原子质量分配到另一个类中?以及使用它来制造化合物

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3169832.html

大家都在问