Home
Time Box
Calculator
Snake
Blogs
Hacks

Question 2 • 20 min read


Free Response Questions

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 Public

Private

b. Identify a scenario when you would use the private vs. public access controllers that isn’t the one given in the scenario above Private

Public

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

public class Book {
    private String title;
    private String author;
    private String datePublished;
    private String personHolding;

    // Constructor
    public Book(String title, String author, String datePublished, String personHolding) {
        this.title = title;
        this.author = author;
        this.datePublished = datePublished;
        this.personHolding = personHolding;
    }

    // Getters
    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public String getDatePublished() {
        return datePublished;
    }

    public String getPersonHolding() {
        return personHolding;
    }

    // Setters
    public void setTitle(String title) {
        this.title = title;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setDatePublished(String datePublished) {
        this.datePublished = datePublished;
    }

    public void setPersonHolding(String personHolding) {
        this.personHolding = personHolding;
    }
}

Question 2 - Writing Classes:

(a) Describe the different features needed to create a class and what their purpose is.

Create a Java class BankAccount to represent a simple bank account. This class should have the following attributes:

public class BankAccount {
    private String accountHolder;
    private double balance;

    public void setAccountHolder(String name){
        this.accountHolder = name;
    }

    public void deposit(Double amount){
        if (amount > 0) {
            this.balance += amount;
        } else {
            System.out.println("Deposit amount must be positive.");
        }
    }

    public void withdraw(double amount) {
        if (amount > 0 && amount <= this.balance) {
            this.balance -= amount;
        } else {
            System.out.println("Withdrawal amount is invalid or exceeds balance.");
        }
    }

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

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

Question 3 - Instantiation of a Class

(a) Explain how a constructor works, including when it runs and what generally is done within a constructor.

public class Person {
    private String name;
    private int age;
    private String email;

    // Constructor with all three variables
    public Person(String name, int age, String email) {
        this.name = name;
        this.age = age;
        this.email = email;
    }

    // Overloaded constructor with two variables (name and age)
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        this.email = ""; // Default value or could be set as null
    }

    // Overloaded constructor with only name
    public Person(String name) {
        this.name = name;
        this.age = 0; // Default value
        this.email = ""; // Default value or could be set as null
    }

    // Getters for the class variables
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getEmail() {
        return email;
    }
}

public class Main {
    public static void main(String[] args) {
        // Using constructor with all three variables
        Person person1 = new Person("Alice", 30, "alice@example.com");
        
        // Using constructor with two variables (name and age)
        Person person2 = new Person("Bob", 25);
        
        // Output to demonstrate that different constructors were called
        System.out.println("Person 1: " + person1.getName() + ", " + person1.getAge() + ", " + person1.getEmail());
        System.out.println("Person 2: " + person2.getName() + ", " + person2.getAge() + ", " + person2.getEmail());
    }
}

Main.main(null);
Person 1: Alice, 30, alice@example.com
Person 2: Bob, 25, 

Question 4 - Wrapper Classes:

(a) Provide a brief summary of what a wrapper class is and provide a small code block showing a basic example of a wrapper class.

(b) Create a Java wrapper class called Temperature to represent temperatures in Celsius. Your Temperature class should have the following features:

Fields:

A private double field to store the temperature value in Celsius.

Constructor:

A constructor that takes a double value representing the temperature in Celsius and initializes the field.

Methods:

getTemperature(): A method that returns the temperature value in Celsius. setTemperature(double value): A method that sets a new temperature value in Celsius. toFahrenheit(): A method that converts the temperature from Celsius to Fahrenheit and returns the result as a double value.


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.