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

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

public class Book{
    private String title;
    private String author;
    private String date;
    private String person;

    // public Book(String title, String author, String date, String person){
    //     this.title = title;
    //     this.author = author;
    //     this.date = date;
    //     this.person = person;
    // }

    // getters and setter methods
    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 getDate() {
        return date;
      }

      public void setDate(String date) {
        this.date = date;
      }


      public String getPerson() {
        return person;
      }

      public void setPerson(String person) {
        this.person = person;
      }


}

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.
public class BankAccount{
    private String accountHolder;
    private double balance;

    // mutator (setter) methods
    public void setAccountHolder(String accountHolder){
        this.accountHolder = accountHolder;
    }

    public void deposit(double moneydeposity){
        this.balance = moneydeposity + this.balance;
        System.out.println(this.balance);
    }

    public void withdraw(double moneywithdraw){
        if (this.balance >= moneywithdraw){
            this.balance = this.balance - moneywithdraw;
            System.out.println(this.balance);
        }
        else{
            System.out.println("Not enough money in 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.

(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 exampleConstructor{

    private String variable1;
    private String variable2;
    private String variable3;
    
    public exampleConstructor(){
    }

    public exampleConstructor(String variable1, String variable2, String variable3){
        this.variable1 = variable1;
        this.variable2 = variable2;
        this.variable3 = variable3;
    }

    public static void main(String[] args){
        exampleConstructor Object1 = new exampleConstructor();
        exampleConstructor Object2 = new exampleConstructor("example1", "example2", "example3");
        System.out.println(Object2.variable1);

    }
}

exampleConstructor.main(null);
example1

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.

public class Temperature{
    
}

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.