Question

FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named...

FOR JAVA:

Summary: Create a program that stores info on textbooks.

  • The solution should be named TextBookSort.java.
  • Include these steps:
    • Create a class titled TextBook that contains fields for the author, title, page count, ISBN, and price.
    • This TextBook class will also provide setter and getter methods for all fields. Save this class in a file titled TextBook.java.
    • Create a class titled TextBookSort with an array that holds 5 instances of the TextBook class, filled without prompting the user for input.
    • Next prompt the used to enter a field for sorting ( that is.... do they want to sort by ISBN? author name? etc.), sort the array of objects based on the user input, and then display the newly sorted array of objects.
    • Save this class in a file titled TextBookSort.java.
  • Run your program until it works and the output looks nice.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

TextBook.java:


public class TextBook {
   private String author;
   private String title;
   private int pagecount;
   private String ISBN;
   private double price;
   //getters and setters
   public String getAuthor() {
       return author;
   }
   public void setAuthor(String author) {
       this.author = author;
   }
   public String getTitle() {
       return title;
   }
   public void setTitle(String title) {
       this.title = title;
   }
   public int getPagecount() {
       return pagecount;
   }
   public void setPagecount(int pagecount) {
       this.pagecount = pagecount;
   }
   public String getISBN() {
       return ISBN;
   }
   public void setISBN(String iSBN) {
       ISBN = iSBN;
   }
   public double getPrice() {
       return price;
   }
   public void setPrice(double price) {
       this.price = price;
   }
   //constructor
   public TextBook(String author, String title, int pagecount, String iSBN,
           double price) {
       super();
       this.author = author;
       this.title = title;
       this.pagecount = pagecount;
       ISBN = iSBN;
       this.price = price;
   }
   //toString() method to print the object values
   @Override
   public String toString() {
       return "Author: " + author + "\nTitle: " + title
               + "\nPagecount: " + pagecount + "\nISBN: " + ISBN + "\nPrice: "
               + price+"\n" ;
   }   

}

TextBookSort.java

import java.util.*;
public class TextBookSort {
   public static void main(String[] args)
   {

//Scanner ocject to read input
       Scanner input=new Scanner(System.in);
      
       TextBook[] bookList=new TextBook[5];

//adding books to bookList array
       bookList[0]=new TextBook("abc","hello world",123,"ABc123",123.4);
       bookList[1]=new TextBook("bcd","abdfd",90,"hfhBcqw23",121);
       bookList[2]=new TextBook("def","ekrejk",13,"dgd123",909);
       bookList[3]=new TextBook("kmo","wewe",993,"sdsi3",3434);
       bookList[4]=new TextBook("zui","grgr",1003,"eyuwew23",1201);
       System.out.println("Select field to sort Array \n1.sort by Title\n2.sort by ISBN\n3.sort by Price\n4.sort by Page count\n5.sort by Author");
       int choice=input.nextInt(); //taking user choice
       if(choice==1)
       {
           //lamba expression to sort array based on title of the book
           Arrays.sort(bookList, (tb1, tb2) -> tb1.getTitle().compareTo(tb2.getTitle()));
       }
       else if(choice==2)
       {
           //lamba expression to sort array based on ISBN of the book
           Arrays.sort(bookList, (tb1, tb2) -> tb1.getISBN().compareTo(tb2.getISBN()));
       }
       else if(choice==3)
       {
           //lamba expression to sort array based on Price of the book
           Arrays.sort(bookList, (tb1, tb2) -> Double.compare(tb1.getPrice(), tb2.getPrice()));
       }
       else if(choice==4)
       {
           //lamba expression to sort array based on Pagecount of the book
           Arrays.sort(bookList, (tb1, tb2) -> tb1.getPagecount()-tb2.getPagecount());
       }
       else if(choice==5)
       {
           //lamba expression to sort array based on Author of the book
           Arrays.sort(bookList, (tb1, tb2) -> tb1.getAuthor().compareTo(tb2.getAuthor()));
       }
       else//If invalid choice is entered
       {
           System.out.println("Invalid Choice Found!");
       }
       System.out.println("Array after sorting");
       for(int i=0;i<5;i++) //Printing the array of objects after sorting
       {
           System.out.println(bookList[i]);

       }
          
              
   }

}

OUTPUT:

Please rate my answer if you liked it.

Add a comment
Know the answer?
Add Answer to:
FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named...
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
  • C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header ...

    C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header files given. Then make a main program using Book and Warehouse to read data from book.dat and have functions to list and find book by isbn Objectives: Class Association and operator overloading This project is a continuation from Project 1. The program should accept the same input data file and support the same list and find operations. You will change the implementation of...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • In JAVA Create a program with an array with the following data: 50 12 31 76...

    In JAVA Create a program with an array with the following data: 50 12 31 76 5 23 15 Use a text file (.txt created in notepad) to read the data into the array. Write a sort method (your choice of sorting algorithm) to sort the array values values - highest to lowest. Write a method to determine the minimum value. Your program should call the sort and minimum methods. Output the sorted array and minimum value.  

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Create a JAVA program with an array with the following data: 50 12 31 76 5...

    Create a JAVA program with an array with the following data: 50 12 31 76 5 23 15 Use a text file (.txt created in notepad) to read the data into the array. Write a sort method (your choice of sorting algorithm) to sort the array values values - FROM HIGHEST TO LOWEST. Write a method to determine the minimum value. Your program should call the sort and minimum methods. Output the sorted array and minimum value.

  • In Java For the following questions, “define a class” means the following: • Create an appropriately-named...

    In Java For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

  • FOR JAVA: Summary: Write a program to assess password stringency. The solution should be named Password.java....

    FOR JAVA: Summary: Write a program to assess password stringency. The solution should be named Password.java. Include these steps: Write an application that provides the criteria for a strong password, and accepts a user's password from an input dialog box. If the entered password is less than six characters, more than 10 characters, or does not contain at least one uppercase letter and one digit, prompt the user to enter again. When the user's entry meets all the password requirements,...

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • VISUAL BASIC- create a program for managing a "To Do" list. The program will need to...

    VISUAL BASIC- create a program for managing a "To Do" list. The program will need to read and update a text file named ToDoList.txt. The record structure of the file must be: 1) sort order, 2) task name and 3) desired completion date. When the program starts, it should read the contents file into a structure. The information should then be displayed in a data grid view. The data should be initially sorted by the sort order value. Afterwards, the...

  • IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named...

    IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

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