Java

OOP GUIDE / WORK

 

BOOK CLASS SOLUTIONS

 

 

TASK – PART 1 – PAGE CLASS SETUP

 

No solution required.

 

 

TASK – PART 2 – TESTING THE PAGE CLASS

 

No solution required.

 

 

TASK – PART 3 – BOOK CLASS

 

Below is my solution.

 

public class Book

{

     private String author;

     private String title;

     private Page[] pages;

    

     public Book(String a, String t, String[] content)

     {

           author = a;

           title = t;

          

           pages = new Page[content.length];

          

           for (int i=0; i<pages.length; i++)

           {

                int pageNumber = i + 1;

                if (pageNumber % 2 == 0)

                      pages[i] = new Page(pageNumber, title, content[i]);

                else

                      pages[i] = new Page(pageNumber, author, content[i]);

           }

     }

    

     public Page getPage(int pageNumber)

     {

           return pages[pageNumber-1];

     }

    

     public String toString()

     {

           String book = "";

           for(int i=0; i<pages.length; i++)

           {

                book = book + "---------------------------------\n";

                book = book + pages[i] + "\n";

           }

           book = book + "---------------------------------\n";

           return book;

     }

}

 

 

 

 

TASK – PART 4 – BOOKTESTER CLASS

 

No solution required.