Objects - Instances of Classes

  • a class is a blueprint for creating objects with the same behavior and defined attributes
  • an object is a specific entity, made from a class, that you can manipulate in your programs
  • objects are instances of classes with variables used to name them

Creating and Storing Objects (Instantiation)

  • constructors are used to initialize the attributes for an object
  • an example of formal parameters:
public Person(String nm, int ag, boolean ad) {
    name = nm;
    age = ag;
    isAdult = ad;
}
  • overloading constructors
    • there can be more than one constructor for an object, which is called overloading the constructor
    • a no-argument constructor has no parameters and sets the instance variables for the object to default values
  • employee example -
public class Employee {
    
    private String name;
    private String empId;
    private int pay;

    public Employee(String nm, String id, int pay) {
        name = nm;
        empId = id;
        pay = 20;
    }

    Employee person1 = new Employee("Simon", "7628", 60000);
    Employee person2 = new Employee("Cassidy", "3224", 60000);
    Employee person3 = new Employee("Bill", "1810", 60000);

}

Calling a Void Method

  • methods define the behaviors for all objects of a class and consist of a set of instructions for executing the behavior
  • procedural abstraction
    • shortens and groups code so it's more simplified and methods can be used by other people without them having to understand it
    • letter example -
public class Letter {
    
    public void writeLetter() {
        greeting();
        specialMessage();
        closing();
    }

    public void greeting() {
        System.out.println("Hi, friend!");
    }

    public void specialMessage() {
        System.out.println("CSA is awesome!");
    }

    public void closing() {
        System.out.println("See you soon!");
    }

    public static void main(String[] args) {
        Letter friendLetter = new Letter();
        friendLetter.writeLetter();
    }
    
}
Letter.main(null);
Hi, friend!
CSA is awesome!
See you soon!

Calling a Void Method with Parameters

public class Calculator {
    
    public void calcAverage(int num1, double num2) {
        double average = (num1 + num2) / 2;
        System.out.println("The average is = " + average);
    }

    public void calcAverage(int num1, int num2, double num3) {
        double average = (num1 + num2 + num3) / 3;
        System.out.println("The average is = " + average);
    }

    public static void main(String[] args) {
        Calculator newAverage = new Calculator();
        newAverage.calcAverage(21, 51.4);
        newAverage.calcAverage(4, 24, 72.2);
    }

}
Calculator.main(null);
The average is = 36.2
The average is = 33.4
public class WordMatch
{
/** The secret string. */
private String secret;
/** Constructs a WordMatch object with the given secret string of lowercase letters. */
public WordMatch(String word)
{
/* implementation not shown */
}
/** Returns a score for guess, as described in part (a).
* Precondition: 0 < guess.length() <= secret.length()
*/
public int scoreGuess(String guess)
{ /* to be implemented in part (a) */ }
/** Returns the better of two guesses, as determined by scoreGuess and the rules for a
* tie-breaker that are described in part (b).
* Precondition: guess1 and guess2 contain all lowercase letters.
* guess1 is not the same as guess2.
*/
public String findBetterGuess(String guess1, String guess2)
{ /* to be implemented in part (b) */ }
}

a)

/** Returns a score for guess, as described in part (a).
* Precondition: 0 < guess.length() <= secret.length()
*/
public int scoreGuess(String guess) {
    int count = 0;
    for(int i = 0; i < secret.length(); i++) {
      int j = i + guess.length();
      if(j <= secret.length() && secret.substring(i, j).equals(guess))
        count++;
    }
    return count * (guess.length() * guess.length());
}

b)

public String findBetterGuess(String guess1, String guess2) {
  int score1 = scoreGuess(guess1);
  int score2 = scoreGuess(guess2);
  if(score1 > score2) {
    return guess1;
  } else if(score2 > score1) {
    return guess2;
  } else {
    if(guess1.compareTo(guess2) > 0) z{
      return guess1;
    } else {
      return guess2;
    }
  }
}

Period 2 - Using Objects in Java Quiz