import java.util.ArrayList; class Trial { private int trialNum; private double temperature; private String compoundName; private double amountUsed; public Trial(int num, double temp, String compound, double amt){ trialNum = num; temperature = temp; compoundName = compound; amountUsed = amt; } public int getTrialNum(){ return trialNum; } public double getTemp(){ return temperature; } public String getCompound(){ return compoundName; } public double getAmtUsed(){ return amountUsed; } } class ScienceExperiment { private ArrayList trialList; public double getCompoundAvg(String comp){ int ct = 0; double totalTemp = 0; for (Trial trial : trialList){ if (trial.getCompound() != comp) continue; ct++; totalTemp += trial.getTemp(); } if (ct > 0) return totalTemp/ct; else return -1; } public ArrayList getCompoundList(){ ArrayList out = new ArrayList(); for (Trial trial : trialList){ if (!out.contains(trial.getCompound())) out.add(trial.getCompound()); } return out; } public String getCompoundWithHighestAvg(){ double max = -1; // less than all non-negative temperatures. String compound = ""; // to make java behave for (String curCompound : getCompoundList()){ double avg = getCompoundAvg(curCompound); if (avg > max){ max = avg; compound = curCompound; } } return compound; } public void add(Trial trial){ trialList.add(trial); } public ScienceExperiment(){ trialList = new ArrayList(); } public double getCompoundAmountUsed(String compound){ double sum = 0; for (Trial trial : trialList){ if (trial.getCompound() == compound) sum += trial.getAmtUsed(); } return sum; } } public class QuestionOne { public static void main(String[] args){ ScienceExperiment exp = new ScienceExperiment(); exp.add(new Trial(1,70,"alkynes",1)); exp.add(new Trial(27,100,"ketones",15)); exp.add(new Trial(29,80,"ketones",2)); exp.add(new Trial(35,90,"ethers",3)); System.out.println(exp.getCompoundAvg("alkynes")); System.out.println(exp.getCompoundAvg("ketones")); System.out.println(exp.getCompoundAvg("thiol")); System.out.println(exp.getCompoundAmountUsed("ketones")); System.out.println(exp.getCompoundWithHighestAvg()); } }