Question

1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4...

1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4 digit String) and Price (Double). Write a Java Book class with constructors and set/get methods.

2) Write a Java program that asks user to enter the Book data on the Console. User can enter any number of books until terminated on the Console. Write the book data to a text file called "book.txt" in the current project directory.

3) Write a Java program to read the "books.txt" file and display the book data contained in it on the Console with each field displayed in column, one line per book.

Please only code it exactly how it asks, nothing fancy. thank you

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

Answer- 1

User wants to keep track Books. Each Book has Author (String), Title (String), Year (4 digit String) and Price (Double). Write a Java Book class with constructors and set/get methods.

here we are creating java class named as Book.java which code is given as below:

public class Book {
// these are the four field we required as book data  
private String Author;
private String Title;
private String Year;
private double Price;
// getter setter for the above variables
public String getAuthor() {
   return Author;
}

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

public String getTitle() {
   return Title;
}

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

public String getYear() {
   return Year;
}

public void setYear(String year) {
   Year = year;
}

public double getPrice() {
   return Price;
}

public void setPrice(double price) {
   Price = price;
}
// constructor of Book class to initialize the value of book variables
Book(String Author, String Title, String Year, double Price)
{
   this.Author=Author;
   this.Title=Title;
   this.Year=Year;
   this.Price=Price;
}
}

Answer-2

Write a Java program that asks user to enter the Book data on the Console. User can enter any number of books until terminated on the Console. Write the book data to a text file called "book.txt" in the current project directory.

for this we will write a BookEntry.java class in which we will take input from user and write every content in file (book.txt) and when user enter ctrl-z we will stop taking input from user .

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;

public class BookEntry {

   public static void main(String[] args) throws IOException {

       Reader r = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(r);
       String str = null;

       try {
       //prompt the user to input data
       System.out.println("enter author title year price of each book and press enter then hit ctrl-z");
       PrintWriter writer = new PrintWriter("book.txt", "UTF-8");
       while((str = br.readLine())!=null)
       {
       //save the line

       writer.println(str);
       }
       writer.close();

       } catch (IOException e) {
       e.printStackTrace();
       }
   }
}
output console:

enter author title year price of each book and press enter then hit ctrl-z
maddy bookA 1996 220
muskan bookB 1997 254
data added to text file successfully

Answer 3-

Write a Java program to read the "books.txt" file and display the book data contained in it on the Console with each field displayed in column, one line per book.

now in this we write BookDetails.java file and display the book data from book.txt file to console in column.

package cmsnew;
import java.io.*;
public class BookDetails {

   public static void main(String[] args) throws IOException {
       // TODO Auto-generated method stub
  
String line = null;
System.out.println("the book details are:");
/* FileReader reads text files in the default encoding */
FileReader fileReader = new FileReader("book.txt");
  
/* always wrap the FileReader in BufferedReader */
BufferedReader bufferedReader = new BufferedReader(fileReader);
  
while((line = bufferedReader.readLine()) != null)
{
System.out.println(line);
}
  
/* always close the file after use */
bufferedReader.close();
}
   }

output:

the book details are:
maddy bookA 1996 220
muskan bookB 1997 254

thanks!!

Add a comment
Know the answer?
Add Answer to:
1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4...
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
  • 1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4...

    1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4 digit String) and Price (Double). Write a Java Book class with constructors and set/get methods. 2) Write a Java program that asks user to enter the Book data on the Console. User can enter any number of books until terminated on the Console. Write the book data to a text file called "book.txt" in the current project directory. 3) Write a Java program to...

  • Create a java program that asks the user for their title (String) and their health level...

    Create a java program that asks the user for their title (String) and their health level (int). Display their title and health level with ‘:’ between them. For example if they enter Wizard and 9 then display- Wizard:9 Thank you in advance

  • Write a definition for a class named Book with attributes title, price and author, where author...

    Write a definition for a class named Book with attributes title, price and author, where author is a Contact object and title is a String, and price is a float number. You also need to define the Contact class, which has attributes name and email, where name and email are both String. Instantiate a couple of Book objects that represents books with title, price and author (You can come up with the attributes values for each object) Write a function...

  • please use C++ write a program to read a textfile containing a list of books. each...

    please use C++ write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. each line in the file has tile, ..... write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. Each line in...

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

  • Database Management ID Price 1 Author Richie Karen 10 2 4 Year 1955 1958 1959 Title...

    Database Management ID Price 1 Author Richie Karen 10 2 4 Year 1955 1958 1959 Title с JAVA Script Java C++ prolog Perl 20 10 5 6 Schwartz Ross Maria 1959 1972 20 10 7 Wall 1987 20 The above relation has information about different books. The table information is already saved as txt format in Moodle under the name Quiz2Data.txt. Using MySql on your computer do: Create a database Create a table with name book, then load Quiz2Data.txt (check...

  • Implement a Java application for the following: 1.) Keep track of a movie collection. 2.) Each...

    Implement a Java application for the following: 1.) Keep track of a movie collection. 2.) Each movie in the collection will contain: Title, Genre, Year (4 digits) and Runtime (double - ex. 2.1 (hrs)). 3.) Program will read movies from a local text file named movies.txt in the current project directory in Eclipse. 4.) Each line in the text file contains one movie, containing each field, per line.separated by commas. 5.) Read the text file and load the movies into...

  • Database Management ID Title 1 Author Richie Karen с JAVA Script Java Year 1955 1958 1959...

    Database Management ID Title 1 Author Richie Karen с JAVA Script Java Year 1955 1958 1959 Price 10 20 10 2 4 5 6 C++ prolog Peri Schwartz Ross Maria 1959 1972 20 10 7 Wall 1987 20 The above relation has formation about different books. The table information is already saved as txt format in Moodle under the name Quiz2Data.txt. Using MySql on your computer do: Create a database • Create a table with name book, then load Quiz2Data.txt...

  • PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from...

    PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from tony gaddis starting out with java book. I have included the screenshot of the problems for reference. I need HELP implementing these two problems TOGETHER. There should be TWO class files. One which has TWO methods (one to implement problem 8, and one to implement problem 9). The second class should consist of the MAIN program with the MAIN method which calls those methods....

  • please write this program in C++(Linux). Also write all the explanation of codes, the comment for...

    please write this program in C++(Linux). Also write all the explanation of codes, the comment for your code. Description You are an avid reader and have decided that you would like to keep track of the books that you read. You will define a structure called book_list that will hold 1. a number associated with each book (int) 2. book title (string), 3. author (string), 4. a description of the book (string), 5. a date when the book was read...

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