HW ASSNS 2-6 Tri 1
Homework Assignments
//HW
//2006 FRQ
//2a
public double purchasePrice() {
return getListPrice() * (1+taxRate);
}
//3a
public int compareCustomer(Customer other)
{
int compare = getName().compareTo(other.getName());
if (compare == 0) {
return getID() - other.getID();
}
else {
return compare;
}
}
//UNIT 2 HW
//goblin hacks
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.lang.Math;
public class Duel {
public static void attack(Goblin attackerGoblin, Goblin attackeeGoblin) {
System.out.println(attackerGoblin.getName() + " attacks " + attackeeGoblin.getName() + "!");
if (0.8 > attackerGoblin.getHitChance()) {
attackeeGoblin.takeDMG(attackerGoblin.getDMG());
System.out.println(attackerGoblin.getName() + " hits!");
System.out.println(attackeeGoblin.getName() + " takes " + attackerGoblin.getDMG() + " damage");
} else {
System.out.println(attackerGoblin.getName() + " misses...");
}
System.out.println(attackeeGoblin.getName() + " HP: " + attackeeGoblin.getHP());
System.out.println();
}
public static void fight(Goblin goblin1, Goblin goblin2) {
while (goblin1.isAlive() && goblin2.isAlive()) {
attack(goblin1, goblin2);
if (!goblin1.isAlive()) {
System.out.println(goblin1.getName() + " has perished");
break;
}
attack(goblin2, goblin1);
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(0.50);
Goblin goblin2 = new Goblin();
goblin2.setName("Gunther the great");
goblin2.setHP(4);
goblin2.setDMG(1);
goblin2.setHitChance(1);
fight(goblin1, goblin2);
}
}
Duel.main(null);
//2017 1b
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 false;
return false;
}
//UNIT 3
//2009 3b
public int getChargeStartTime(int chargeTime)
{
int lowestPrice = 0;
for(int i = 1; i < rateTable.length; i++)
if(getChargingCost(startHour, chargeTime) <
getChargingCost(i, chargeTime))
i = startHour;
return i;
}
//2019 3b
public boolean isBalanced(ArrayList<String> delimiters) {
int openDels = 0;
int closeDels = 0;
for (String del : delimiters) {
if (del.equals(openDel)) {
openDels++;
} else {
closeDels++;
if (closeDels > openDels) {
return false;
}
}
return openDels == closeDels;
}
//unit 4 hw
//number guesser
import java.util.Scanner;
import java.lang.Math;
Scanner number = new Scanner(System.in);
float num_float = Integer.parseInt(number.nextLine()); // reads input
System.out.println(number_float);
double random = Math.random() * 10;
System.out.println(random);
public class Number{
public static void main(String[] args){
while (num_float != random){
if (num_float < random){
System.out.println("too low. higher");
// Scanner number = new Scanner(System.in);
float num_float = Integer.parseInt(number.nextLine());
System.out.println(number_float);
}
if (number_float > random){
System.out.println("too high. lower");
Scanner number = new Scanner(System.in);
float num_float = Integer.parseInt(number.nextLine());
System.out.println(num_float);
}
}
System.out.println("you got it!");
}
}
Number.main(null)
unit 4 hw
Quiz:
//unit 5
//1a
public int scoreGuess( String guess ) {
int val = 0;
int len = guess.length();
for( int i = 0; i <= secret.length()-len; i+=1){
String ck = secret.substring( i, i+len );
if( ck.equals(guess) ){
val++;
}
}
return val*len*len;
}
//3a
public void addMembers(
String[] names, int gradYear ){
for( String n : names ){
memberList.add(new MemberInfo( n, gradYear, true) );
}
}
import java.util.Arrays;
public class unit6hw {
public static void main(String[] args)
{}}
int[] array1 = {92, 38, 23, 90, 12, 54, 72};
System.out.println("Original Array: "+Arrays.toString(array1));
int x = array1[0];
array1[0] = array1[array1.length-1];
array1[array1.length-1] = x;
System.out.println("New array: "+Arrays.toString(array1));
//swaps first/last elements
//all even values to 0
import java.util.Arrays;
public class unit6hw {
public static void main(String[] args)
{}}
int[] array1;
array1 = new int[7];
int array1[] = {92, 38, 23, 90, 65, 54, 72, 99};
// const int size = 7;
// int array1[size] = { 1,2,3,4,5,6,7 };
for (int i = 0; i < array1.length; i++)
{
if (array1[i] % 2 == 0)
array1[i] = 0;
System.out.println("New array: "+Arrays.toString(array1)); }