Boolean expressions and ifs lesson
Hacks from the presentation
// 09 - 3b
// We are assuming that getChargingCost() works well
public class BatteryCharger {
public int getChargeStartTime(int chargeTime) {
int starting = 0;
for (int j = 1; j < 24; j++) {
if (this.getChargingCost(j, chargeTime) < this.getChargingCost(starting, chargeTime)) starting = j;
}
return starting;
}
}
BatteryCharger bat = new BatteryCharger();
bat.getChargeStartTime(6);
// 17 - 1b
import java.util.*;
public class Digits {
private ArrayList<Integer> digitList;
public Digits(int num) {
digitList = new ArrayList<Integer>();
if (num==0){
digitList.add(new Integer(0));
}
while (num > 0) {
digitList.add(0, new Integer(num % 10));
num /= 10;
}
}
public boolean isStrictlyIncreasing() {
boolean increase = true;
for (int i = 0; i < digitList.size() - 1; i++) {
if (digitList.get(i).intValue() >= digitList.get(i + 1).intValue()) {
increase = false;
return increase;
}
}
return increase;
}
}
System.out.println(new Digits(1356).isStrictlyIncreasing());
System.out.println(new Digits(1536).isStrictlyIncreasing());
// 19 - 3b
public class Delimiters {
private String openDel;
private String closeDel;
public Delimiters(String open, String close) {
}
public ArrayList<String> getDelimitersList(String[] tokens){
}
public boolean isBalanced(ArrayList<String> delimiters) {
int opencount = 0;
int closecount = 0;
for (int i = 0; i < delimiters.size(); i++) {
if (delimiters.get(i) == openDel) {
opencount++;
}
else if (delimiter.get(i) == closeDel) {
closecount++;
}
if (closecount > opencount) {
return false;
}
}
return true;
}
}