Boolean Expressions and If Else Statements Post
Boolean expressions and if else statements post for AP CSA
2009 FRQ 3b
public class BatteryCharger {
// Initialized array containing the costs at each hour (0 - 23)
private int[] rateTable = {50, 60, 160, 60, 80, 100, 100, 120, 150, 150, 150, 200, 40, 240, 220, 220, 200, 200, 180, 180, 140, 100, 80, 60};
// Method for determining the total charge cost based on the given startHour and chargeTime
private int getChargingCost(int startHour, int chargeTime) {
int totalCost = 0;
for (int i = startHour; i < startHour + chargeTime; i++) {
if (i >= 24) {
int temp = i % 24;
totalCost += rateTable[temp];
} else {
totalCost += rateTable[i];
}
}
return totalCost;
}
// Method for determining the most cost efficient startHour based on the given chargeTime
public int getChargeStartTime(int chargeTime) {
int optimalStartHour = 23;
for (int i = 22; i >= 0; i--) {
if (getChargingCost(i, chargeTime) < getChargingCost(optimalStartHour, chargeTime)) {
optimalStartHour = i;
}
}
return optimalStartHour;
}
// Running method for visualizing outputs
public static void main(String[] args) {
BatteryCharger myObj = new BatteryCharger();
System.out.println(myObj.getChargingCost(22, 30));
System.out.println(myObj.getChargeStartTime(30));
}
}
BatteryCharger.main(null);
2017 FRQ 1b
public class Digits {
private ArrayList<Integer> digitList;
// Constructor that adds all digits of an integer to an ArrayList
public Digits(int num) {
Integer number = num;
digitList = new ArrayList<Integer>();
String numberNew = number.toString();
for (int i = 0; i < numberNew.length(); i++) {
int numberNewDigit = Character.getNumericValue(numberNew.charAt(i));
digitList.add(numberNewDigit);
}
}
// Method for checking if all the numbers in the digit list are strictly increasing
public boolean isStrictlyIncreasing() {
// Returns true if there is only 1 digit
if (digitList.size() < 2) {
return true;
}
for (int a = 0; a < digitList.size() - 1; a++) {
// Returns false if at least one of the digits does not follow the strictly increasing rule
if (digitList.get(a) >= digitList.get(a + 1)) {
return false;
}
}
return true;
}
// Running method for visualizing outputs
public static void main(String[] args) {
Digits myObj = new Digits(1234578);
System.out.println(myObj.isStrictlyIncreasing());
}
}
Digits.main(null);
2019 FRQ 3b
public class Delimiters {
private String openDel;
private String closeDel;
ArrayList<String> delimitersList;
// Constructor that initializes openDel, closeDel, and delimitersList
public Delimiters(String open, String close) {
openDel = open;
closeDel = close;
delimitersList = new ArrayList<String>();
}
// Method for collecting all delimiters from a text
public ArrayList<String> getDelimitersList (String[] tokens) {
for (String token : tokens) {
if (token == openDel || token == closeDel) {
delimitersList.add(token);
}
}
return delimitersList;
}
// Method to check whether or not the open delimiters and close delimiters are balanced.
public boolean isBalanced(ArrayList<String> delimiters) {
// Counters for the number of open delimiters and close delimiters
int openCount = 0;
int closeCount = 0;
for (String delimiter : delimiters) {
if (delimiter == openDel) {
openCount++;
} else if (delimiter == closeDel) {
closeCount++;
}
// First condition is broken
if (openCount < closeCount) {
return false;
}
}
// First and second condition are both met
if (openCount == closeCount) {
return true;
}
// Second condition is broken
else {
return false;
}
}
// Running method for visualizing outputs
public static void main(String[] args) {
Delimiters myObj = new Delimiters("(", ")");
String[] tokens = {"(", "yy", ")", "zz", "(", ")"};
ArrayList<String> delimiters = myObj.getDelimitersList(tokens);
System.out.println(delimiters);
System.out.println(myObj.isBalanced(delimiters));
}
}
Delimiters.main(null);