main method, tester methods

  • main method runs main code
  • tester method tests main code

ex:

q5

Subclass constructor, super Keyword

  • subclass; inheriting class
  • super: refers to parent objects; calls superclass methods

Overloading a method, same name different parameters

  • java feature allowing classes to have more than one method that is named the same (but it must have different parameters)
  • easier to overload one instead of having to define two identically-functioning methods

Overriding a method, same signature of a method

  • when a subclass has the same method has the parent class
  • implements a certain aspect of method declared by a parent class

Abstract Class, Abstract Method

  • is a method that has just the method definition but no actual implementation
  • can be subclassed, not instantiated

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

  • toString returns string
  • hashCode returns hashCode of a string
public class toString
{
   public static void main(String[] args)
{}}

Integer x = new Integer(10);
System.out.println(x.toString());
System.out.println(x.toString(90));
10
90
public class hashCode
{
   public static void main(String[] args)
{}}

String str_New = "yay!";
System.out.println(str_New.hashCode());
3701712

Late binding of object

  • name of method looked up at runtime
  • compiler doesn't perform argument checks and leaves it up to runtime

Polymorphism

  • class has different implementations of a method
  • same action performed differently

Accessor & Mutator methods

  • accessor methods: return private variable values
    • access to instance variables
    • don't take parameters
    • return type matches type of variable they're accessing
    • getters
  • mutator methods: called setters

    • set the value
    • void return type; no return value
//basic example
public class Person {
    private String name; // private = restricted access
  
    // Getter
    public String getName() {
      return name;
    }
  
    // Setter
    public void setName(String newName) {
      this.name = newName;
    }
  }
import java.util.Scanner;

public class Main {
    //static variables
  private static String[] words = {"words", "to", "put", "in"};
  private static String word = words[(int) (Math.random() * words.length)];
  private static String asterisk = new String(new char[word.length()]).replace("\0","*");
  private static int count = 0;
  
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    
    //create a while loop when count is less than 7 and
    //when asterisk contains *
    while (count < 7 && asterisk.contains("*")) {
      //print guess any letter in the word
      //print asterisk
      //take in guess and scan it in (string guess and take in scan)
      //hang(guess)
      System.out.println("Guess any letter in the word");
      System.out.println(asterisk);
      String guess = sc.next();
      hang(guess);
    }
    sc.close();

}}

Static methods, Class methods

  • belong to class, not object
  • accessed through class
public class friends
{
   // instance variables
   private String name;
   private String email;
   private String phoneNumber;

   // Static counter variable
   public static int personCounter = 0;

   // static method to print out counter
   public static void printPersonCounter() {
        System.out.println("Person counter: " + personCounter);
   }

   // constructor
   public friends(String initName, String initEmail, String initPhone)
   {
      name = initName;
      email = initEmail;
      phoneNumber = initPhone;
      personCounter++;
   }

   // toString() method
   public String toString()
   {
     return  name + ": " + email + " " + phoneNumber;
   }

   // main method for testing
   public static void main(String[] args)
   {
      // call the constructor to create a new person
      friends p1 = new friends("Sana", "sana@gmail.com", "123-456-7890");
      friends p2 = new friends("Jean", "jean@gmail.com", "404 899-9955");

      friends.printPersonCounter();
   }
}
//class methods
public class Main {
    static void myMethod() {
      System.out.println("Hello World!");
    }
  }  
//methods declared within a class

Wrapper Classes

  • Why wrap int, double, etc?
  • way to use primitive data types as objects

q5

  • primitive type and corresponding wrapper class
  • Wrapper classes are used to convert any data type into an object. The primitive data types are not objects; they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language.
public class wrappers
{
   public static void main(String[] args)
{}}

// int x = 10;
// System.out.println(x.toString()); ------ doesn't work because x is a primitive data type, not a modifiable object
Integer x = new Integer(10);
//to get around this, you define an integer wrapper class
System.out.println(x.toString());
//x is now an object, not a primitive data type


Integer x = new Integer(15);
10

Comparing Objects

  • use .equals() or ==
  • checks if an object is null, or if attributes are the same
class Food {
    //attributes
    String cuisine;
    String name;
    int mealsConsumed;


    //constructor
    Food(String cuisine, String name, int mealsConsumed)
    {
        this.cuisine = cuisine;
        this.name = name;
        this.mealsConsumed = mealsConsumed;
    }
}

//objects
Food Dal1 = new Food("Indian", "Mango Dal", 10);
Food Dal2 = new Food("Indian", "Moong Dal", 4);

System.out.println("Dal1 = Dal2 is " + Dal1.equals(Dal2));
Dal1 = Dal2 is false

HOMEWORK

//UNIT 2 HW
//goblin hacks
public class Goblin {
    private String name;
    private int HP;
    private int DMG;
    private double hitChance;

    public String getName() {
        return name;
    }

    public int getHP() {
        return HP;
    }

    public int getDMG() {
        return DMG;
    }

    public double getHitChance() {
        return hitChance;
    }

    
    public boolean isAlive() {
        if (this.HP > 0) {
            return true;
        } else {
            return false;
        }
    }

    public void setName(String newName) {
        this.name = newName;
    }

    public void setHP(int newHP) {
        this.HP = newHP;
    }

    public void takeDMG(int takenDamage) {
        this.HP -= takenDamage;
    }

    public void setDMG(int newDMG) {
        this.DMG = newDMG;
    }

    public void setHitChance(double newHitChance) {
        this.hitChance = newHitChance;
    }
}
import java.lang.Math;

public class Duel {

    public static void attack(Goblin attackerGoblin, Goblin attackeeGoblin) {

        System.out.println(attackerGoblin.getName() + " attacks " + attackeeGoblin.getName() + "!");
        if (0.8 > attackerGoblin.getHitChance()) {
            attackeeGoblin.takeDMG(attackerGoblin.getDMG());
            System.out.println(attackerGoblin.getName() + " hits!");
            System.out.println(attackeeGoblin.getName() + " takes " + attackerGoblin.getDMG() + " damage");
        } else {
            System.out.println(attackerGoblin.getName() + " misses...");
        }

        System.out.println(attackeeGoblin.getName() + " HP: " + attackeeGoblin.getHP());
        System.out.println();
    }

    public static void fight(Goblin goblin1, Goblin goblin2) {
        while (goblin1.isAlive() && goblin2.isAlive()) {
            
            attack(goblin1, goblin2);

            if (!goblin1.isAlive()) {
                System.out.println(goblin1.getName() + " has perished");
                break;
            }

            attack(goblin2, goblin1);

            if (!goblin2.isAlive()) {
                System.out.println(goblin2.getName() + " has perished");
                break;
            }
        }
    }

    public static void main(String[] args) {
        Goblin goblin1 = new Goblin();
        goblin1.setName("jeffrey");
        goblin1.setHP(12);
        goblin1.setDMG(2);
        goblin1.setHitChance(0.50);

        Goblin goblin2 = new Goblin();
        goblin2.setName("Gunther the great");
        goblin2.setHP(4);
        goblin2.setDMG(1);
        goblin2.setHitChance(1);

        fight(goblin1, goblin2);
    }
}

Duel.main(null);
jeffrey attacks Gunther the great!
jeffrey hits!
Gunther the great takes 2 damage
Gunther the great HP: 2

Gunther the great attacks jeffrey!
Gunther the great misses...
jeffrey HP: 12

jeffrey attacks Gunther the great!
jeffrey hits!
Gunther the great takes 2 damage
Gunther the great HP: 0

Gunther the great attacks jeffrey!
Gunther the great misses...
jeffrey HP: 12

Gunther the great has perished