What will we be teaching?

We will be teaching Unit 5, Writing Classes. We will explore the anatomy of a class, fields, methods, and constructors. We will learn how constructors initialize objects and the different types, how mutators (setters) modify object properties, and how static variables belong to the class rather than any instance. By the end, there will be a solid understanding of how to create and manage objects efficiently in Java

Unit 5 Topics For Learning:

  • 5.1 Anatomy of a Class
  • 5.2 Constructors
  • 5.4 Accessor Methods
  • 5.5 Mutator
  • 5.7 Static Variables and Methods

Why do we need to write classes?

Writing classes in Java is essential because it allows you to organize your code into reusable, modular components. Think of a class as a blueprint for creating objects. Without classes, your code would be cluttered and difficult to manage, especially as projects grow larger. Why not just write all your code in one place? Well, that would make it hard to maintain and update, leading to errors and inefficiency. Classes enable you to encapsulate data and behavior, making your code more flexible, scalable, and easier to troubleshoot. This structured approach is key for building complex, real-world applications.

Homework Assignment: Constructors, Mutators, Accessors, and Static Variables in Java

Objective:

Create a BankAccount class to practice working with constructors, mutators (setters), accessors (getters), and static variables in Java.

Instructions:

Class: BankAccount

  • Instance Variables:
    • String accountHolderName
    • double balance
  • Static Variable:
    • static int totalAccounts (tracks the number of accounts created)

Constructors:

  • Default constructor: Sets accountHolderName to "Unknown" and balance to 0.0.
  • Parameterized constructor: Accepts accountHolderName and balance as parameters.
  • Both constructors should increment totalAccounts.

Mutator Methods:

  • void setAccountHolderName(String name): Updates the account holder’s name.
  • void deposit(double amount): Adds money to the balance.
  • void withdraw(double amount): Subtracts money from the balance (if funds are available).

Accessor Methods:

  • String getAccountHolderName(): Returns the account holder’s name.
  • double getBalance(): Returns the account balance.
  • static int getTotalAccounts(): Returns the total number of accounts created.

Main Program (BankApp):

  • Create three BankAccount objects.
  • Modify account holder names and balances using setters.
  • Print account details using getters.
  • Display the total number of accounts created.

Example Output:

Account Holder: Alice  
Balance: 500.0

Account Holder: Bob  
Balance: 1000.0

Account Holder: Charlie  
Balance: 750.0

Total number of accounts created: 3

Submission:

Submit a Jupyter Notebook file containing your final code.

class BankAccount {
    // instance variables
    String accountHolderName;
    double balance;

    // the static variables
    static int totalAccounts = 0;

    // this is the deafult constructor
    BankAccount() {
        this.accountHolderName = "Unknown";
        this.balance = 0.0;
        totalAccounts++;
    }

    // parameterized constructor
    BankAccount(String accountHolderName, double balance) {
        this.accountHolderName = accountHolderName;
        this.balance = balance;
        totalAccounts++;
    }

    // mutator method 
    void setAccountHolderName(String name) {
        this.accountHolderName = name;
    }

    // mutator method to deposit money
    void deposit(double amount) {
        if (amount > 0) {
            this.balance += amount;
        }
    }

    // mutator method to withdraw money
    void withdraw(double amount) {
        if (amount > 0 && this.balance >= amount) {
            this.balance -= amount;
        } else {
            System.out.println("not enough money u brokie");
        }
    }

    // accessor method to get the name
    String getAccountHolderName() {
        return this.accountHolderName;
    }

    // accessor method to get the balance
    double getBalance() {
        return this.balance;
    }

    // static accessor method to get the total number of accounts created
    static int getTotalAccounts() {
        return totalAccounts;
    }
}

BankAccount account1 = new BankAccount();
BankAccount account2 = new BankAccount("Nisarg", 1.0);
BankAccount account3 = new BankAccount("Sri", 750.0);

//  this part modifies the account
account1.setAccountHolderName("Mort");
account1.deposit(10000.0);

// prints account details
System.out.println("account holder: " + account1.getAccountHolderName());
System.out.println("Balance: " + account1.getBalance());

System.out.println("account holder: " + account2.getAccountHolderName());
System.out.println("balance: " + account2.getBalance());

System.out.println("account Holder: " + account3.getAccountHolderName());
System.out.println("balance: " + account3.getBalance());

// displays total # of accounts created
System.out.println("total # of accounts created: " + BankAccount.getTotalAccounts());

account holder: Mort
Balance: 10000.0
account holder: Nisarg
balance: 1.0
account Holder: Sri
balance: 750.0
total # of accounts created: 6