Home
Time Box
Calculator
Snake
Blogs
Hacks

Workshop 2 Hacks • 16 min read

Description

Classes Workshop Hacks


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:

public class BankAccount {
    private String accountHolder;
    private double balance;

    // Constructor
    public BankAccount(String accountHolder, double balance) {
        this.accountHolder = accountHolder;
        this.balance = balance;
    }

    // Setter for accountHolder
    public void setAccountHolder(String accountHolder) {
        this.accountHolder = accountHolder;
    }

    // Method to deposit amount
    public void deposit(double amount) {
        if (amount > 0) {
            this.balance += amount;
        }
    }

    // Method to withdraw amount
    public void withdraw(double amount) {
        if (amount > 0 && amount <= this.balance) {
            this.balance -= amount;
        }
    }

    // Getter for balance
    public double getBalance() {
        return this.balance;
    }

    public static void main(String[] args) {
        BankAccount account = new BankAccount("John Doe", 1000);
        System.out.println("Initial balance: " + account.getBalance());
        account.deposit(500);
        System.out.println("Balance after depositing 500: " + account.getBalance());
        account.withdraw(200);
        System.out.println("Balance after withdrawing 200: " + account.getBalance());
    }
}

BankAccount.main(null);
Initial balance: 1000.0
Balance after depositing 500: 1500.0
Balance after withdrawing 200: 1300.0

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 Person {
    private String name;
    private int age;
    private String address;

    // Constructor with 1 parameter
    public Person(String name) {
        this.name = name;
        this.age = 0; // Default value
        this.address = ""; // Default value
    }

    // Constructor with 2 parameters
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        this.address = ""; // Default value
    }

    // Constructor with 3 parameters
    public Person(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    // Method to print person details
    public void printPerson() {
        System.out.println("Name: " + this.name + ", Age: " + this.age + ", Address: " + this.address);
    }
}

// Create objects using different constructors
Person person1 = new Person("John");
Person person2 = new Person("Jane", 25);
Person person3 = new Person("Jack", 30, "123 Street, City");

// Print person details
person1.printPerson();
person2.printPerson();
person3.printPerson();
Name: John, Age: 0, Address: 
Name: Jane, Age: 25, Address: 
Name: Jack, Age: 30, Address: 123 Street, City

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 {
    private double celsius;

    // Constructor
    public Temperature(double celsius) {
        this.celsius = celsius;
    }

    // Getter for temperature in Celsius
    public double getTemperature() {
        return this.celsius;
    }

    // Setter for temperature in Celsius
    public void setTemperature(double celsius) {
        this.celsius = celsius;
    }

    // Method to convert Celsius to Fahrenheit
    public double toFahrenheit() {
        return this.celsius * 9/5 + 32;
    }

    public static void main(String[] args) {
        // Create a Temperature object
        Temperature temp = new Temperature(25);

        // Print temperature in Celsius
        System.out.println("Temperature in Celsius: " + temp.getTemperature());

        // Convert temperature to Fahrenheit
        System.out.println("Temperature in Fahrenheit: " + temp.toFahrenheit());
    }
}

Temperature.main(null);
Temperature in Celsius: 25.0
Temperature in Fahrenheit: 77.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.

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