From a64ffd7ffc5cc28cabecd51e96102d3e830709b5 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Fri, 15 May 2020 16:51:02 -0400 Subject: added AP CSA answers --- ap-computer-science-a-exam/QuestionOne.java | 82 +++++++++++++++++++++++++++++ ap-computer-science-a-exam/q1.txt | 40 ++++++++++++++ ap-computer-science-a-exam/q2.txt | 28 ++++++++++ 3 files changed, 150 insertions(+) create mode 100644 ap-computer-science-a-exam/QuestionOne.java create mode 100644 ap-computer-science-a-exam/q1.txt create mode 100644 ap-computer-science-a-exam/q2.txt 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 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()); + } +} diff --git a/ap-computer-science-a-exam/q1.txt b/ap-computer-science-a-exam/q1.txt new file mode 100644 index 0000000..fe2b68e --- /dev/null +++ b/ap-computer-science-a-exam/q1.txt @@ -0,0 +1,40 @@ +WY5V8181 +HR + +(a) + +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; +} + +(b) + +public String getCompoundWithHighestAvg(){ + double max = -1; // less than all non-negative temperatures. + String compound; + for (String curCompound : getCompoundList()){ + double avg = getCompoundAvg(curCompound); + if (avg > max){ + max = avg; + compound = curCompound; + } + } + return compound; +} + +(c) + +The programmer would need to add the amount of compound used as a +private instance variable (with a getter) in Trial (which would be +initialized in its constructor). ScienceExperiment would hold the method +`public double getCompoundAmountUsed(String compoundName)` which would +sum, for all Trials in trialList with the same compoundName as given, +the value of trial.getAmountUsed(), and return that value. diff --git a/ap-computer-science-a-exam/q2.txt b/ap-computer-science-a-exam/q2.txt new file mode 100644 index 0000000..d9ae2aa --- /dev/null +++ b/ap-computer-science-a-exam/q2.txt @@ -0,0 +1,28 @@ +WY5V8181 +HR + +(a) + +public static boolean runSimulation(int sampleSize){ + int half = sampleSize/2; + int halves[] = { 0, 0 }; + for (int i = 0; i < 2; i++){ + for (int j = 0; j < half; j++){ + int num = getInt(); + if (num > 0 && isTarget(num)) halves[i]++; + } + } + return (halves[0] > halves[1]); +} + +(b) + +The programmer would, instead of using `int sampleSize` as a parameter, +use a `public final static int sampleSize = $value` where $value is the +value the programmer wants it to be so that it could be set at compile +time. If the programmer wants it to be set at runtime instead, the +programmer could use a basic class-wide declaration `private static int` +and setter public static void setSampleSize(int size) which would only +set the variable if it is non-null. Note that static is used for all +variables and methods because the class does not appear to use +instances. -- cgit