Lesson Hacks Throughout
// Unit 1 - 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);
// return (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);
Vocab for Unit 1
- Constructor - A constructor initializes an object, a constructor has no return value because it is not actually used by the code, instead it simply initializes the object.
public class Customer {
- Intializing class Customer in this case with a constructor - no return value
- Accessor methods - AKA “getters”, return the values in a defined class/reads the value, it is usually named get + Class Name
public String getName(){ return name; };
- Getters in the Customer class, for this case it returns variable name
Mutator methods - AKA “setters”, sets the parameters to the attributes, it updates the values of a variable, it is usually named set + Class Name
public Customer(String name, int idNum){ this.name = name; this.id = idNum; };
- Setters to the customer class, setting name and id num updating the values
“This” refers to an object within a constructor, used in setters to set variables/parameters.
this.name = name;
- Used in the creation of the Customer class
// Unit 2 - OOP
import java.util.*;
//initializing the class Goblin
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.2);
Goblin goblin2 = new Goblin();
goblin2.setName("Donald Trump");
goblin2.setHP(10);
goblin2.setDMG(3);
goblin2.setHitChance(0.75);
fight(goblin1, goblin2);
}
}
Duel.main(null);
Vocab for Unit 2
- Used things like Class creation, mutator methods, and accessor methods
- Usage of Private modifier in variable creation - least accessible modifier that can be only used in the class in itself
private String name;
- name is private, no changes can be made to it outside the class
Compound Boolean Method - this includes using the &&, ||, and ! operators,
while (goblin1.isAlive() && goblin2.isAlive()){ }
- Boolean expression for while the goblins are both alive
Tester Method - Main/tester method is used to test a class with a specific input to validate its working efficiency.
public static void main(String[] args) { Goblin goblin1 = new Goblin(); goblin1.setName("Joe Biden"); goblin1.setHP(12); goblin1.setDMG(2); goblin1.setHitChance(0.2); Goblin goblin2 = new Goblin(); goblin2.setName("Donald Trump"); goblin2.setHP(10); goblin2.setDMG(3); goblin2.setHitChance(0.75); fight(goblin1, goblin2); }
- This is a main method used that tests the class making two created goblins named "Joe Biden" and "Donald Trump" fight each other
// Unit 3 - Boolean Expressions
// 2009 3b
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);
//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 : delimiters) {
if (delimiters.get(i) == openDel) {
opencount++;
}
else if (delimiter.get(i) == closeDel) {
closecount++;
}
if (closecount > opencount) {
return false;
}
}
return true;
}
}
Vocab for Unit 3
Note: The code may not run as the getChargingCost() method is not defined by the AP exam, and here we are assuming that it works
* Initialized the classes for each problem
- Enhanced for loop -easier to use for loops, errors will usually happen less, as you don't have to manage steps by themselves, but for loops allows you to control everything about looping
for (int i : delimiters) {
// Unit 4 - Iteration
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(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();
}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);
// Unit 5 - Classes
//initialize the class
public class WordMatch {
String secret;
//setters
public WordMatch(String secret) {
this.secret = secret;
}
public void scoreGuess(String guess) {
//counter variable
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);
}
}
WordMatch game = new WordMatch("mississippi");
game.scoreGuess("i");
game.scoreGuess("iss");
game.scoreGuess("issipp");
game.scoreGuess("mississippi");
//Part 2
public class ClubMembers {
private ArrayList<MemberInfo> memberList;
public void addMembers(String[] names, int gradYear) {
for(String name : names) {
memberList.add(new MemberInfo(name, gradYear, true));
}
}
}
Vocab for Unit 5
- Initialized the class
- Nested For Loop - meant to loop through the single array and compare the word it's on and the word in front(in this case)
for (int i = 0; i < this.secret.length(); i++){ for (int j = i + 1; j < this.secret.length() + 1; j++) {
- Comparing strings - used .equals() which compares the values of two strings
if (guess.equals(this.secret.substring(i,j))) {
// Unit 6 - Arrays
// A. Swap the first and last element of an array
import java.util.*;
public class A{
public static void main(String[] args){
int[] array = {1, 2, 3,4};
System.out.println(Arrays.toString(array));
int temp = array[0];
array[0] = array[array.length-1];
array[array.length-1] = temp;
System.out.println("Swapped array: "+ Arrays.toString(array));
}
}
// B. Replace all even elements with 0.
public class B{
public static void main(String[] args){
int[] array = {1,2,3,4,5,6};
System.out.println(Arrays.toString(array));
for(int i = 0; i < array.length; i++){
if(array[i] % 2 == 0){
array[i] = 0;
}
}
System.out.println("Zero array: " + Arrays.toString(array));
}
}
A.main(null);
B.main(null);