Lesson Hacks Throughout
// Classes
public class WordMatch {
String secret;
public WordMatch(String secret) {
this.secret = secret;
}
public void scoreGuess(String guess) {
int counter = 0;
for (int i = 0; i < this.secret.length(); i++){
for (int j = i + 1; j < this.secret.length() + 1; j++) {
if (guess.equals(this.secret.substring(i,j))) {
counter++;
}
}
}
int points = counter * guess.length() * guess.length();
System.out.println("" + guess + " = " + points);
}
}
// Boolean Expressions
// 2009 3b
public class BatteryCharger {
public int getChargeStartTime(int chargeTime) {
int starting = 0;
//for loop to find the least
for (int j = 1; j < 24; j++) {
if (this.getChargingCost(j, chargeTime) < this.getChargingCost(starting, chargeTime)) starting = j;
}
return starting;
}
}
// Make new battery
BatteryCharger bat = new BatteryCharger();
bat.getChargeStartTime(6);
//2017 1b
import java.util.*;
public class Digits {
public 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());
//2019 3b
// 19 - 3b
public class Delimiters {
public String openDel;
public String closeDel;
public Delimiters(String open, String close) {
}
public ArrayList<String> getDelimitersList(String[] tokens){
//empty array
}
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;
}
}
// Iteration Homework
import java.util.*;
class Main{
public static int GuessGame(){
int randomint = (int)(Math.random()*(100-1+1)+1);
Scanner scan = new Scanner(System.in);
System.out.println("Guess a number from 1 to 100");
int guess = scan.nextInt();
int count = 0;
//While loop until the player gets it
while(guess != randomint){
if(guess > randomint && guess <= 100){
System.out.println("Too high");
System.out.println("Guess a number in range 1 to 100");
count++;
guess = scan.nextInt();
}else if(guess < randomint && guess >= 1){
System.out.println("Too Low");
System.out.println("Guess a number in range 1 to 100");
count++;
guess = scan.nextInt();
//this is just in case the player guesses outside
}else{
System.out.println("Number outside predefined range");
System.out.println("Guess a number in range 1 to 100");
guess = scan.nextInt();
}
}
System.out.println("nice");
return(count);
}
public static void main(String[] args){
System.out.println("You guessed: " + GuessGame() + " times");
}
}
Main.main(null);
// Primitives
//2006 Full
int i = 5; int p =27;
for(int l = 23; l <p; l++){
i*=(l-22);
}
System.out.print(i);
//2a
int i = 100;
double d = 4.55; double d2 = 3.75;
int j = (int) (d*100 + d2);
System.out.print(j);
//3a
int val = 205;
for(int i=0; i<5; i++) {
val/=2;
}
System.out.print(val);
//4
int i = 3;
for(int j = 5;j > 0; j--){
i+=i;
}
System.out.print(i);
// Pair coding
public double purchasePrice(double x){
x *= 1.1;
return x;
}
purchasePrice(6.50);
public class Customer {
private String name;
private int id;
public Customer(String name, int idNum){
this.name = name;
this.id = idNum;
};
public String getName(){
return name;
};
public int getID(){
return id;
};
public void compareCustomer(Customer x){
System.out.println(id - x.id);
};
}
Customer c1 = new Customer("Smith", 1001);
Customer c2 = new Customer("Anderson", 1002);
Customer c3 = new Customer("Smith", 1003);
c1.compareCustomer(c1);
c1.compareCustomer(c2);
c1.compareCustomer(c3);
// Using Objects/ Goblin Fight
import java.util.*;
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;
}
}
public class Duel {
public static void fight(Goblin goblin1, Goblin goblin2) {
while (goblin1.isAlive() && goblin2.isAlive()) {
// goblin1 hit chance tester
if (Math.random() < goblin2.getHitChance()) {
goblin2.takeDMG(goblin2.getDMG());
System.out.println(goblin1.getName() + " takes " + goblin2.getDMG() + " damage");
}
else {
System.out.println(goblin2.getName() + " missed!");
}
// print hp of goblin1
System.out.println(goblin1.getName() + " HP: " + goblin1.getHP());
if (!goblin1.isAlive()) {
System.out.println(goblin1.getName() + " has perished");
break;
}
// if statement for goblin2 hit chance
if (Math.random() < goblin1.getHitChance()) {
goblin2.takeDMG(goblin1.getDMG());
System.out.println(goblin2.getName() + " takes " + goblin1.getDMG() + " damage");
}
else {
System.out.println(goblin1.getName() + " missed!");
}
// print hp of goblin2
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("Joe Biden");
goblin1.setHP(12);
goblin1.setDMG(2);
goblin1.setHitChance(0.5);
Goblin goblin2 = new Goblin();
goblin2.setName("Donald Trump");
goblin2.setHP(4);
goblin2.setDMG(1);
goblin2.setHitChance(0.75);
fight(goblin1, goblin2);
}
}
Duel.main(null);