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

Private variables or methods are only accessible from inside the same class. If I had two classes, pooFart1 and pooFart2, and made a private variable Poo in pooFart1, then I could not access variable poo from pooFart2. An object could not have it’s data tampered with if it were private. POJOs could be more secure, but you could no longer access them in other classes

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

If you’re making a little game or something, the data stored wouldn’t be so important so using public might be more convenient. Public means it’s easier to access the variables and you can use them in other classes if needed. If you’re working with more sensitive data, like the personal information of a human, than private may be more appropriate.

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

import java.util.Date;

class Book {
    private String title;
    private String author;
    private Date datePublished;
    private String personHoldingBook;

    public Book(String title, String author, Date datePublished, String personHoldingBook) 
    {
        this.title = title;
        this.author = author;
        this.datePublished = datePublished;
        this.personHoldingBook = personHoldingBook;
    }

    public String getTitle() 
    {
        return this.title;
    }
    public String getAuthor() 
    {
        return this.author;
    }
    public Date getDatePublished()
    {
        return this.datePublished;
    }
    public String getPersonHoldingBook() 
    {
        return this.personHoldingBook;
    }

    public static void main(String[] args) 
    {
        Book book1 = new Book("Asher Rivera, an Autobiography", "Asher Rivera", new Date(), "AJ Ruiz");
        System.out.println(book1.getTitle());
        System.out.println(book1.getAuthor());
        System.out.println(book1.getDatePublished());
        System.out.println(book1.getPersonHoldingBook());
    }
}
Book.main(null)
Asher Rivera, an Autobiography
Asher Rivera
Tue Mar 26 12:18:28 PDT 2024
AJ Ruiz

Question 2 - Writing Classes:

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

Do you really need any features? You could write class empty{} and it would work.

(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 Bank {
    private String accountHolder;
    private double balance;

    public void setAccountHolder(String name) {
        this.accountHolder = name;
    }
    public void deposit(double amount) {
        this.balance += amount;
        System.out.println("Your total is now $" + this.balance);
    }
    public void withdraw(double amount) {
        if (this.balance >= amount) {
            this.balance -= amount;
            System.out.println("Your total is now $" + this.balance);
        } else {
            System.out.println("you don't have that much money");
        }
    }

    public static void main(String[] args) {
        Bank customer = new Bank();
        customer.setAccountHolder("John Smith");
        customer.deposit(20.10);
        customer.deposit(1.15);
        customer.withdraw(100000);
        customer.withdraw(5);
    }
}
Bank.main(null)
Your total is now $20.1
Your total is now $21.25
you don't have that much money
Your total is now $16.25

Question 3 - Instantiation of a Class

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

A constructor is a shortcut for creating an object. WHen you define the variables a class will have, you’ll need to input a value for each of those variables individually, but with a constructor, you can input the values for each within a single line. It would probably run when you call it. In a constructor, it will generally just be those lines you would have typed individually without a constructor. It’ll be a few this.variable = variable, which would set the value of an variable in the object.

(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.

class Sinep {
    public int x;
    public String y;
    public double z;

    public Sinep(int x) {
        this.x = x;
    }
    public Sinep(int x, String y) {
        this.x = x;
        this.y = y;
    }
    public Sinep(double z) {
        this.z = z;
    }
    public Sinep(int x, String y, double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public static void main(String[] args) {
        Sinep obj = new Sinep(5);
        Sinep pojo = new Sinep(4.5);
        Sinep pogo = new Sinep(3, " Hello");
        Sinep bogo = new Sinep(2, " Goodbye ", 1.15);

        System.out.println(obj.x);
        System.out.println(pojo.z);
        System.out.println(pogo.x + pogo.y);
        System.out.println(bogo.x + bogo.y + bogo.z);
    }
}
Sinep.main(null)
5
4.5
3 Hello
2 Goodbye 1.15

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.