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 access controllers means that the variables can only be accessed within the class they are declared in, which mean that a POJO needs to have dedicated methods to access and modify it. Public access controllers means that the variables can be accessed everywhere, throughout the whole program.

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 don’t want your users to be able to access certain data, like maybe passwords or something, you can make the variable private to prevent it being accessed without specific methods in their classes.

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("The Great Gatsby", "Fitzgerald, F. Scott", "04-10-1925", "none");
        System.out.println(greatGatsby.getAuthor());
        System.out.println(greatGatsby.getTitle());
        System.out.println(greatGatsby.getDate());
        System.out.println(greatGatsby.getHolder());
        greatGatsby.setHolder("Vinay");
        System.out.println(greatGatsby.getHolder());
    }
}
Q1.main(null);
Fitzgerald, F. Scott
The Great Gatsby
04-10-1925
none
Holder updated
Vinay

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 a a special method that runs when a class is created. It usually initializes instance variables in the object. They can take no arguments (in which case they will initialize instance variables to a default value), or 1+ arguments, initializing instance variables to those arguments as described in the 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 Person {
    private int age;
    private String name;
    private double height;

    public Person(int age, String name, double height) {
        this.age = age;
        this.name = name;
        this.height = height;
    }

    public Person() {
        this.age = 0;
        this.name = "John Smith";
        this.height = 0.0;
    }
    
    public void getAge() {
        System.out.println(age);
    }
}

public class Q3 {
    public static void main(String[] args) {
        Person james = new Person(19, "James Hill", 65.9);
        Person nemo = new Person();
        james.getAge();
        nemo.getAge();
    }
}
Q3.main(null);
19
0

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 is a Java concept, and it is when one class “inherits” methods and variables from another class, called the parents class. An example scenario where this can be used is when you want code for different jobs. You can have a Person class, which can have basic methods, and then be extended to other job classes, like “Construction Worker”, or “Programmer”

(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 int weight;
    private int height;
    private String species;

    public Animal(int weight, int height, String species) {
        this.weight = weight;
        this.height = height;
        this.species = species;
    }

    public void move() {
        System.out.println("The animal is moving");
    }

    public void grow() {
        weight+=10;
        height+=10;
    }
}

public class Eagle extends Animal {
    public Eagle(int weight, int height, String species) {
        super(weight, height, species);
    }

    public void move() {
        System.out.println("The bird is flying");
    }

    public void squawk() {
        System.out.println("squawk.");
    }
}

public class Wolf extends Animal {
    public Wolf(int weight, int height, String species) {
        super(weight, height, species);
    }

    public void move() {
        System.out.println("The wolf is running");
    }

    public void howl() {
        System.out.println("howl.");
    }
}

public class Fish extends Animal {
    public Fish(int weight, int height, String species) {
        super(weight, height, species);
    }

    public void move() {
        System.out.println("The fish is swimming");
    }

    public void glub() {
        System.out.println("glub.");
    }
}