Unit Score Homework and Notes
1 0.85 link
2 0.95 link
3 0.93 link
4 Ours(2/2) link
5 0.9 link

Overall: 5.63/6

Most notes are embedded within each lesson for units 1-5, but here are the terms I was unsure where to put in a unit blog

Terms

Casting (Division)

  • when you assign a value of one primitive data type to another type
  • it is necessary in dividing two integers otherwise the two int types divided will just default to the lower number without a decimal so '0' as a double which becomes '0.0'
int top = 4;
int bottom = 9;
double division = top / bottom;
System.out.println("Without Cast: "+ division);
double division = ((double) top) / bottom;
System.out.println("With Cast: "+ division);
Without Cast: 0.0
With Cast: 0.4444444444444444

Casting (Truncating and Rounding)

  • when we cast a double to an integer, it truncates every decimal value and only retrieves the int
double blah = 6.99999935629148437124097925792348972398472983749283749823749832478923;
System.out.println("Non-truncated: " + blah);
int blahhehe = (int) blah;
System.out.println("Casted and Truncated:" + blahhehe);
Non-truncated: 6.999999356291484
Casted and Truncated:6

Concatenation

  • combining strings or other variables
String blah1 = "monkey ";
String blah2 = "adi";

System.out.println(blah1+blah2);

String monkey = blah1 + blah2 ;
int blah = 5;

System.out.println(blah + " " + (monkey) + "s");
monkey adi
5 monkey adis

Java Math Class

  • basic class in java that contains math functions and common math values such as pi.
  • Called using Math.""
  • Other functions include trig functions, log functions, and the random function.
import java.lang.Math;//must be imported at start of program
double rand = Math.random();
System.out.println("random: " + rand);
System.out.println("Cos: " + Math.cos(rand));
System.out.println("Log: " + Math.log(rand));
System.out.println("Square Root: " + Math.sqrt(rand));
random: 0.8588897550824057
Cos: 0.6532784567335359
Log: -0.15211470623265586
Square Root: 0.926763052285969

Truth Table

  • just displays basic logic in a table format given the 4 different combinations of "a" and "b"

De Morgan’s Law

  • shows that you can flip the operators of a boolean to return the same thing
  • switches && to || and ! to nothing and vice versa
boolean a = true;
boolean b = false;
!(a && b) == (!a || !b);
!(a || b) == (!a && !b);
true

Comparing Numbers

  • == compares numbers
int x = 1;
int y = 1;
int z = x + 2;

if(x == y){
    System.out.println("true");
}else{
    System.out.println("false");
}

if(x == z){
    System.out.println("true");
}else{
    System.out.println("false");
}
true
false

Comparing Strings

comparing strings is done through the .equals operator

let score = 0
let total = 0
let count = 0
let correct = 0
class Jeopardy {
    constructor(question, answer, point) {
        this.question = question;
        this.answer = answer;
        this.point = point;
    }
// REAL BIG BOY IMPLEMENTATION!!!
    CheckAnswer(guess) {
        return guess === this.answer
    }
// WOO BIG BOY IMPLEMENTATION RIGHT HERE
}
let q1 = new Jeopardy('What is the capital of Chile?', 'Santiago', 2);
let q2 = new Jeopardy('What is the capital of France?', 'Paris', 1);
let q3 = new Jeopardy('What is the capital of Czech Republic?', 'Prague', 2);
let q4 = new Jeopardy('What is the capital of Portugal?', 'Lisbon', 2);
let q5 = new Jeopardy('What is the capital of Ethiopia?', 'Addis Ababa', 2);
let q6 = new Jeopardy('Who is the President of the United States?', 'Joe Biden', 1);
let q7 = new Jeopardy('Who is the leader of Russia?', 'Vladimir Putin', 1);
let q8 = new Jeopardy('What is the capital of the United States?', 'Washington DC', 1);
let q9 = new Jeopardy('What country is being invaded by Russia?', 'Ukraine', 2);


const qarray = [q1, q2, q3, q4, q5, q6, q7, q8, q9]

function QNA(number) {
    for (let i = 0; i < number; i++) {
        const randomValue = qarray[Math.floor(Math.random() * qarray.length)];
        var index = qarray.indexOf(randomValue);
        if (index > -1) {
            qarray.splice(index, 1);
        }
        let guess = prompt(randomValue.question + " Points: " + randomValue.point);
        count = count + 1
        total = total + randomValue.point
        if (randomValue.CheckAnswer(guess)) {
            score = score + randomValue.point;
            correct = correct + 1
            console.getElementById('answer').innerHTML = "Well Done!";
            console.getElementById('score').innerHTML = "Your score is " + score + "/" + total;
            console.getElementById('correct').innerHTML = "You got " + correct + " questions correct out of " + count;
        }
        else {
            console.getElementById('answer').innerHTML = "Nice Try!";
            console.getElementById('score').innerHTML = "Your score is " + score + "/" + total;
            console.getElementById('correct').innerHTML = "Sorry, you have gotten " + correct + " questions correct out of " + count;
        }
        }
}
function Tester() {

}
true
false

Comparing Objects

  • same as primitives "=="
String x = new String("blah2");
String y = new String("blah2");
String z = new String("blah5");
x=y;

if(x == y){
    System.out.println("true");
}else{
    System.out.println("false");
}

if(z == x){
    System.out.println("true");
}else{
    System.out.println("false");
}
true
false

Constructors

  • constructors are an implicit statement done within the class that does not return a value

Accessor and Mutator Methods

  • instance method that gets or sets the value of a property of an object.
  • Getters and setters are accessor and mutator methods to retrieve and set data of a variable, respectively
  • accessors access the predetermined data
  • mutators change the data to set new predetermined data
public class Person {
    private String name; // private = restricted access
  
    // Getter
    public String getName() {
      return name;
    }
  
    // Setter
    public void setName(String newName) {
      this.name = newName;
    }
  }

Access Modifiers

  • Restrict the scope of a class, constructor, variable, method, or data member.
  • Types: default, private, protected, and public.
void HelloWorldDefault(){
    System.out.println("Hello World");
}

public void HelloWorldPublic(){
    System.out.println("Hello World");
}

private void HelloWorldPrivate(){
    System.out.println("Hello World");
}

protected void HelloWorldProtected(){
    System.out.println("Hello World");
}

Inheritance and Extends with Examples of Subclass and Super Keyword

Inheritance in Java is the method to create a hierarchy between classes by inheriting from other classes.

public class Animal {

	private String eats;

	private int numLegs;

	public Animal(){}

	public Animal(String food, int legs){
		this.eats = food;
		this.numLegs = legs;
	}

	public String getEats() {
		return eats;
	}

	public void setEats(String eats) {
		this.eats = eats;
	}

	public int getNumLegs() {
		return numLegs;
	}

	public void setNumLegs(int numLegs) {
		this.numLegs = numLegs;
	}
}
public class Cat extends Animal{

	private String toy;

	public Cat( String food, int legs) {
		super(food, legs);
		this.toy="String";
	}

	public Cat(String food, int legs, String toy){
		super(food, legs);
		this.toy=toy;
	}

	public String getToy() {
		return toy;
	}

	public void setToy(String toy) {
		this.toy = toy;
	}

}

Method Overloading

  • classes can have two methods with the same name with one having more parameters
public class Concat {
    String concat(String blah, String blah2){
        String blahblah = blah + blah2;
        return blahblah;
    }

    String concat(String blah, String blah2, String blah3){
        String blahblah = blah + blah2 + blah3;
        return blahblah;
    }
}
//no errors