FRQ Hacks Testing
Question 1 - Pojos and Access Control:
Situation: The school librarian wants to create a program that stores all of the books within the library in a database and is used to manage their inventory of books available to the students. You decided to put your amazing code skills to work and help out the librarian!
a. Describe the key differences between the private and public access controllers and how it affects a POJO
b. Identify a scenario when you would use the private vs. public access controllers that isn’t the one given in the scenario above
c. Create a Book class that represents the following attributes about a book: title, author, date published, person holding the book and make sure that the objects are using a POJO, the proper getters and setters and are secure from any other modifications that the program makes later to the objects
a. Key Differences between Private and Public Access Controllers:
Public Access Modifier (public): When a class member (variable or method) is declared as public, it can be accessed from any other class. This means that the member is visible to any other class, regardless of the package in which it is declared. Public members are accessible to all other classes and can be used to interact with the object of the class.
Private Access Modifier (private): When a class member is declared as private, it can only be accessed within the same class in which it is declared. It cannot be accessed or modified directly by any other class. Private members are encapsulated within the class, making them inaccessible from outside the class. They are typically used to hide implementation details and ensure data integrity.
Impact on POJO (Plain Old Java Object): In a POJO, private access controllers are often used to encapsulate the internal state of an object, providing controlled access to its attributes through getter and setter methods. Public access controllers, on the other hand, are used to expose certain behavior or attributes of the object to external classes.
b. Scenario of Using Private vs. Public Access Controllers:
Private Access Controller: Suppose you’re designing a banking application. Within a BankAccount class, you’d likely have attributes such as balance, accountNumber, and ownerName. It’s crucial to ensure that balance and accountNumber are private because they contain sensitive information that should not be directly accessible or modified from outside the BankAccount class. Thus, you would use private access controllers to protect these attributes.
Public Access Controller: However, you might have methods like depositMoney() and withdrawMoney() which should be accessible to external classes to interact with the BankAccount object. These methods would be declared as public, allowing other classes to use them to deposit or withdraw money from the account.
public class Book {
private String title;
private String author;
private String datePublished;
private String personHolding;
public Book(String title, String author, String datePublished, String personHolding) {
this.title = title;
this.author = author;
this.datePublished = datePublished;
this.personHolding = personHolding;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDatePublished() {
return datePublished;
}
public void setDatePublished(String datePublished) {
this.datePublished = datePublished;
}
public String getPersonHolding() {
return personHolding;
}
public void setPersonHolding(String personHolding) {
this.personHolding = personHolding;
}
@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
", datePublished='" + datePublished + '\'' +
", personHolding='" + personHolding + '\'' +
'}';
}
public static void main(String[] args) {
Book book = new Book("The Great Gatsby", "F. Scott Fitzgerald", "April 10, 1925", "Luna Iwazaki");
System.out.println(book);
}
}
Book.main(null);
Book{title='The Great Gatsby', author='F. Scott Fitzgerald', datePublished='April 10, 1925', personHolding='Luna Iwazaki'}
Question 2 - Writing Classes:
(a) Describe the different features needed to create a class and what their purpose is.
(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.
Class Declaration: It begins with the keyword class followed by the class name. This defines a new class and serves as a blueprint for creating objects.
Attributes (Instance Variables): These are the data members of the class and represent the state of objects. They hold the data associated with the class and its objects.
Constructors: Constructors are special methods used for initializing objects. They have the same name as the class and are called automatically when an object is created.
Methods (Member Functions): These are the behaviors of the class and define what objects of the class can do. They can manipulate the attributes of the class and perform various operations.
Access Modifiers (public, private, protected): These control the visibility and accessibility of class members (attributes and methods) to other classes. They enforce encapsulation and protect the internal state of objects.
Getter and Setter Methods: These are special methods used to access (get) and modify (set) the values of private attributes. They help in achieving data encapsulation and ensure controlled access to class attributes.
Override Methods: Java allows overriding methods from the superclass (like toString() method from Object class) to provide custom behavior specific to the class.
public class BankAccount {
private String accountHolder;
private double balance;
public BankAccount(String accountHolder, double balance) {
this.accountHolder = accountHolder;
this.balance = balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println(amount + " deposited successfully.");
} else {
System.out.println("Invalid amount. Deposit amount should be positive.");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println(amount + " withdrawn successfully.");
} else {
System.out.println("Invalid amount or insufficient balance.");
}
}
public String getAccountHolder() {
return accountHolder;
}
public double getBalance() {
return balance;
}
public void setAccountHolder(String name) {
this.accountHolder = name;
}
@Override
public String toString() {
return "BankAccount{" +
"accountHolder='" + accountHolder + '\'' +
", balance=" + balance +
'}';
}
public static void main(String[] args) {
BankAccount account = new BankAccount("Luna Iwazaki", 1000);
System.out.println("Account Holder: " + account.getAccountHolder());
System.out.println("Initial Balance: " + account.getBalance());
account.deposit(500);
System.out.println("Balance after deposit: " + account.getBalance());
account.withdraw(200);
System.out.println("Balance after withdrawal: " + account.getBalance());
}
}
BankAccount.main(null);
Account Holder: Luna Iwazaki
Initial Balance: 1000.0
500.0 deposited successfully.
Balance after deposit: 1500.0
200.0 withdrawn successfully.
Balance after withdrawal: 1300.0
Question 5 - Inheritence:
Situation: You are developing a program to manage a zoo, where various types of animals are kept in different enclosures. To streamline your code, you decide to use inheritance to model the relationships between different types of animals and their behaviors.
(a) Explain the concept of inheritance in Java. Provide an example scenario where inheritance is useful.
(b) Code:
You need to implement a Java class hierarchy to represent different types of animals in the zoo. Create a superclass Animal with basic attributes and methods common to all animals, and at least three subclasses representing specific types of animals with additional attributes and methods. Include comments to explain your code, specifically how inheritance is used.
In Java, inheritance allows one class to inherit attributes and methods from another class.
For example, imagine you’re creating a program for a zoo. You might have a basic class called Animal with general attributes like name and age, along with methods like eat() and sleep().
Now, you can create specific types of animals like Lion, Elephant, and Monkey as subclasses of Animal. These subclasses automatically inherit the attributes and methods of Animal, so you don’t have to rewrite them.
Each subclass can then add its own unique characteristics. For instance, Lion might have a roar() method, Elephant could have a trumpet() method, and Monkey might have a swing() method.
Inheritance simplifies coding by letting you reuse common features and customize them as needed for specific types of objects.
class Animal {
protected String name;
protected int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void makeSound() {
System.out.println("The animal makes a sound.");
}
}
class Lion extends Animal {
private int maneLength;
public Lion(String name, int age, int maneLength) {
super(name, age);
this.maneLength = maneLength;
}
public void roar() {
System.out.println("The lion roars loudly.");
}
}
public class Main {
public static void main(String[] args) {
Lion lion = new Lion("Leo", 5, 10);
lion.makeSound(); // The animal makes a sound.
lion.roar(); // The lion roars loudly.
}
}
Main.main(null);
The animal makes a sound.
The lion roars loudly.