CSA Blog

Ian Wu

Unit 5 - Writing Classes P1

Intro Anatomy of a Class Constructors Accessor Methods Mutator Methods Static Variables Game

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.

public static void print(String p) {
    System.out.println(p);
}

public class BankAccount {
    private String accountHolder;
    private double balance;
    static int totalAccounts = 0;

    BankAccount() {
        this("Unknown", 0);
    }

    BankAccount(String a, double b) {
        this.accountHolder = a;
        this.balance = b;
        totalAccounts += 1;
    }

    public void setAccountHolderName(String n) {
        this.accountHolder = n;
    }

    public void deposit(double amount) {
        this.balance += amount;
    }

    public void withdraw(double amount) {
        this.balance -= amount;
    }

    public String getAccountHolderName() {
        return this.accountHolder;
    }

    public double getBalance() {
        return this.balance;
    }

    public static int getTotalAccounts() {
        return totalAccounts;
    }

    public String toString() {
        return (this.accountHolder + "\n" + this.balance);
    }
}

BankAccount dod = new BankAccount("dod", 20);
BankAccount fof = new BankAccount("fof", 0);
BankAccount gog = new BankAccount();

print(dod.toString());
print(fof.toString());
print(gog.toString());

dod.withdraw(10);
fof.deposit(100);
gog.setAccountHolderName("gog");

print("\n");

print(dod.toString());
print(fof.toString());
print(gog.toString());

print("\n");

print("Total Accounts: " + BankAccount.getTotalAccounts());
dod
20.0
fof
0.0
Unknown
0.0


dod
10.0
fof
100.0
gog
0.0


Total Accounts: 3

Unit 4 - Iteration P1

4.1 While Loop 4.2 For Loop 4.3 String Iteration 4.4 Nested Iteration Unit 4 HW Quiz

Collegeboard Resources for loops/iteration

## AP Computer Science A - Unit 4 Home Page

Welcome to Unit 4! In this unit, we will explore various concepts of Iteration in Java, focusing on while, for, for each, do while loops and string manipulation

Learning Goals:

  • Understand how to declare, initialize, loops
  • Learn how to iterate through strings
  • Practice writing algorithms that use iteration

Key Topics:

Topic 4.1 - while Loops Topic 4.2 - for Loops Topic 4.3 - Developing Algorithms Using Strings Topic 4.4 - Nested Iteration

2
15


Iteration: 0

Current Velocity: 2, 2

Unit 3 Boolean Expressions - 3.1

3.1: Boolean Expressions 3.2: If Control Flow 3.3: If Else 3.4: Else If 3.5: Compound Booleans 3.6: Equivalent Booleans 3.7: Comparing Objects 3.8: Homework

3.1 Boolean Expressions

Java’s relational operators

  • equal to: ==
  • not equal to: !=
  • less than: <
  • greater than: >
  • less than or equal to: <=
  • greater than or equal to >=

Hack!

int myAge = 15;
int otherAge = 45; 

using these integers, determine weather the following statements are True or False

  1. False
  2. True
  3. True
  4. False
  5. False
  6. True

Screenshot 2024-09-15 at 10 00 54 PM

Strings

popcorn hack

whats wrong with this code? (below)


String myName = "Alisha";

myName != "Anika";
myName == "Alisha";
true

comparison of string objects should be done using String methods, NOT integer methods.

  • .equal
  • compare to
String myName = "Alisha";
boolean areNamesEqual = myName.equals("Alisha");  

if (areNamesEqual) {
    System.out.println("True");
} else {
    System.out.println("False");
}

homework question

Screenshot 2024-09-16 at 8 05 24 AM what is the precondition for num?

The number of digits in num between one and six, inclusive.