diff options
Diffstat (limited to 'ap-computer-science-a-exam/QuestionOne.java')
-rw-r--r-- | ap-computer-science-a-exam/QuestionOne.java | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/ap-computer-science-a-exam/QuestionOne.java b/ap-computer-science-a-exam/QuestionOne.java new file mode 100644 index 0000000..3b61fab --- /dev/null +++ b/ap-computer-science-a-exam/QuestionOne.java @@ -0,0 +1,82 @@ +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<Trial> 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<String> getCompoundList(){ + ArrayList<String> out = new ArrayList<String>(); + 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<Trial>(); + } + 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()); + } +} |