14 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

b. Identify a scenario when you would use the private vs. public access controllers that isn’t the one given in the scenario above When making a app, there can be some data that is personal and it can be useful to utilize public and private controllers, so that the data within the private class can not be accessed from other classes, but public constructors can.

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 holder;

    public Book(String title, String author, String datePublished, String holder) {
        this.title = title;
        this.author = author;
        this.datePublished = datePublished;
        this.holder = holder;
    }
    public String getTitle() {
        return title;
    }
    public String getAuthor() {
        return author;
    }
    public String getDate() {
        return datePublished;
    }
    public String getHolder() {
        return holder;
    }
    public void setHolder(String holder) {
        this.holder = holder;
        System.out.println("Holder updated");
    }
}

public class Q1 {
    public static void main(String[] args) {
        Book greatGatsby = new Book("Geronimo Silton", "G. Silton", "12-18-2015", "none");
        System.out.println(greatGatsby.getAuthor());
        System.out.println(greatGatsby.getTitle());
        System.out.println(greatGatsby.getDate());
        System.out.println(greatGatsby.getHolder());
        greatGatsby.setHolder("Edwin");
        System.out.println(greatGatsby.getHolder());
    }
}
Q1.main(null);
G. Silton
Geronimo Silton
12-18-2015
none
Holder updated
Edwin

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.
// N/A

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.

// N/A

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. A wrapper class is where objects contain primitive data. They “wrap” primitive values in an object, and can be accessed as an argument and add can make primitives into objects.

(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 partA {
    public static void main(String[] args) {
        Integer num = new Integer(1);
        System.out.println(num);
    }
}
partA.main(null);
1
public class Temperature {
    private double temperatureValue;

    public Temperature(double temperatureValue) {
        this.temperatureValue = temperatureValue;
    }

    public double getTemp() {
        return temperatureValue;
    }

    public void setTemp(double value) {
        temperatureValue = value;
    }

    public double toFahrenheit() {
        return (temperatureValue * 9 / 5 + 32);
    }

    public static void main(String[] args) {
        Temperature temp = new Temperature (37.4);
        System.out.println("Celsius: " + temp.getTemp());
        System.out.println("In Fahrenheit: " + temp.toFahrenheit());
        temp.setTemp(30.0);
        System.out.println("After Celsius set to 30: " + temp.getTemp());
    }
}
Temperature.main(null);
Celsius: 37.4
In Fahrenheit: 99.32
After Celsius set to 30: 30.0

Question 5 - Inheritance:

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.

// N/A