AP Pop Test
Mock AP Exam
import java.util.ArrayList;
import java.util.UUID;
public class Library {
private String id;
private ArrayList<Book> books;
static class Book {
private String id;
private String title;
public Book(String title) {
this.id = generateBookId();
this.title = title;
}
public String getId() {
return id;
}
public String getTitle() {
return title;
}
@Override
public String toString() {
return "Book ID: " + id + ", Title: " + title;
}
}
// 1. Define 1 argument constructor for Library
public Library(String id) {
this.id = id;
this.books = new ArrayList<>();
}
// 2. Define toString method for Library and a tester method
@Override
public String toString() {
return "Library ID: " + id + ", Book Count: " + getBookCount();
}
// 3. Generate unique id for class and for each book object
private static String generateLibraryId() {
long timestamp = System.currentTimeMillis();
int random = (int) (Math.random() * 1000);
return "LibraryID_" + timestamp + "_" + random;
}
private static String generateBookId() {
long timestamp = System.currentTimeMillis();
int random = (int) (Math.random() * 1000);
return "BookID_" + random + "_" + timestamp;
}
// 4. Create a public getter that has a Book Count
public int getBookCount() {
return books.size();
}
// 5. Define tester method that initializes at least 2 books, outputs title,
// and provides a count of books in library
public void testLibrary() {
Book book1 = new Book("Hamlet");
Book book2 = new Book("Java For Dummies");
Book book3 = new Book("AP CSA Practice Exams");
books.add(book1);
books.add(book2);
books.add(book3);
System.out.println("Book Count in Library: " + getBookCount());
System.out.println("\nTitle of Book 1: " + book1.getTitle() + "\nBook1 ID: " + book1.getId());
System.out.println("\nTitle of Book 2: " + book2.getTitle() + "\nBook2 ID: " + book2.getId());
System.out.println("\nTitle of Book 3: " + book3.getTitle() + "\nBook3 ID: " + book3.getId());
}
public static void main(String[] args) {
Library library = new Library(generateLibraryId());
library.testLibrary();
}
}
Library.main(null);
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
class Book {
private String title;
private String genre;
public LocalDateTime datePublished;
public int checkedOutCount;
public LocalDateTime shelfLifeExpiration;
public Book(String title, String genre, LocalDateTime datePublished) {
this.title = title;
this.genre = genre;
this.datePublished = datePublished;
this.checkedOutCount = 0;
this.shelfLifeExpiration = datePublished;
}
public void incrementCheckedOutCount() {
checkedOutCount++;
}
public long getShelfLife() {
LocalDateTime currentTime = LocalDateTime.now();
long shelfLife = ChronoUnit.DAYS.between(datePublished, currentTime);
return shelfLife;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
class Novel extends Book {
private String author;
private int returnStamps;
public Novel(String title, String genre, LocalDateTime datePublished, String author) {
super(title, genre, datePublished);
this.author = author;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public void addReturnStamp() {
returnStamps++;
// Renew shelf life for an extra year after 3 return stamps
if (returnStamps >= 3) {
shelfLifeExpiration = shelfLifeExpiration.plusYears(1);
returnStamps = 0;
}
}
}
class Textbook extends Book {
private String publishingCompany;
public Textbook(String title, String genre, LocalDateTime datePublished, String publishingCompany) {
super(title, genre, datePublished);
this.publishingCompany = publishingCompany;
}
public String getPublishingCompany() {
return publishingCompany;
}
public void setPublishingCompany(String publishingCompany) {
this.publishingCompany = publishingCompany;
}
public void setShelfLifeExpiration(LocalDateTime shelfLifeExpiration) {
this.shelfLifeExpiration = shelfLifeExpiration;
}
}
public class BookTester {
public static void main(String[] args) {
// Test the implementation
LocalDateTime novelDatePublished = LocalDateTime.of(2000, 1, 1, 0, 0);
Novel novel = new Novel("The Catcher in the Rye", "Fiction", novelDatePublished, "J.D. Salinger");
LocalDateTime novel1DatePublished = LocalDateTime.of(2005, 1, 1, 0, 0);
Novel novel1 = new Novel("The Tragedy of Hamlet, Prince of Denmark", "Fiction", novel1DatePublished, "William Shakespeare");
LocalDateTime textbookDatePublished = LocalDateTime.of(2010, 1, 1, 0, 0);
Textbook textbook = new Textbook("Fundamentals of Physics, Resnick Halliday", "Education", textbookDatePublished, "John Wiley & Sons, Inc.");
LocalDateTime textbookShelfLifeExpiration = textbookDatePublished.plusYears(3);
textbook.setShelfLifeExpiration(textbookShelfLifeExpiration);
LocalDateTime textbook1DatePublished = LocalDateTime.of(2015, 1, 1, 0, 0);
Textbook textbook1 = new Textbook("Advanced Placement United States Government & Politics", "Education", textbook1DatePublished, "Amsco Advanced Placement");
System.out.println("Novel Shelf Life: " + novel.getShelfLife() + " days");
System.out.println("Textbook Shelf Life: " + textbook.getShelfLife() + " days");
System.out.println("Second Novel Shelf Life: " + novel1.getShelfLife() + " days");
System.out.println("Second Textbook Shelf Life: " + textbook1.getShelfLife() + " days\n");
novel.incrementCheckedOutCount();
novel1.incrementCheckedOutCount();
textbook.incrementCheckedOutCount();
textbook1.incrementCheckedOutCount();
System.out.println("Novel Checked Out Count: " + novel.checkedOutCount);
System.out.println("Second Novel Checked Out Count: " + novel1.checkedOutCount);
System.out.println("Textbook Checked Out Count: " + textbook.checkedOutCount);
System.out.println("Second Textbook Checked Out Count: " + textbook1.checkedOutCount + "\n");
System.out.println("Shelf Life Expiration for the textbook: " + textbookShelfLifeExpiration);
}
}
BookTester.main(null);
/**
* 1. Ensure Novel and Textbook run the Constructor from Book
* 2. Create instance variables unique to Novel Has Author, Textbook has publishing company
* 3. Define a default method in Book that returns shelfLife from date/time of constructor. Define shelfLife methods as needed in Textbook and Novel
* 4. Make a method to count time book is checked out
* 5. Make a method to determine if book should come off of the shelf
* 6. Define tester method to test items #1 - #5
*/
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
// Book class
abstract class Book {
private String title;
private LocalDateTime constructionDateTime;
public Book(String title) {
this.title = title;
this.constructionDateTime = LocalDateTime.now();
}
// Default method to get shelf life based on construction date/time
public LocalDateTime getShelfLifeExpiration() {
return this.constructionDateTime.plus(3_000_000_000L, ChronoUnit.NANOS);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
// TextBook class extending Book
class TextBook extends Book {
private static final int SHELF_LIFE_YEARS = 3;
private static final int SHELF_LIFE_NANOSECONDS = 3000;
public TextBook(String title) {
super(title);
}
// Override method to get shelf life expiration based on construction date/time
@Override
public LocalDateTime getShelfLifeExpiration() {
return super.getShelfLifeExpiration().plus(SHELF_LIFE_YEARS, ChronoUnit.YEARS);
}
}
// Novel class extending Book
class Novel extends Book {
private static final int SHELF_LIFE_RENEWAL_STAMPS = 3;
private int renewalStamps;
public Novel(String title) {
super(title);
this.renewalStamps = 0;
}
// Method to increment renewal stamps
public void addRenewalStamp() {
this.renewalStamps++;
}
// Override method to get shelf life expiration based on renewal stamps
@Override
public LocalDateTime getShelfLifeExpiration() {
int additionalYears = this.renewalStamps / SHELF_LIFE_RENEWAL_STAMPS;
return super.getShelfLifeExpiration().plus(additionalYears, ChronoUnit.NANOS);
}
}
// Library class
class Library {
private List<Book> books;
public Library() {
this.books = new ArrayList<>();
}
// Method to add book to library
public void addBook(Book book) {
this.books.add(book);
}
// Method to check if books need to be taken off the shelf
public void checkBookStatus() throws InterruptedException {
for (Book book : books) {
LocalDateTime now = LocalDateTime.now();
if (now.isAfter(book.getShelfLifeExpiration())) {
System.out.println("Book Title: " + book.getTitle() + " - Status: Off Shelf");
} else {
System.out.println("Book Title: " + book.getTitle() + " - Status: On Shelf");
}
}
}
}
public class BookSimulation {
public static void main(String[] args) throws InterruptedException {
// Create TextBook and Novel objects
TextBook textBook = new TextBook("Textbook 1");
Novel novel = new Novel("Novel 1");
// Add books to library
Library library = new Library();
library.addBook(textBook);
library.addBook(novel);
// Perform simulation with sleep in Java
for (int i = 1; i <= 6; i++) {
System.out.println("Simulation Year: " + i);
library.checkBookStatus();
Thread.sleep(1000); // Sleep for 1 second
if (i == 2) {
novel.addRenewalStamp(); // Add 1 renewal stamp in year 2 of simulation
} else if (i == 3) {
novel.addRenewalStamp(); // Add 2nd renewal stamp in year 3 of simulation
} else if (i == 4) {
novel.addRenewalStamp(); // Add 3rd renewal stamp in year 4 of simulation
}
}
}
}
BookSimulation.main(null);