FRQs
Question 2 - Writing Classes:
(a) Describe the different features needed to create a class and what their purpose is.
- Class Name: identifies the class, use for constructor later
- Instance Variables: store data in the object
- Methods: define functions of class
- Constructor: instantiate an object of the class
- Encapsulation: public (anywhere), private (within class), protected (within class and subclass): (defines scope for the class/visibiltiy)
- Inheritance: allows class to inherit data/methods from other classes
- Polymorphism: allows objects of different classes to be treated as part of one superclass
- Abstraction: hides implementation details of an object
(b) Code:
Create a Java class BankAccount to represent a simple bank account. This class should have the following attributes:
- accountHolder (String): The name of the account holder.
- balance (double): The current balance in the account.
Implement the following mutator (setter) methods for the BankAccount class:
- setAccountHolder(String name): Sets the name of the account holder.
- deposit(double amount): Deposits a given amount into the account.
- withdraw(double amount): Withdraws a given amount from the account, but only if the withdrawal amount is less than or equal to the current balance. Ensure that the balance is never negative.
public class BankAccount {
// Attributes
private String accountHolder;
private double balance;
// Constructor
public BankAccount(String accountHolder, double balance) {
this.accountHolder = accountHolder;
if (balance >= 0) {
this.balance = balance;
} else {
System.out.println("Initial balance cannot be negative. Setting balance to 0.");
this.balance = 0;
}
}
// Setter name for account holder
public void setAccountHolder(String name) {
this.accountHolder = name;
}
// Deposit method
public void deposit(double amount) {
if (amount > 0) {
this.balance += amount;
System.out.println(amount + " deposited successfully.");
} else {
System.out.println("Invalid amount. Deposit amount should be greater than 0.");
}
}
// Withdraw method
public void withdraw(double amount) {
if (amount > 0) {
if (amount <= this.balance) {
this.balance -= amount;
System.out.println(amount + " withdrawn successfully.");
} else {
System.out.println("Insufficient balance. Withdrawal amount exceeds the current balance.");
}
} else {
System.out.println("Invalid amount. Withdrawal amount should be greater than 0.");
}
}
// Getter method for balance
public double getBalance() {
return this.balance;
}
// Main method to test the BankAccount class
public static void main(String[] args) {
// Creating a BankAccount object
BankAccount account = new BankAccount("John Doe", 1000);
// Display initial balance
System.out.println("Initial balance: " + account.getBalance());
// Depositing and withdrawing money
account.deposit(500);
account.withdraw(200);
account.withdraw(1500); // This withdrawal should fail due to insufficient balance
// Display updated balance
System.out.println("Updated balance: " + account.getBalance());
}
}
BankAccount.main(null);
Initial balance: 1000.0
500.0 deposited successfully.
200.0 withdrawn successfully.
Insufficient balance. Withdrawal amount exceeds the current balance.
Updated balance: 1300.0
Question 3 - Instantiation of a Class
(a) Explain how a constructor works, including when it runs and what generally is done within a constructor.
- Allows instantiation of a class to make an object of the class using
new
keyword - Can take in parameters to give initial values of the object
- Use
super
to access superclass andthis
to access construtors in the class - Can be static or non-static to access these values without making an object of the class
(b) Create an example of an overloaded constructor within a class. You must use at least three variables. Include the correct initialization of variables and correct headers for the constructor. Then, run the constructor at least twice with different variables and demonstrate that these two objects called different constructors.
public class SchoolMember {
// Instance variables
private String memberName;
private int memberAge;
private String memberPosition;
// Constructor with memberName parameter
public SchoolMember(String memberName) {
this.memberName = memberName;
this.memberAge = 0;
this.memberPosition = "None";
System.out.println("Constructor with memberName parameter called.");
}
// Constructor with memberName and memberAge parameters
public SchoolMember(String memberName, int memberAge) {
this.memberName = memberName;
this.memberAge = memberAge;
this.memberPosition = "None"; // Default memberPosition
System.out.println("Constructor with memberName and memberAge parameters called.");
}
// Constructor with memberName, memberAge, and memberPosition parameters
public SchoolMember(String memberName, int memberAge, String memberPosition) {
this.memberName = memberName;
this.memberAge = memberAge;
this.memberPosition = memberPosition;
System.out.println("Constructor with memberName, memberAge, and memberPosition parameters called.");
}
// Getter methods
public String getMemberName() {
return memberName;
}
public int getMemberAge() {
return memberAge;
}
public String getMemberPosition() {
return memberPosition;
}
// Main method to test the SchoolMember class
public static void main(String[] args) {
// Creating SchoolMember objects using different constructors
SchoolMember member1 = new SchoolMember("John");
SchoolMember member2 = new SchoolMember("Jane", 30);
SchoolMember member3 = new SchoolMember("Doe", 25, "Teacher");
// Displaying the details of the created SchoolMember objects
System.out.println("Member1 details - Name: " + member1.getMemberName() + ", Age: " + member1.getMemberAge() + ", Position: " + member1.getMemberPosition());
System.out.println("Member2 details - Name: " + member2.getMemberName() + ", Age: " + member2.getMemberAge() + ", Position: " + member2.getMemberPosition());
System.out.println("Member3 details - Name: " + member3.getMemberName() + ", Age: " + member3.getMemberAge() + ", Position: " + member3.getMemberPosition());
}
}
SchoolMember.main(null);
Constructor with memberName parameter called.
Constructor with memberName and memberAge parameters called.
Constructor with memberName, memberAge, and memberPosition parameters called.
Member1 details - Name: John, Age: 0, Position: None
Member2 details - Name: Jane, Age: 30, Position: None
Member3 details - Name: Doe, Age: 25, Position: Teacher