Unit 1-5 Review + Vocab List
List of CS terms list
- Primitives (int, char,double,bool) vs Object (String, other classes)
- Primitives only have value, Objects have properties Homework: We did this unit, so no HW needed to be done
- Casting: Defining the variable of a prinitive, can be used to either round a number (Truncate) or to get the decimal value of a number -done by (primitive)variable_name
- Warapper Class: Used to convert Primitive Data Types into Objects
//CASTING EXAMPLE
int g = 1;
int h = 3;
int f = g/h;
System.out.println(f); //prints out 0, if double would be 0.33 because it keeps out double's place
double c = 6.9;
// No casting (shows full decimal)
System.out.println(c);
// casting to integer (rounds down to 6)
System.out.println((int)c); //printing out the function
// WRAPPER CLASS EXAMPLE
int b = 10;
// Using "Integer" wrapper class
ArrayList<Integer> list = new ArrayList<Integer>();
// Creating "Integer" from int
Integer b_wrap = new Integer(b);
list.add(b_wrap);
// Adding A again to they arraylist with wrapper class
list.add(b);
//Printing Both elements
System.out.println(list);
- Object is a created instance in a class with a constructor (thing which helps create and define instances)
- Static methods and properties based on class, cannot be used in other classes
- Methods can have more than 1 parameter, as long as the types and name of them are different
- Homework: https://kinerboi.github.io/myFirstRepopart2/2022/10/23/Unit2HW.html
- Concatenation: Allows for combining Strings together with the + sign when printing out code
- Math class: Built in Java functions which allow someone to do mathematical operations instead of hardcoding them.
- Comparing Numbers/Strings/Objects: == to check if values have equal value, /= to check if values do not equal each other
- Truth Tables: Used to determine whether a expression is true/false for all input values
- && is and
- || is or
- true = true;
- false = false;
- == is equals
- != is not equals
//CONCATENATION EXAMPLE
String a = "The";
String b = " San Francisco 49ers ";
int c = 100;
String d = "% ong fr.";
System.out.println(a+b+c+d);
//MATH CLASS EXAMPLE
double n = 10;
double o = Math.pow(n,2); //to take the square root of n (or 10)
System.out.println(o);
//COMPARING NUMBERS/STRINGS/OBJECTS
int a = 0;
int b = 0;
int c = 1;
// Comparing two same numbers
System.out.println(a == b);
// Comparing two different numbers
System.out.println(a != c);
System.out.println(a == c);
String as = new String("holy");
String bs = new String("holy");
// Comparing the same string to itself (SAME memory location)
System.out.println(as == as);
// Comparing strings with same content using wrong operator
System.out.println(as == bs);
// Comparing strings with right function .equals()
System.out.println(as.equals(bs));
- Booleans store true or false
- Can be generated with comaprisons (==,>,<,>=,<=)
- Compound Boolean Expression can be made combining various conditons with &&,||,==,!= (Look for Basic Definitions up top to see meaning)
- if statements take boolean conditional and do code based on such
- else if/else statements can expand code
- De Morgan's Law (!(a && b) = !a || !b) AND (!(a || b) = !a && !b)
- Truth Tables
- Homework: https://kinerboi.github.io/myFirstRepopart2/2022/10/24/Unit3.html
// DE MORGAN'S LAW EXAMPLE
boolean a = true;
boolean b = false;
// complicated boolean expression
boolean res1 = !(a && b) || !(a || b);
// !a || !b || !a && !b
//True, because in all cases it cannot be either a or b
System.out.println(res1);
- While loop runs WHILE a boolean coditional is true
- For loops iterate and increase in loops throghout until conditional value is met
- Enhanced for loops can iterate though all values of an arrary, but that value cant be changed
- Homework: https://kinerboi.github.io/myFirstRepopart2/2022/10/23/Unit4HW.html
- For/Enhanced For loops are to iterate through a loop until a condition is met
- While and Do While loop runs while a condition is true
- While loop runs check before each iteration of code is run
- Do While loop runs check after each iteration of code is run
- Nested loops are loops within loops
//LOOP CONDITIONALS
int sum = 0;
int sum1 = 0;
int[] arr = {2, 4, 6, 8};
for(int i = 0; i < arr.length; i++) { //condition to keep loop running
sum = sum + arr[i]; //adding up values in array
}
System.out.println(sum);
for(int i : arr) { //enhanced for loop especially used for Arrays
sum1 = sum1 + i; //adding up values of array
}
System.out.println(sum1);
int i = 0;
int steps = 0;
while (i < 9) {
steps = steps + i;
i++;
}
System.out.println(steps);
do {
steps = steps + i;
i++;
} while (i < 9);
System.out.println(steps);
//seeing how differentt values pop up, based on checking before or after increment
//NESTED LOOP EXAMPLE
String [][] phoneKeyboard = {
{"1", "2", "3"},
{"4", "5", "6"},
{"7", "8", "9"},
{" ", "0"," " }
};
for (int i = 0; i< phoneKeyboard.length; i++) {
for (int j = 0; j< phoneKeyboard[i].length; j++) {
System.out.print(phoneKeyboard[i][j] + " ");
}
System.out.println();
}
- Classes used to create objects, have properties and methods
- Properties stre info about class, can be made public (accessible thoghout code) or private (accessible in class only)
- Methods modify object
- Getters/Setters can be used to retrieve data from a private class
- Homework: https://kinerboi.github.io/myFirstRepopart2/2022/10/20/Class.html
- Creating a Class, describe Naming Conventions
- Camel case, first letter of word always lowercase and then first letter of next word is upper case
- No Spaces in a class name
- Constructor, describe why there is no return -Constructor is where all the objects of class are defined, thus letting one know what properties a class has
- Accessor methods, relationship to getter
- help get the value of one variable, accssing it and storing the value temporarily
- funciton will have primitive type based on type of primitive that the value is
- Mutator methods, relationship to setter, describe void return type
- take the temporarily stored value, and sets it to another variable which can be used for other tasks
- are void functions, because they do not return any value
- Static variables
- Static Variables are variables that are common to all parts of the class
- Show use case of access modifiers: Public, Private
- Public Classes are classes which are acccessible everywhere in code
- Private Classes are classes wherre primitives and other data types are accessible within the class only
- Static methods
- Methods that are already defined in class, do not need to be made manually unlike other Class methods
- this Keyword
- Allowa one to access properties inside class
- Assumes class in which this Keyword is called is the default class that user means to call
- main method, tester methods -Where code is implemented and ran
- Inheritance, extends
- Made to extend a defined class, and add more methods and properties into it
- Overloading a method, same name different parameters
- Help to calculate the same things with same method name without needing to worry aout the type of primitive variable
- Overriding a method, same signature of a method
- Where the definition of a subclass becomes the default definition of the main class
- Abstract Class, Abstract Method -Classes and methods, without any definition to them (no code written define the abstract class, allowing one to make new objects in seperate classes based on abstract class)
- Standard methods: toString(), equals(), hashCode()
- These methods help to access and print out final information without needing to access methods in class, thus allowing one to just print the method
//CLASS, NAMING CONVEENTIONS, TESTER METHOD, CONSTRUCTOR CODE
public class thisAnimal { //camel case, public class
int legs = 4; //primitive variables
int face = 1;
public thisAnimal (int legs, int face) { //contstructor defining class with variables
this.legs = legs; //this keyWord referring to thisAnimal class
this.face = face;
}
public void main (String[] args) { //Tester Method
System.out.println(legs);
}
}
//Setters and Getters code
public class ThisAnimal { //camel case, public class
int legs = 4; //primitive variables
int face = 1;
public ThisAnimal (int legs1, int face1) { //contstructor defining class with variables
this.legs = legs1; //this keyWord referring to thisAnimal class
this.face = face1;
}
public int getThisAnimal() {
return this.face;
}
public void setThisAnimal(int faceFinal) {
this.legs = faceFinal;
}
public static void main (String[] args) { //Tester Method
ThisAnimal thisTiger = new ThisAnimal();
System.out.println(thisTiger.getThisAnimal());
}
}
thisAnimal.main(null);
//STATIC METHOD CODE
class staticClass {
// static method
static String coolMethod (String k) {
return k + " is ";
}
// static property
static int mujer = 8;
public static void main(String[] args) {
// no object needed
System.out.print(staticClass.coolMethod("she"));
System.out.println("a " + staticClass.mujer + "/10");
}
}
staticClass.main(null);