Please help with this java file, Also Only do what is asked from instructions no
ID: 3697200 • Letter: P
Question
Please help with this java file, Also Only do what is asked from instructions no more no less please I will apply this file to compilation of files Instructions:
Activity 1.
This certainly calls for a new user-defined type: Book. You will implement this new type, with the above attributes, and corresponding methods (constructors, accessors (one per attribute), mutators (one per attribute), and a print method that prints the information in any current Book), in the file Book.java that is provided to you.
The print method for an object whose attributes are as follows:
title: “The Power of Algorithms: Inspiration and Examples in Everyday Life”
authors: {“Giorgio Ausiello”, “RossellaPetreschi”}
pages: 255
date: 20131109
price: 40.08
enjoyed: true
should print the following:
Book: The Power of Algorithms: Inspiration and Examples in Everyday Life, of 255 pages
First published on 11/9/2013
Price: $40.08
I enjoyed it very much!
And if attribute enjoyed had been false, the print method should have printed:
Book: The Power of Algorithms: Inspiration and Examples in Everyday Life, of 255 pages
First published on 11/9/2013
Price: $40.08
This book was not so great…
END OF INSTRUCTIONS
JAVA FILE:
public class Book {
// Attributes... ARE PROVIDED TO YOU:
private String title;
private String[] authors;
private int pages;
private int pubDate;
private double price;
private boolean enjoyed;
// NOW COMPLETE THE FOLLOWING SECTIONS WITH RELEVANT METHODS
// Constructors
// Accessors
// Mutators
// Print method
}
END OF JAVA FILE
Book: The Power of Algorithms: Inspiration and Examples in Everyday Life, of 255 pages
First published on 11/9/2013
Price: $40.08
I enjoyed it very much!
Explanation / Answer
File: Book1.java
import java.util.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
//Class definition
public class Book
{
//Private instance Variables
private String title;
private String[] authors;
private int pages;
private int pubDate;
private double price;
private boolean enjoyed;
//Default Constructor
public Book()
{
title = "";
authors = new String[1];
authors[0] = "";
pages = 0;
pubDate = 0;
price = 0.0;
enjoyed = true;
}
//Parameterized Constructor
public Book(String title, String[] authorNames, int pages, int pubDate, double price, boolean enjoyed)
{
this.title = title;
//Setting authors
authors = new String[authorNames.length];
for(int i=0; i<authorNames.length; i++)
authors[i] = authorNames[i];
this.pages = pages;
this.pubDate = pubDate;
this.price = price;
this.enjoyed = enjoyed;
}
// Accessor Methods
//Method that returns title
public String getTitle()
{
return this.title;
}
//Method that return author names as an array
public String[] getAuthors()
{
return this.authors;
}
//Method that returns number of pages
public int getPages()
{
return this.pages;
}
//Method that returns publication date
public Date getPubDate()
{
int date = this.pubDate;
String strDate = Integer.toString(date);
Date currentDate = new Date();
try
{
//Format the date object
currentDate = new SimpleDateFormat("yyyymmdd").parse(strDate);
}
catch(Exception e)
{
System.out.println("Error:: " + e);
e.printStackTrace();
}
return currentDate;
}
//Method that returns price
public double getPrice()
{
return this.price;
}
//Method that returns enjoyed status
public boolean getEnjoyed()
{
return this.enjoyed;
}
// Mutators
//Method that sets book title
public void setTitle(String title)
{
this.title = title;
}
//Method that sets book authors
public void setAuthors(String[] authorNames)
{
for(int i=0; i<authorNames.length; i++)
authors[i] = authorNames[i];
}
//Method that sets number of pages
public void setPages(int pages)
{
this.pages = pages;
}
//Method that sets book publication date
public void setPubDate(int pubDate)
{
this.pubDate = pubDate;
}
//Method that sets book price
public void setPrice(double price)
{
this.price = price;
}
//Method that sets enjoyment status
public void setEnjoyed(boolean enjoyed)
{
this.enjoyed = enjoyed;
}
// Print method that prints all the attributes of Book class
public void print()
{
DateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy");
System.out.println(" Book: " + title + ", of " + pages + " pages. ");
System.out.println(" First published on " + dateFormat.format(getPubDate()));
System.out.println(" Price: $" + price);
if(enjoyed)
System.out.println(" I enjoyed it very much! ");
else
System.out.println(" This book was not so great ");
}
}