Vocab Terms
Documentation of vocab
- General Notes
- Terms
- Casting(Division) -
- Casting(Truncating or Rounding)-
- Wrapper Classes -
- Concatenation
- Java Math Class -
- Compound Boolean Expression
- Truth Table
- De Morgan’s Law
- Comparing Numbers
- Comparing Strings
- Comparing Objects
- Constructors
- Access Modifiers
- This Keyword
- Inheritance and Extends with Examples of Subclass and Super Keyword
- Method Overloading
- Standard Methods
General Notes
Primitives are predefined, lowercase, "Primitives", which cannot call methods, are a value, and can be different sizes based on what primitive it is. Non primitives are defined by the coder, are uppercase, "Reference types", which can call methods, can be null, and are all the same size
OOP or object oriented programming is a type of programming language. Classes are a template or blueprint from which objects are created. Objects are instances of a class. Methods are a set of code that perform a specific task. Classes in java can hace data members, methods, constructors, nested classes, and interfaces.
If/else statements allow you to create a code segment that has checks. If/ElseIf/Else statments can be used to decide different program outputs depending on what the user inputted. Switch statements are also an alternative method to if statements.
Iteration is worth 25% of the test so its pretty important. While loops, for loops, and recursion loops are types of loops. Another example is nested iteration, which can put a loop inside another loop.
Classes are a blueprint for instantiating objects. An object is an istance of a class, a class defines an abstract type, methods are the behaviors you get objects to perform. Constructors create the object, are a special method for object instantiation, default constructor has no arguements. Accessor methods are getters and setters, which allow you to get values of variables and return a copy, or edit a variable.
Common errors when making arrays include bound errors, unfilled and uninitialized arrays, when you allocate, or assign a single array variable, but not the whole array. You can use for loops to iterate through an array, by using array.length.
int num = 5;
int denom = 7;
double d = num / denom;
System.out.println("Without Cast: "+d);
double d = ((double) num) / denom;
System.out.println("With cast: "+d);
double exampleDouble = 9.78d;
System.out.println("Not Rounded: " + exampleDouble);
int exampleInt = (int) exampleDouble;
System.out.println("Rounded: " + exampleInt);
Wrapper Classes -
these are classes in java where primitive data types can be used as objects. For examples, int to Integer, double to Double, char to Character. These are useful in cases such as arraylists, where they can only interact with objects, and not primitive types, that's why you would want to wrap an int, or a double
ArrayList<Integer> myNumbers = new ArrayList<Integer>(); // This example works because the arraylist recognizes the Integer object
ArrayList<int> myNumbers = new ArrayList<int>(); // This example doesn't work because the arraylist doesn't recognize the primitive type int
Integer intVariable = 456;
Double doubVaribale = 456.456;
String word1 = "hello ";
String word2 = "world";
System.out.println(word1+word2);
word2 = word2+ " 10";
System.out.println(word1+word2);
word2 += " 20";
System.out.println(word1+word2);
//can also concat variables and string
int x = 40;
System.out.println(x+" adding words");
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));
int x = 2;
int y = 3;
System.out.println("First Expression");
if(x > 0 && (y / x) == 3){
System.out.println("True");
}else{
System.out.println("False");
}
Truth Table
boolean function that is a mathematical function that maps arguments to a value, where the allowable values of range (the function arguments) and domain (the function value) are just one of two values— true and false (or 0 and 1) NOT(x)= 1 if x is 0 0 if x is 1
XOR(x,y) 1 if x and y are different 0 otherwise
!(a && b) == (!a || !b)
!(a || b) == (!a && !b)
if(!(true)&&(true==false)){
System.out.println("Demonstrates De Morgans law");
}
int x = 2;
int y = 3;
int z = 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");
}
String word1 = "world";
String word2 = "hello";
String word3 = "hello";
if(word1.equals("world")){
System.out.println("true");
}else{
System.out.println("false");
}
if(word2.equals(word1)){
System.out.println("true");
}else{
System.out.println("false");
}
Integer x = new Integer(2);
Integer y = new Integer(2);
Integer z = new Integer(3);
x=y;
if(x == y){
System.out.println("true");
}else{
System.out.println("false");
}
if(z == 2){
System.out.println("true");
}else{
System.out.println("false");
}
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");
}
This Keyword
the "this" keyword implies that the object passed in as a member variable of the class is the value passed in when you create an object of that class. Additionally, this can also be used to invoke current class constructor, invoke current class method, return the current class object, pass an argument in the method call, pass an argument in the constructor call
public class Example{
int ex;
// Constructor with a parameter
public Example(int ex) {
this.ex = ex;
}
}
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 color;
public Cat( String food, int legs) {
super(food, legs);
this.color="White";
}
public Cat(String food, int legs, String color){
super(food, legs);
this.color=color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class Addition {
int add(int first, int second){
int sum = first + second;
return sum;
}
int add(int first, int second, int third){
int sum = first + second + third;
return sum;
}
}
//no errors when running