In: Computer Science
WRITE IN JAVA, please include comments and explain what you did
A Book has such properties as title, author, and numberOfPages. A Volume will have properties such as volumeName, numberOfBooks, and an array of book objects (Book [ ]). You are required to develop the Book and Volume classes, then write an application (DemoVolume) to test your classes. The directions below will give assistance.
Create a class called Book with the following properties using appropriate data types: Title, Author, numberOfPages, Create a second class called Volume with the following properties using appropriate data types: volumeName, numberOfBooks and Book [ ]. The Book [ ] contains an array of book objects.
Directions
Create a class called Book with the following properties using appropriate data types: Title, Author, numberOfPages,
The only methods necessary in the Book class, for this exercise, are the constructor and a toString().
Create a second class called Volume with the following properties using appropriate data types: volumeName, numberOfBooks and Book [ ]. Book [ ] will contain an array of book objects.
The only methods necessary in the Volume class, for this exercise, are the constructor, toString() and getBookArray(). The getBookArray returns a string of book properties for each book.
Create an application called DemoVolume.In the main method,
Create an array of book objects to be added to the volume.
Create a volume object called volume1.
Display the properties of volume1.
Grading
Task |
Points |
Book class properties created |
2 |
Volume class created correctly |
1 |
Constructors created correctly |
2 |
"this" reference used effectively |
1 |
Array of book objects created |
2 |
Proper documentation / Pseudocode |
1 |
Program works effectively |
1 |
Total |
10 |
OUTPUT SCREENSHOT :
===========================================================================
CODE:
Book.java:
public class Book { private String title; private String author; private int numberOfPages; //Constructor for Book Object public Book(String title, String author, int numberOfPages) { this.title = title; this.author = author; this.numberOfPages = numberOfPages; } @Override public String toString() { return "Title = '" + title + '\'' + ", Author = '" + author + '\'' + ", Number Of Pages = " + numberOfPages ; } }
==========================================================================
Volume.java:
public class Volume { private String volumeName; private int numberOfBooks; private Book[] books; //Constructor for Volume Object public Volume(String volumeName, Book[] books) { this.volumeName = volumeName; this.numberOfBooks = books.length ; this.books = books; } //Prints the books stored public void getBookArray(){ for (Book b: this.books) { System.out.println(b); } } @Override public String toString(){ return "Name of Volume: " + this.volumeName +"\nNumber of Books: " + this.numberOfBooks + "\n"; } }
============================================================================
DemoVolume.java:
public class DemoVolume { public static void main(String[] args) { //Making books array Book[] books = { new Book("Game of Thrones","George Martin", 1200), new Book("Harry Potter","JK Rowling",1500), new Book("The Alchemist","Paulo Coelho", 350), new Book("50 Shades of Grey","EL James",1400) }; //Making a volume Object and adding books array to it. Volume voulme1 = new Volume("Volume 1", books); //Displaying the property of Books in Volume 1. System.out.println(voulme1); voulme1.getBookArray(); } }
===========================================================================