Instances of Classes

  • java is object-oriented programming, java is about manipulating objects
  • objects are a reference type, when referring to it, you are referring to where it is stored
  • an object is an instance of a class

Constructors

  • to make object, call constructor, will initialize the object and make characteristics
  • Class object = new Class();

Constructor Overloading

  • one class can have multiple constructors, the different constructors need to have diff numbers of parameters or different order of variables, each constructor will make objects with same types of characteristics, but are created differently
  • parameters missing are given default parameters instead

Null Objects

  • object can be made null , makes it so no object exists for that

Void Methods vs. Non-Void Methods

  • void methods don't return value, change characteristics of object or prints text
  • static methods belong to a class, not tied to an object

static v non-static

  • dot notation to use static, classname.method()
  • non-static on a particular object, objectname.method()

write/call methods with parameters

  • parameters in the parentheses,
  • to call method, plug values in place of variables

Non-void method

  • use method as part of expression/stored as variable
  • dataType variableName = methodName(parameterListOptional);

Strings

  • string literals are objects
  • escape characters print a character or empty space
  • combine strings with concatenation, used through + operator

Vocab

Overloading a method, same name different parameters

When you use the same method name in a class each with different parameters.

Overriding a method, same signature of a method

When a subclass has the same name and parameters as the parent class

Late binding of object

Late binding is when the compiler runs the object and is created then and there

Polymorphism: any of overloading, overriding, late binding

Multiple methods with the same name but different parameters

Comparing Objects

import java.io.*;
 
class Pet {

    String name;
    int age;
    String breed;
 

    Pet(String name, int age, String breed)
    {
        this.name = name;
        this.age = age;
        this.breed = breed;
    }
}

public class Main {
 
    public static void main(String args[])
    {
 
        Pet dog1 = new Pet("Snow", 3, "German Shepherd"); //late binding
        Pet cat = new Pet("Jack", 2, "Tabby"); //overriding method different
        Pet dog2 = new Pet("Snow", 3, "German Shepherd"); //overriding method same
        
        
        System.out.println(dog1.equals(dog2));
    }
}

Main.main(null);
false
  • equals and hashCode methods can be used to compare objects
  • equals method compares equality of two objects

Standard methods: toString(), equals(), hashCode()

  • toString() method returns a string representation of a class
  • equals() compares two strings
  • hashCode() returns the integer value that is associated with all values in Java(aka the hash code)

Abstract Class, Abstract Method

This class’s objects and methods can be referenced, however the class itself is not initialized.

google form

FRQs

a)

public int scoreGuess( String guess )
{
    int val = 0;
    int len = guess.length();
    for( int i = 0; i <= secret.length()-len; i+=1)
    {
        String ck = secret.substring( i, i+len );
        if( ck.equals(guess) )
            val++;
    }
    return val*len*len;
}

b)

public String findBetterGuess(String guess1, String guess2 )
{
    int a = scoreGuess( guess1 );
    int b = scoreGuess( guess2 );
    if( a > b ) return guess1;
    if( b > a ) return guess2;
    if( guess1.compareTo( guess2 ) > 0 )
    return guess1;
    return guess2;
}