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 in the project explaining why the...

    Add appropriate descriptive comments to EACH line of the code in the project explaining why the code is in the application. import java.io.*; public class ExceptionTesterApp { public static void main(String[] args) { System.err.println("In main: calling method1."); method1(); System.err.println("In main: returned from method1."); } public static void method1() { System.err.println("\tIn method1: calling method2."); try { method2(); } catch (FileNotFoundException e) { System.err.println(e.toString()); } System.err.println("\tIn method1: returned from method2."); } public static void method2() throws FileNotFoundException { System.err.println("\t\tIn method2: calling method3.");...

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

  • JAVA - Given two List objects (input and output), write a generic method that 1) clears...

    JAVA - Given two List objects (input and output), write a generic method that 1) clears output list if it contains any element, and 2) copies every element of input list to the output list in the same order and position. Note that the type of output list can be any super type of the input list (e.g. type(output)=Numeric & type(input)=Double). import java.util.List; import java.util.ArrayList; public class Collections { // Modify method signature if needed & implement the body of...

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

  • Add appropriate descriptive comments to each line of code in the project explaining why the code...

    Add appropriate descriptive comments to each line of code in the project explaining why the code is in the application. public class FutureValueFrame extends JFrame { private JTextField investmentField; private JTextField interestRateField; private JComboBox yearsComboBox; private JList futureValueList; private DefaultListModel futureValueModel; public FutureValueFrame() { initComponents(); } private void initComponents() { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { System.out.println(e); } setTitle("Future Value Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationByPlatform(true); investmentField = new JTextField(); interestRateField = new JTextField();...

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

  • How do i rewrite this code only using the list below? import java.util.ArrayList; import java.util.Collection; import...

    How do i rewrite this code only using the list below? import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Set; import java.util.HashSet; public class RotationCheck {    /**    * Rotates the elements in the specified list by the specified distance. After calling this    * method, the element at index {@code i} will be the element previously at index    * {@code (i - distance) % list.size()}, for all values of {@code i} between {@code 0} and    * {@code...

  • Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random;...

    Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class RQS { public static int[] prepareData(int[] patients){ /* Data is prepared by inserting random values between 1 and 1000. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ // what is our range? int max = 1000; // create instance of Random class Random randomNum...

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