Notes

---------------------------------------------------------------------------------------------------------------------------

EX: .....

tuna tunaObject = new tuna();

   tunaObject.simpleMessage();
  
 
 //tunaObject is the referrer to the class Tuna
 //tunaObject.simpleMessage(); refers to the Method in the class Tuna
 
 //Referring to multiple Classes

  mutt muttObject = new mutt();
   muttObject.dirtyDog();
  System.out.println(" and ");
   tuna tunaobject = new tuna();
  tunaobject.simpleMessage();

---------------------------------------------------------------------------------------------------------------------------

Random Crap - Uncategorized

Private Method: Only the class it is in can use its functions. Other Classes do not have access to this Method

Public/Private ________ = return type/ Blank is return type

Return Type: Tells what the method returns ( use Void after the return type if the method does not return anything)

Parameter: the variables that are listed as part of a method declaration. Each parameter must have a unique name and a defined data type.

---------------------------------------------------------------------------------------------------------------------------

EX:

public void changeCircle(Circle c1, int chgX, int chgY) {
 c1.setX(circle.getX() + chgX);
 c1.setY(circle.getY() + chgY);
}

---------------------------------------------------------------------------------------------------------------------------

EX: .....

  Public String getName
 //String is the Return Type
  Public String void()

---------------------------------------------------------------------------------------------------------------------------

Memory leaks-

Garbage collector cannot collect the live reference. Live reference is used once and never used again. The garbage collector cannot take live references

---------------------------------------------------------------------------------------------------------------------------

Instructor: Allows user to initialize variables as soon as they are made

\n-new line

\”-quotation mark

\t-tab

\\-backslash

---------------------------------------------------------------------------------------------------------------------------

---Random method setup

---------------------------------------------------------------------------------------------------------------------------

Random rand = new Random();

int n = rand.nextInt(100);

---------------------------------------------------------------------------------------------------------------------------

The code will print the first line under the for loop because there are no curly braces

---------------------------------------------------------------------------------------------------------------------------

Swapping Values

public static void main(String args[]){
int a = 7;
int b = 35;
// swap a with b?
int temp = a;
    a = b;
    b = temp;
System.out.println(a + " " + b);
}

Primitive Crap

int: Output|Whole numbers only;

Double: OUTPUT|Decimal numbers(Exact Number);

% when used with int gives a remainder instead of the answer; (Modulo)

--All operations can be used with above( - %)--

++int will add ONE to the integer;

int++ will add ONE to the integer after the system has used it;

---------------------------------------------------------------------------------------------------------------------------

EX: .....

int tuna = 5;

   int bass = 18;
   ++tuna;
    System.out.println(++tuna);
    //OUTPUT = 6

---------------------------------------------------------------------------------------------------------------------------

EX: .....

int tuna = 5;

   int bass = 18;
   tuna++;
    System.out.println(tuna++);
    //OUTPUT = 5
    System.out.println(tuna++);
    //OUTPUT = 6

---------------------------------------------------------------------------------------------------------------------------

The subtraction operation can be used with above;

Assignment Operator: Takes new variable and adds it to the old variable

---------------------------------------------------------------------------------------------------------------------------

EX: Tuna = tuna +5

EX: Tuna+= 8

---------------------------------------------------------------------------------------------------------------------------

String test methods

Boolean & Loops

--If/Else Statements--


== means Exactly equal to

-Comparison

<= Less than or equal to

>= Greater than or equal to

!= means not Equal

If/Else Statements: Allows the computer to make a decision based off of the variables given

---------------------------------------------------------------------------------------------------------------------------

EX: .....

if (boy > 10 && girl < 60) {

   System.out.println("You may enter");
   } else{ System.out.println("Get outta here!");
   }

---------------------------------------------------------------------------------------------------------------------------

Or Statements: Either side of a condition must be true (||)

---------------------------------------------------------------------------------------------------------------------------

EX: .....

int boy = 25;

 int girl = 12;
   if (boy > 10 || girl < 60) {
   System.out.println("You may enter");
   } else{ System.out.println("Get outta here!");
 }

---------------------------------------------------------------------------------------------------------------------------

Switch Statements: Finds variable values in parameters (switch)

Case Statements: In case of:

Break Statements: Stops the operation/Ends there. Doesn't continue with the rest of the code

Default Statements: If the variables do not match the aforementioned cases then it will immediately go to default

---------------------------------------------------------------------------------------------------------------------------

EX: .....

int age;

   age = 3;
   
   switch (age) {
   case 1:
    System.out.println("You can Crawl");
    break;
   case 2:
    System.out.println("You can Talk!");
    break;
   case 3:
    System.out.println("You can Read!");
    break;
   case 4:
    System.out.println("You can Attack!");
    break;
   default:
    System.out.println("I don't know how old you are.");
    }

//Output = You can read

---------------------------------------------------------------------------------------------------------------------------

While loops: Can continuously execute code when only typed once within a parameter as long as its logical test is true

---------------------------------------------------------------------------------------------------------------------------

EX: .....

   int counter = 0;
   
   while (counter < 101) {
//The while loop (Counter < 101) stops the command at 100
    System.out.println(counter);
    counter++;
//Counter command adds one to the integer
}

---------------------------------------------------------------------------------------------------------------------------

For Loops: a sequence of instruction s that is continually repeated until a certain condition is reached.

---------------------------------------------------------------------------------------------------------------------------

EX: .....

for (int i = 0; i < 5; i++) {
System.out.println("Bother");
}
for (int i = 0; i < 5; i++) {
System.out.println(i);    //vs ("i")
}

/*Output = Bother
Bother
Bother
Bother
Bother*/

---------------------------------------------------------------------------------------------------------------------------

Sentinel Values: A value that signals the end of user input

Sentinel Loop: Repeats until a sentinel value is see

---------------------------------------------------------------------------------------------------------------------------

Returning Boolean Values

! = not --- changes the value or boolean expression to the opposite of the current value or supposed value

B is the answer because

truth table

Arrays

Answer is C

The For Loop integer k is the signifier for position. The index starts at 0 and so the position of 1 is 3. The next part of the For loop shows that the position signifier is less than the array length named "arr". When the loop is finished, it increments the position value up by 1. In the loop, the array named "arr" is taking the position minus 1 which goes back one position. Then it adds the current position with the previous.

Array Lists

-

Classes

---------------------------------------------------------------------------------------------------------------------------

OBJECTS: An entity that combines state and behavior

-Object oriented programming: Programs that perform thier behavior as interactions between objects

---------------------------------------------------------------------------------------------------------------------------

Classes as modules

-Module: A resuable piece of software, stored as a class: Math, Arrays, System


Encapsulation

Encapsulation - hiding implementation details from clients

Benefits of encapsulation:

  • Abstraction between object and clients
  • Protects from unwanted access
    • Ex: Can't fraudulently increase and Account's balance
  • Can change the class implementation later
    • Ex: Point could be rewritten in polar coordinates (r, θ) with the same methods
  • Can constrain objects' state (invariants)
    • Ex: Only allow Accounts with non-negative balance

This: Refers to the implicit parameter inside your class

      • Refer to a field: this.Field
      • Call a method: this.method(parameters);
      • One constructor can call another: this(parameters);

Overloading

Method overloading is the process of creating several methods with the same name, but with different method signatures. Then, depending on what parameters the method is called with, the appropriately signed method is called.

signature = method name + all parameter types

void myMethod(int j, float k)

signature: myMethod(int, float)

different number of params, different order of params, different type of params

NOT VALID: if methods only different by return type (compile error!)

Example 1:

class Overload {
int r;
String s;

public void setValue(int a, String b){ 
  r = a;
  s = b; 
}

public void setValue(String a, int b) { 
  r = b;
  s = a; 
} 

public static void main(String[] args) {
  Overload o = new Overload();
  o.setValue(1, "One");  //this is fine
  o.setValue("One", 1); //this is fine too!
}
}

Example 2:

class Overload {
int r;
String s;

public void setValue(int r, String s){ 
  this.r = r;
  this.s = s; 
}
public void setValue(String s, int r) { 
  this.setValue(r, s); 
} 

public static void main(String[] args) {
  Overload o = new Overload();
  o.setValue(1, "One");  //this is fine
  o.setValue("One", 1); //this is fine too!
}
}

Keyword this

A reference to the current object!

Can be used only inside a method

Useful when referring to a member variable in class with same name as variable in method.

class ThisExample {
int r;
void setValue(int r){ 
  this.r = r; 
}
ThisExample incrementR() { 
  r++; 
  return this;
} 

public static void main(String[] args) {
  ThisExample x = new ThisExample();
  x.setValue(10);
  x.incrementR().incrementR();  //r is now 12
}
}

Can invoke one constructor from another

this(param) Useful if constructors share significant amount of initialization

Note: this() must be the first statement of the constructor!

Can be called only once!

Collections