Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part1: Write a class called Book.java that has attributes named category, title,

ID: 3619726 • Letter: P

Question

Part1:
Write a class called Book.java that has attributes named category, title, author, and price. Write get
and set methods for each attribute and also a toString() method that uses the attributes. Write some
code inside main() method so that when it is run it displays in the console as follows:
Category: Computer Science
Title: Java programming
Author: Micheal Owens
Price: $15.00
You must use toString() method in displaying data as shown above.


ok so i have this part done. Heres what it looks like.


public class Book {

public String Category, Title, Author;
public float Price;

public void setCategory (String c){
Category = c;
}

public void setTitle (String t){
Title = t;
}

public void setAuthor (String a){
Author = a;
}

public void setPrice (float p){
Price = p;
}

public String getCategory(){
return Category;
}

public String getTitle(){
return Title;
}

public String getAuthor(){
return Author;
}

public float getPrice(){
return Price;
}

public String toString(){
return String.format("Category: %s Title: %s Author: %s Price: $%4.2f " , getCategory(), getTitle(), getAuthor(), getPrice());
}


public static void main (String args[]){

Book BookObject = new Book();

BookObject.setCategory("Action");
BookObject.setTitle("King");
BookObject.setAuthor("Kobe Richards");
BookObject.setPrice(18.99f);

System.out.printf("%s ", BookObject);
}
}

PART2:
Write a class called BookCatalog.java that has an ArrayList of Book class.
The class must have the following methods.
A method that takes a parameter of Book type and add the book to the ArrayList of Book.
A method that takes one parameter for title and another parameter for author and returns true or
false based on whether the corresponding book exists or not.
Write a method that takes two parameters for title and author and returns the corresponding Book
object if it exists. You must use the previous method to check whether the book exists or not.
Write a method that takes two parameters for title and author and delete the corresponding book if
it exists.
***This Where im Having Trouble***Write a toString() method and call it in the main() method so that it displays as follows.
Category: Computer Science
Title: Java programming
Author: Micheal Owens
Price: $15.00
If there are more Book objects in the ArrayList of Book type all should be displayed one after another.
Add at least two Book objects to the ArrayList of Book type in the main() method.

This What I have so far.

import java.util.*;

public class BookCatalog {
ArrayList books;

public BookCatalog(){
books = new ArrayList();
}

void add(Book x) {
books.add(x);
}

boolean Book (String Title, String Author) {
for(Book x: books)
if (x.Title == Title && x.Author == Author) {
return true;
}
return false;
}

Book getTitleAuthor(String Title, String Author) {
for(Book x: books)
if (x.Title == Title && x.Author == Author) {
return x;
}
return null;
}

void remove(String Title, String Author) {
for(Book x: books)
if (x.Title == Title && x.Author == Author) {
books.remove(x);
}
}

public String toString(){
String a = null;
for(Book B: books)
a= a + B.toString();
return a;
}

public static void main (String args[]){

BookCatalog BookCatalogObject = new BookCatalog();

BookCatalogObject.setCategory("Action");
BookCatalogObject.setTitle("CapintUnderpants");
BookCatalogObject.setAuthor("Joseph Who");
BookCatalogObject.setPrice(20.99f);

System.out.printf("%s ", BookCatalogObject);
}

}


Explanation / Answer

//Book class import java.util.*; public class Book { public String Category, Title, Author; public float Price; public void setCategory (String c){ Category = c; } public void setTitle (String t){ Title = t; } public void setAuthor (String a){ Author = a; } public void setPrice (float p){ Price = p; } public String getCategory(){ return Category; } public String getTitle(){ return Title; } public String getAuthor(){ return Author; } public float getPrice(){ return Price; } public String toString(){ return String.format("Category: %s Title: %s Author: %s Price: $%4.2f " , getCategory(), getTitle(), getAuthor(), getPrice()); } } //BookCatalog public class BookCatalog { ArrayList books; public BookCatalog(){ books = new ArrayList(); } void add(Book x) { books.add(x); } //Return true if the book object existed in book list boolean Book (String Title, String Author) { //Iterating through the books list for(Book x: books) //Check whether the book exists or not if (x != null && x.Title.equals(Title) && x.Author.equals(Author)) { return true; } return false; } //Return book object based on Title and author if the book object existed in book list Book getTitleAuthor(String Title, String Author) { //Previous method book return true if the book exists with this title and author if(Book(Title,Author)) { //If book existed then iterate books list for the corresponding book for(Book x: books) if (x != null && x.Title.equals(Title) && x.Author.equals(Author)) { return x; } } return null; } void remove(String Title, String Author) { //Iterating through the books list for(Book x: books) ////Check whether the book exists or not if (x != null && x.Title.equals(Title) && x.Author.equals(Author)) { books.remove(x); return; } } public String toString(){ String a = null; for(Book B: books) a= a + B.toString(); return a; } } ///Main class public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Book b1 = new Book(); b1.setAuthor("Micheal Owens"); b1.setCategory("Computer Science"); b1.setPrice(100.12F); b1.setTitle("Java Programming"); Book b2 = new Book(); b2.setAuthor("Micheal Dwins"); b2.setCategory("Computer Science"); b2.setPrice(220.12F); b2.setTitle("JSF Init"); Book b3 = new Book(); b3.setAuthor("Kanethkar"); b3.setCategory("Computer Science"); b3.setPrice(320.12F); b3.setTitle("Let US C"); BookCatalog bookCatalog = new BookCatalog(); bookCatalog.add(b1); bookCatalog.add(b2); bookCatalog.add(b3); //Display all books System.out.println(bookCatalog); //Removing book bookCatalog.remove(b1.Title, b1.Author); //Display after removing book System.out.println(bookCatalog); //Display specific book System.out.println(bookCatalog.getTitleAuthor(b2.Title,b2.Author)); } }