SS

5. Writing Classes

/* 
Writer: SP
Date: March 25th
References: knowt & Fiveable
Unit 5: Writing Classes */

Classes

  • Class- A blueprint for an object.

  • The name of your class should refer to the purpose and function of the class, similar to how you name your variables.

public class Plant {
   // Instance Variables --------
   private String Species;
   private double height;
   // --------------------------
   
   // Constructor
   public Plant(String species, double height) {
      // Implementation not shown
   }
}
  • Note: Remember your indentations!! It needs to be correctly indented for the code to be considered a part of the class.

  • If we don’t put value, the String variable will return ‘null

  • int will return 0

  • double will return 0.0

In classes, variable declarations are foun
d at the top of the code right after the header.

Methods

  • Method- A group of code that performs a specific task.

  • Accessor Methods:

    • It is a method that allow other clients (objects, classes, and users) to access the data of an object.

    • There are two types of accessor method:

      • Getter methods

        • Returns the specific piece of data.

      class Student
      {
          private String name;
          private String email;
          private int id;
      
          public Student(String initName, String initEmail, int initId)
          {
              name = initName;
              email = initEmail;
              id = initId;
          }
      
          // accessor methods - getters
          /** getName() @return name */
          public String getName()
          {
              return name;
          }
      
          /** getEmail() @return email */
          public String getEmail()
          {
              return email;
          }
      
          /** getName() @return id */
          public int getId()
          {
              return id;
          }
      }
      • toString()

        • Return the information about an object as a string.

      class Student
      {
          private String name;
          private String email;
          private int id;
      
          public Student(String initName, String initEmail, int initId)
          {
              name = initName;
              email = initEmail;
              id = initId;
          }
      
          // toString() method
          public String toString()
          {
              return id + ": " + name + ", " + email;
          }
      }
  • Mutator Methods:

    • Also called as setter methods.

      class Student
      {
          // Instance variable name
          private String name;
      
          /**
           * setName sets name to newName
           *
           * @param newName
           */
          public void setName(String newName)
          {
              name = newName;
          }
      
          public static void main(String[] args)
          {
              // To call a set method, use objectName.setVar(newValue)
              Student s = new Student();
              s.setName("Ayanna");
          }
      }

Writing Methods:

  • Method headers:

public static void main (String args[])

public String sayHello()

protected static int addNums(int a, int b)

public void printSum(double a, double b, int c, boolean flag, String text)

Static Variables: & Methods

  • We don’t need an object to use this variable & method.

  • It is belong to class itself, so we can access easily.

  • Static methods cannot access or change the values of instance variables.

  • Static methods do not have a .this reference

  • Static methods can access or change the values of static variables.

  • We can understand the static variables and methods are for lazy people.

  • If we write easy code, actually we don’t need non-static thing.

    • Example: pi ≈ 3.4.

      • It doesn’t change, it makes a lot of works if we make object.

      • Math.Pi.

class ClassName
{
  // static variable
  public static type variableName;

  // static method
  public static returnType methodName(parameters)
  {
        // implementation not shown
  }
}
// To call a static method or variable, use the Class Name
System.out.println(ClassName.staticVariable);
ClassName.staticMethod();

Scope: There are two types of scope

  • Local Scope: a variable can only be used inside a particular method or constructor, not outside.

    • Parameter, local variable

  • Global Scope: can be used outisde that method or constructor at least throughout the class.

    • Instance Variable

.this keyword:

  • refer to the same instance variable.

public class Assignment {
  private boolean correctAnswer; // represents the answer to an assignment, either T/F

  /** Makes a new assignment with one True/False question and sets the correct answer
  */
  public Assignment(boolean correctAnswer) {
    this.correctAnswer = correctAnswer;
  }

What is an algorithm?

  • An algorithm is a step-by-step procedure or set of rules for solving a specific problem or completing a specific task.