Question

Can someone please help me add appropriate descriptive comments to each line of code in the...

Can someone please help me add appropriate descriptive comments to each line of code in the project explaining why the code is in the application.

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class BookManager {
private List<Book> bookList;
  
public BookManager() {
bookList = new BookCatalog().getCatalog();
}
  
public List<Book> getBooks(Predicate<Book> condition) {
List<Book> books = new ArrayList<>();
for (Book b: bookList) {
if (condition.test(b)) {
books.add(b);
}
}
return books;
}
}

import java.util.List;

public class BookManagerApp {

public static void main(String[] args) {
BookManager manager = new BookManager();
  
List<Book> booksByTitle = manager.getBooks(
b -> b.getTitle().contains("Java"));
System.out.println("\nBOOKS BY TITLE:");
printList(booksByTitle);
  
List<Book> booksByCategory = manager.getBooks(
b -> b.getCategory().equals(Book.JAVA));
System.out.println("\nBOOKS BY CATEGORY:");
printList(booksByCategory);
  
List<Book> booksByFormat = manager.getBooks(
b -> b.getFormat().equals(Book.PAPERBACK));
System.out.println("\nBOOKS BY FORMAT:");
printList(booksByFormat);
}
  
public static void printList(List<Book> bookList) {
for (Book b : bookList) {
System.out.println(b);
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class BookManager {
// create a list to store the Book objects
private List<Book> bookList;
// In constructor creating the list of books by calling the getCatalog
public BookManager() {
// getting the list of books from the catalog
bookList = new BookCatalog().getCatalog();
}
  
// return the list of all books as List
public List<Book> getBooks(Predicate<Book> condition) {
List<Book> books = new ArrayList<>();
//iterating the List and checking the condition
for (Book b: bookList) {
// if condition is met than add it to result
if (condition.test(b)) {
   //adding to another list
books.add(b);
}
}
//returning the list
return books;
}
}

import java.util.List;

public class BookManagerApp {

public static void main(String[] args) {
BookManager manager = new BookManager();

// here we are filter the books using the contains function so now
// it will hold the books which are related to java
List<Book> booksByTitle = manager.getBooks(
b -> b.getTitle().contains("Java"));
System.out.println("\nBOOKS BY TITLE:");
printList(booksByTitle);
// here we are filter the books using the contains function so now
// it will hold the books which are related to java category
List<Book> booksByCategory = manager.getBooks(
b -> b.getCategory().equals(Book.JAVA));
System.out.println("\nBOOKS BY CATEGORY:");
printList(booksByCategory);

// here we are filter the books using the contains function so now
// it will hold the books which are related to same format
List<Book> booksByFormat = manager.getBooks(
b -> b.getFormat().equals(Book.PAPERBACK));
System.out.println("\nBOOKS BY FORMAT:");
printList(booksByFormat);
}
  
public static void printList(List<Book> bookList) {
// iterating the list and printing the each book
for (Book b : bookList) {
System.out.println(b);
}
}
}

Add a comment
Know the answer?
Add Answer to:
Can someone please help me add appropriate descriptive comments to each line of code in the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Add appropriate descriptive comments to each line of the code, explaining why the code is in...

    Add appropriate descriptive comments to each line of the code, explaining why the code is in the application. import java.text.NumberFormat; public class LineItem implements Cloneable { private Product product; private int quantity; private double total; public LineItem() { this.product = new Product(); this.quantity = 0; this.total = 0; } public LineItem(Product product, int quantity) { this.product = product; this.quantity = quantity; } public void setProduct(Product product) { this.product = product; } public Product getProduct() { return product; } public void...

  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException;...

    how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class checkFruit { private List<String> tests; public List<String> getFruit(String fruits) { tests = new ArrayList<String>(Arrays.asList(fruits.split(","))); for (String test : tests) { System.out.println(test); tests.add()//how would i use add here } return tests; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter file name: "); File file = new File(in.nextLine()); try { checkFruit...

  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

  • (JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private...

    (JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private String author; private double price; /** * default constructor */ public Book() { title = ""; author = ""; price = 0.0; } /** * overloaded constructor * * @param newTitle the value to assign to title * @param newAuthor the value to assign to author * @param newPrice the value to assign to price */ public Book(String newTitle, String newAuthor, double newPrice)...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • Can someone please explain this piece of java code line by line as to what they...

    Can someone please explain this piece of java code line by line as to what they are doing and the purpose of each line (you can put it in the code comments). Code: import java.util.*; public class Array { private Integer[] array; // NOTE: Integer is an Object. Array() {     super();     array = new Integer[0]; } Array(Array other) {     super();     array = other.array.clone(); // NOTE: All arrays can be cloned. } void add(int value) {    ...

  • /** * */ package groceries; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ShoppingList {   ...

    /** * */ package groceries; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ShoppingList {    /**    * @param args    */    public static void main(String[] args) {        List groceryList = new ArrayList<>();        groceryList.add("rice");        groceryList.add("chicken");        groceryList.add("cumin");        groceryList.add("tomato");        groceryList.add("cilantro");        groceryList.add("lime juice");        groceryList.add("peppers");               groceryList.remove("cilantro");               for (int i = 0; i            if (groceryList.get(i).equals("peppers")) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT