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


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.

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 special method used for object initialization. When the object is created, the constructor is called, and when called, the constructor allocates the memory needed for the object. The default state of a constructor is a no argument constructor, which is automatically created when no arguments are specified.

(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 Cars {
    // instantiating params used later
    String model;
    int hp;
    int price;

    // default constructor
    public Cars(){
        model = "Techrules GT96";
        hp = 1030;
        price = 1;
    }

    public Cars(String name){
        model = name;
        hp = 1160;
        price = 2;
    }

    public Cars(String name, int horsepower){
        model = name;
        hp = horsepower;
        price = 500;
    }

    public Cars(String name, int horsepower, int cost){
        model = name;
        hp = horsepower;
        price = cost;
    }

    public static void main(String[] args){
        Cars car1 = new Cars();
        System.out.println("Call 1: No arguments given in creation of car object | " + car1.model + ": " + car1.hp + " hp | $" + car1.price);

        Cars car2 = new Cars("Porsche 919 Hybrid EVO");
        System.out.println("Call 2: Name argument passed | " + car2.model + ": " + car2.hp + " hp | $" + car2.price);

        Cars car3 = new Cars("Volkswagen Golf", 30);
        System.out.println("Call 3: Name and Horsepower passed | " + car3.model + ": " + car3.hp + " hp | $" + car3.price);

        Cars car4 = new Cars("Fiat Panda", 10, 50);
        System.out.println("Call 3: Name and Horsepower passed | " + car4.model + ": " + car4.hp + " hp | $" + car4.price);
    }
}

Cars.main(null);
Call 1: No arguments given in creation of car object | Techrules GT96: 1030 hp | $1
Call 2: Name argument passed | Porsche 919 Hybrid EVO: 1160 hp | $2
Call 3: Name and Horsepower passed | Volkswagen Golf: 30 hp | $500
Call 3: Name and Horsepower passed | Fiat Panda: 10 hp | $50

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.

Inheritance in Java is a mechanism where a new class (derived class or subclass) is created from an existing class (base class or superclass), inheriting the attributes and methods of the superclass. The subclass can then extend or modify the behavior of the superclass, while also having its own unique attributes and methods. One scenario in which inheritance can be used is in creating a software application for managing employees in a company. You might have a base class called Employee with attributes like name, id, and salary, along with methods like calculateSalary(). Now, you might have different types of employees, such as FullTimeEmployee and PartTimeEmployee. Instead of duplicating the attributes and methods in each subclass, you can use inheritance.

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

public class Animal {
    private String species;
    private int age;
    
    // Constructor for Animal class
    public Animal(String species, int age) {
        this.species = species;
        this.age = age;
    }
    
    // Getters and setters for species and age
    public String getSpecies() {
        return species;
    }
    
    public void setSpecies(String species) {
        this.species = species;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    // Method to display information about the animal
    public void displayInfo() {
        System.out.println("Species: " + species);
        System.out.println("Age: " + age);
    }
}

// Penguin subclass which inherits Animal class traits
public class Penguin extends Animal {
    private String pattern;
    
    // Constructor for Penguin class
    public Penguin(String species, int age, String pattern) {
        super(species, age); 
        this.pattern = pattern;
    }
    
    // Getter and setter for pattern
    public String getPattern() {
        return pattern;
    }
    
    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    // Method to display information about the penguin
    public void displayInfo() {
        super.displayInfo(); // Calling the displayInfo() method of superclass Animal
        System.out.println("Pattern: " + pattern);
    }
}

// Cheetah subclass which inherits Animal class traits
public class Cheetah extends Animal {
    private int topSpeed;
    
    // Constructor for Cheetah class
    public Cheetah(String species, int age, int topSpeed) {
        super(species, age); // Calling the constructor of superclass Animal
        this.topSpeed = topSpeed; 
    }
    
    // Getter and setters for topSpeed
    public int getTopSpeed() {
        return topSpeed;
    }
    
    public void setTopSpeed(int topSpeed) {
        this.topSpeed = topSpeed;
    }

    public void cheetahNoise() { // A new method specific to cheetah class
        System.out.println("Rrrrrrowwwwwww");
    }
    
    // Method to display information about the cheetah
    public void displayInfo() {
        super.displayInfo(); // Calling the displayInfo() method of superclass Animal
        System.out.println("Top Speed: " + topSpeed);
    }
}

// Dolphin subclass which inherits Animal class traits
public class Dolphin extends Animal {
    private int length; 
    
    // Constructor for Dolphin class
    public Dolphin(String species, int age, int length) {
        super(species, age); // Calling the constructor of superclass Animal
        this.length = length;
    }
    
    // Getter and setter for length
    public int getLength() {
        return length;
    }
    
    public void setLength(int length) {
        this.length = length;
    }

    public void dolphinSound() { 
        System.out.println("EeEeEeEeEeEeEeEeEeEe"); 
    }
    
    // Method to display information about the dolphin
    public void displayInfo() {
        super.displayInfo(); // Calling the displayInfo() method of superclass Animal
        System.out.println("Length: " + length);
    }
}