Using Primitives and Objects HW (Unit 1&2)
Homework Units 1 and 2
- Unit 1 CB Homework 2006 FRQ Questions & Scoring Guidelines 2006 FRQ Questions 2006 FRQ Solutions
- Unit 2 CB Homework None
Unit 1 HW: 2006 FRQ 1, 2a, 3a
//
# 1a)
public boolean conflictsWith(Appointment other){
TimeInterval a = this.getTime(), b = other.getTime();
return a.overlapsWith(b);
}
# 1b)
public void clearConflicts(Appointment appt){
for(int i = apptList.size() - 1; i >= 0; i--) {
Appointment ab = apptList.get(i);
if(ab.conflictsWith(appt)) {
apptList.remove(i);
}
}
}
# 1c)
public boolean addAppt(Appointment appt, boolean emergency){
if(emergency){
clearConflicts(appt);
return apptList.add(appt);
} else {
boolean conflict = false;
for(Appointment a : apptList)
if(a.conflictsWith(appt))
conflict = true;
if(conflict) {
return false;
} else {
return apptList.add(appt);
}
}
}
# 2a)
public double purchasePrice(rate){
double calculatedPrice = getListPrice() * (1 - TaxableItem(rate));
return calculatedPrice;
}
# 3a)
public int compareCustomer(Customer other){
int nameCompare = this.getName().compareTo(other.getName());
if(nameCompare == 0){
return this.getID() - other.getID();
} else {
return nameCompare;
}
}
//Unit2HW
import java.util.Random;
public class Goblin {
private String name;
private int HP;
private int DMG;
private double hitChance;
public String getName() {
return name;
}
public int getHP() {
return HP;
}
public int getDMG() {
return DMG;
}
public double getHitChance() {
return hitChance;
}
public boolean isAlive() {
if (this.HP > 0) {
return true;
} else {
return false;
}
}
public void setName(String newName) {
this.name = newName;
}
public void setHP(int newHP) {
this.HP = newHP;
}
public void takeDMG(int takenDamage) {
this.HP -= takenDamage;
}
public void setDMG(int newDMG) {
this.DMG = newDMG;
}
public void setHitChance(double newHitChance) {
this.hitChance = newHitChance;
}
}
import java.util.Random;
public class Duel {
public static void fight(Goblin goblin1, Goblin goblin2, Double rand) {
while (goblin1.isAlive() && goblin2.isAlive()) {
for(int i = 0; i < 5; i++) {
if (rand > goblin1.getHitChance()) {
System.out.println("Missed");
} else {
System.out.println("Hit");
break;
}}
goblin1.takeDMG(goblin2.getDMG());
System.out.println(goblin1.getName() + " takes " + goblin2.getDMG() + " damage");
System.out.println(goblin1.getName() + " HP: " + goblin1.getHP());
if (!goblin1.isAlive()) {
System.out.println(goblin1.getName() + " has perished");
break;
}
goblin2.takeDMG(goblin1.getDMG());
System.out.println(goblin2.getName() + " takes " + goblin1.getDMG() + " damage");
System.out.println(goblin2.getName() + " HP: " + goblin2.getHP());
if (!goblin2.isAlive()) {
System.out.println(goblin2.getName() + " has perished");
break;
}
}
}
public static void main(String[] args) {
Goblin goblin1 = new Goblin();
goblin1.setName("jeffrey");
goblin1.setHP(12);
goblin1.setDMG(2);
goblin1.setHitChance(2);
Goblin goblin2 = new Goblin();
goblin2.setName("Gunther the great");
goblin2.setHP(4);
goblin2.setDMG(1);
goblin2.setHitChance(1);
Random x = new Random();
double rand = x.nextDouble();
fight(goblin1, goblin2, rand);
}
}
Duel.main(null);