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

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

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

  • Java Programming Exercise 9-7 In the exercises in Chapter 6, you created a class named Purchase....

    Java Programming Exercise 9-7 In the exercises in Chapter 6, you created a class named Purchase. Each Purchase contains an invoice number, amount of sale, amount of sales tax, and several methods. Add get methods for the invoice number and sale amount fields so their values can be used in comparisons. Next, write a program that declares an array of five Purchase objects and prompt a user for their values. Then, in a loop that continues until a user inputs...

  • Write a modularized, menu-driven program to read a file with unknown number of records.

    ==============C++ or java================Write a modularized, menu-driven program to read a file with unknown number of records.Create a class Records to store the following data: first and last name, GPA , an Id number, and an emailInput file has unknown number of records; one record per line in the following order: first and last names, GPA , an Id number, and emailAll fields in the input file are separated by a tab (‘\t’) or a blank space (up to you)No error...

  • Hi I need help doing this problem *The program must re-prompt as long as invalid input...

    Hi I need help doing this problem *The program must re-prompt as long as invalid input is inserted Implement a program that manages shapes. Implement a class named Shape with a virtual function area()which returns the double value. Implement three derived classes named Diamond, Oval, and Pentagon. Declare necessary properties in each including getter and setter function and a constructor that sets the values of these properties. Override the area() function in each by calculating the area using the defined...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

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