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
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!!
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...
Java Program: Design a class “Book” with the data attributes title - String, author - String, yearPublished - integer and price - double. Write a parameterized constructor that initializes the attributes. Write accessor and mutator methods, and a print method that prints all of the attributes. In the main method, create a single object and give it values of your choice. Call the print method to print the values. Sample Run: The Book is: The Art of Computer Programming Donald...
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 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 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 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)...
Write a class called Book. Here are the relevant attributes: title author yearPublished bookPriceInCAD Provide a constructor that takes parameters to initialize all the instance variables. The constructor calls the mutator (set) methods to initialize the instance variables. Provide an accessor (get) and mutator (set) for each instance variable. The mutators all validate their parameters appropriately and use them only if valid. If the passed parameter was invalid an IllegalArgumentException will be thrown with a proper error message A...
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...
Script 1: Sum of Numbers Write a python program that asks the user to enter a series of single-digit numbers with nothing separating them. The program should display the sum of all the single digit numbers in the string. For example, if the user enters 2514, the method should return 12, which is the sum of 2, 5, 1, and 4.
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...