aboutsummaryrefslogtreecommitdiff
path: root/ap-computer-science-a-exam/QuestionOne.java
blob: 3b61fabedbc609ada8c5111726636e0f13676ce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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());
    }
}