Activity 1. This certainly calls for a new user-defined type: Book. You will imp
ID: 3696876 • Letter: A
Question
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”, “Rossella Petreschi”}
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…
Pseudocode!
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
Book.java
import java.util.ArrayList;
import java.util.Date;
public class Book {
private int pages;
private double price;
private boolean enjoyed;
private ArrayList<String> authors;
private String title;
private Date date;
// Default constructor
public Book() {
}
/**
* @param pages
* @param price
* @param enjoyed
* @param authors
* @param title
* @param date
*/
public Book( String title,ArrayList<String> authors,int pages,String date, double price, boolean enjoyed) {
super();
this.pages = pages;
this.price = price;
this.enjoyed = enjoyed;
this.authors = authors;
this.title = title;
// getting year, month and day from string
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(4, 6));
int day = Integer.parseInt(date.substring(6));
this.date = new Date(year, month, day);
}
/**
* @return the pages
*/
public int getPages() {
return pages;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @return the enjoyed
*/
public boolean isEnjoyed() {
return enjoyed;
}
/**
* @return the authors
*/
public ArrayList<String> getAuthors() {
return authors;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @return the date
*/
public Date getDate() {
return date;
}
/**
* @param pages the pages to set
*/
public void setPages(int pages) {
this.pages = pages;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @param enjoyed the enjoyed to set
*/
public void setEnjoyed(boolean enjoyed) {
this.enjoyed = enjoyed;
}
/**
* @param authors the authors to set
*/
public void setAuthors(ArrayList<String> authors) {
this.authors = authors;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @param date the date to set
*/
public void setDate(Date date) {
this.date = date;
}
// print method
public void print(){
System.out.println("Book: "+title+", of "+pages+" pages");
System.out.print("authors: {");
for(int i=0; i<authors.size(); i++){
System.out.print("""+authors.get(i)+""");
if(i != authors.size()-1)
System.out.print(", ");
}
System.out.println(" }");
System.out.println("First published on "+ date.getMonth()+"/"+date.getDate()+"/"+date.getYear());
System.out.println("Price: $"+String.format("%.2f", price));
if(enjoyed)
System.out.println("I enjoyed it very much!");
else
System.out.println("This book was not so great…");
}
}
############## BookDriver.java #######################
import java.util.ArrayList;
public class BookDriver {
public static void main(String[] args) {
ArrayList<String> authors = new ArrayList<String>();
authors.add("Giorgio Ausiello");
authors.add("Rossella Petreschi");
Book b1 = new Book("The Power of Algorithms: Inspiration and Examples in Everyday Life",
authors, 255, "20131109",40.08, true);
b1.print();
}
}
/*
Output:
Book: The Power of Algorithms: Inspiration and Examples in Everyday Life, of 255 pages
authors: {"Giorgio Ausiello", "Rossella Petreschi" }
First published on 11/9/2013
Price: $40.08
I enjoyed it very much!
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.