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

a. The difference between priv and public access controllers is that publich has more scope and accessibility. Priv restricts the scope to within the class itself. Public can be accessed in classses outside the class itself. Priv objects can only be access by the class itself.

b. I would use priv when dealing with sensitive information. For example, in a banking app, the account itself would be an instance of a private class which would prevent people from doing unauthorized deposits.

c. see code cell below

public class Book {
    private String title;
    private String author;
    private String datePublished;
    private String personHolding;
 
    public Book(String title, String author, String datePublished) {
        this.title = title;
        this.author = author;
        this.datePublished = datePublished;
        this.personHolding = null;
}
 // title getter setter 
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    // author getter setter
    public String getAuthor() {
        return author;
    }


    public void setAuthor(String author) {
        this.author = author;
    }

    // datePublished getter setter
    public String getDatePublished() {
        return datePublished;
    }

    public void setDatePublished(String datePublished) {
        this.datePublished = datePublished;
    }

    public String getPersonHolding() {
        return personHolding;
    }

    // setter personHolding
    public void setPersonHolding(String personHolding) {
        this.personHolding = personHolding;
    }
}

Book testBook = new Book("Title", "Tanisha Patil", "March 4, 2022");
System.out.println(testBook.getTitle()) ;
System.out.println(testBook.getAuthor()) ;
Title
Tanisha Patil

Question 2 - Writing Classes:

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

  • Class Name: Used to create instances. Start with a capital letter. Ex. public class Book

  • Attributes : represent properties of object. Ex. datePublished

  • Constructor: function sed to initialize new objects.

  • Methods: Functions for accessing for example getters and setters

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

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

    // getters
    public String getAccountHolder() {
        return accountHolder;
    }

    public double getBalance() {
        return balance;
    }

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

    // deposite method
    public void deposit(double deposit) {
        if (deposit > 0) {
            balance += deposit;
        }
    }

    // withdraw method
    public void withdraw(double withdrawal) {
        if (withdrawal > 0 && withdrawal <= balance) {
            balance -= withdrawal;
        }
    }
}


// Testing
BankAccount myAccount = new BankAccount("Tanisha ", 500);
myAccount.deposit(200);
System.out.println("After Deposit: " + myAccount.getBalance());

myAccount.withdraw(600);
System.out.println("After withdrawal: " + myAccount.getBalance());
After Deposit: 700.0
After withdrawal: 100.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.

(a)

A constructor in Java is a type of method that is called when an instance of a class is created. Its main purpose is to initialize the newly created object. A constructor runs automatically using the new keyword.

(b) see code below

public class Person {
    private String name;
    private int age;
    private String gender;

    
    public Person(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        System.out.println("Constructor 1 called.");
    }

  
    public Person(String name, int age) {
        this(name, age, "Unknown"); 
        System.out.println("Constructor 2 called.");
    }

    // Getter methods
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getGender() {
        return gender;
    }

}

Person person1 = new Person("Alice", 30, "Female");
Person person2 = new Person("Bob", 25);

        System.out.println("Person 1: Name - " + person1.getName() + ", Age - " + person1.getAge() + ", Gender - " + person1.getGender());
        System.out.println("Person 2: Name - " + person2.getName() + ", Age - " + person2.getAge() + ", Gender - " + person2.getGender());
Constructor 1 called.
Constructor 1 called.
Constructor 2 called.
Person 1: Name - Alice, Age - 30, Gender - Female
Person 2: Name - Bob, Age - 25, Gender - Unknown

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.

(a): Wrapper classes convert primitive types to objects. I will demonstrate this in the code below.

(b) see code below

// a 
public class IntegerWrapper {
    private int value;

    public IntegerWrapper(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }


}

IntegerWrapper wrapper = new IntegerWrapper(10);

System.out.println("Initial value: " + wrapper.getValue());
wrapper.setValue(20);
System.out.println("Updated value: " + wrapper.getValue());
Initial value: 10
Updated value: 20
public class Temperature {
    private double celsius;

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

    public double getTemperature() {
        return celsius;
    }

    public void setTemperature(double celsius) {
        this.celsius = celsius;
    }

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

Temperature temp = new Temperature(2485.0);
System.out.println("Temperature - Celsius: " + temp.getTemperature());
System.out.println("Temperature - Fahrenheit: " + temp.toFahrenheit());

temp.setTemperature(4007.0);
Temperature - Celsius: 2485.0
Temperature - Fahrenheit: 4505.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 in OOP allows a new class - subclass- to inherit attributes and behaviors from an existing class -parent class. We can have a parent class Vehicle which has common attributes and methods shared by all vehicles. Then, we can create subclasses such as Car, Bicycle, and Motorcycle which inherit from the Vehicle class. Each subclass can still have its own unique properties.

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

// parent Animal 
class Animal {
    protected String name;
    protected int age;

    // constructor
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    //  method display basic information about the animal
    public void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

// child representing animal type Lion
class Lion extends Animal {
    private String roarSound;


    public Lion(String name, int age, String roarSound) {
        super(name, age);
        this.roarSound = roarSound;
    }

    public void displayLionInfo() {
        System.out.println("Roar Sound: " + roarSound);
    }
  // only lion has this method
    public void roar() {
        System.out.println(name + " roars: " + roarSound);
    }
}

// child representing animal type Penguin
class Penguin extends Animal {
    private boolean canSwim;

    public Penguin(String name, int age, boolean canSwim) {
        super(name, age);
        this.canSwim = canSwim;
    }

    public void displayPenguinInfo() {
        System.out.println("Can Swim: " + canSwim);
    }

    // only penguin has this method 
    public void swim() {
        if (canSwim)
            System.out.println(name + " is swimming.");
        else
            System.out.println(name + " cannot swim.");
    }
}

// // child representing animal type Elephant
class Elephant extends Animal {
    private int trunkLength;

    public Elephant(String name, int age, int trunkLength) {
        super(name, age);
        this.trunkLength = trunkLength;
    }

    public void displayElephantInfo() {
        System.out.println("Trunk Length: " + trunkLength + " meters");
    }

// only elephant can do this 
    public void trumpet() {
        System.out.println(name + " trumpets loudly!");
    }
}

AP Computer Science A Exam Study Plan

Duration: March 28th to May 8th

Goals:

  • Familiarize yourself with the format of FRQ types 1-4.
  • Practice solving FRQs of each type.
  • Review key concepts and programming constructs.

Resources:

  • Official College Board AP Computer Science A materials.
  • Textbooks (e.g., “Java: A Beginner’s Guide” by Herbert Schildt).
  • Online platforms for practice (e.g., Khan Academy, Codecademy, LeetCode).
  • Previous AP Computer Science A exam papers.

Week 1: March 28th - April 3rd

  • Day 1-2: Introduction to FRQ Type 1
    • Read College Board guidelines for FRQ Type 1.
    • Solve 3 practice FRQs of Type 1.
  • Day 3-4: Introduction to FRQ Type 2
    • Read College Board guidelines for FRQ Type 2.
    • Solve 3 practice FRQs of Type 2.
  • Day 5-6: Introduction to FRQ Type 3
    • Read College Board guidelines for FRQ Type 3.
    • Solve 3 practice FRQs of Type 3.

Week 2: April 4th - April 10th

  • Day 1-2: Introduction to FRQ Type 4
    • Read College Board guidelines for FRQ Type 4.
    • Solve 3 practice FRQs of Type 4.
  • Day 3-4: Review of Object-Oriented Programming (OOP) concepts
    • Focus on classes, objects, inheritance, and polymorphism.
    • Solve coding exercises related to OOP.
  • Day 5-6: Review of Algorithms and Data Structures
    • Focus on sorting algorithms, searching algorithms, and data structures like arrays, linked lists, stacks, and queues.
    • Solve coding exercises related to algorithms and data structures.

Week 3: April 11th - April 17th

  • Day 1-2: Review of Recursion and Dynamic Programming
    • Understand the concept of recursion and dynamic programming.
    • Solve coding exercises involving recursion and dynamic programming.
  • Day 3-4: Practice FRQs of all types
    • Allocate time to solve at least 1 FRQ of each type daily.
    • Review solutions and identify areas for improvement.
  • Day 5-6: Review of Exception Handling and Input/Output
    • Understand exception handling mechanisms in Java.
    • Practice coding exercises involving input/output operations.

Week 4: April 18th - April 24th

  • Day 1-2: Mock Exam Practice
    • Simulate exam conditions and solve a full-length practice exam.
    • Review answers and identify weak areas for further improvement.
  • Day 3-4: Review Weak Areas
    • Focus on topics or question types where you struggled during mock exams or practice sessions.
  • Day 5-6: Final Review and Consolidation
    • Review all key concepts, algorithms, and data structures.
    • Solve miscellaneous practice problems to reinforce learning.

Week 5: April 25th - May 1st

  • Day 1-2: Final Mock Exam Practice
    • Take another full-length practice exam under timed conditions.
    • Analyze your performance and identify any remaining weak areas.
  • Day 3-4: Last-Minute Review
    • Brush up on any remaining concepts or topics.
    • Solve a few targeted practice problems to boost confidence.
  • Day 5-6: Relax and Rest
    • Avoid intensive studying.
    • Get adequate rest and relaxation to ensure a fresh mind for the exam.

Week 6: May 2nd - May 8th

  • Day 1-2: Exam Day Preparation
    • Gather all necessary materials for the exam day (ID, pencils, calculator, etc.).
    • Review exam-day procedures and guidelines.
  • Day 3-4: Final Review of Tips and Strategies
    • Review exam-taking strategies such as time management and question prioritization.
  • Day 5-6: Relax and Confidence Building
    • Engage in activities that help alleviate stress and boost confidence.
    • Trust in your preparation and believe in your abilities.

Conclusion:

This study plan is designed to provide a structured approach to prepare for the AP Computer Science A exam, focusing on FRQ types 1-4. Adjust the plan according to your individual needs and progress. Stay consistent, practice regularly, and stay confident! Good luck with your exam preparation! 🚀