Java

OOP GUIDE / WORK

 

BOOK CLASS

 

Topics

  • Object array as instance variable (datafield)
  • Processing object arrays

 

 

TASK – PART 1 – PAGE CLASS SETUP

 

a)    Copy and paste the Page class below into your IDE.
 

b)    Analyze the code to understand how this class works.  Note that we could have added get and set methods but chose to keep things simple.

 

public class Page

{

     private int pageNumber;

     private String header;

     private String content;

    

     public Page(int pn, String h, String c)

     {

           pageNumber = pn;

           header = h;

           content = c;

     }

    

     public String toString()

     {

           return header + " " + pageNumber + "\n\n" + content;

     }

}

 

 

 

 

TASK – PART 2 – TESTING THE PAGE CLASS

 

Copy and paste the following PageTester class and test the Page class to see how it works.

 

public class PageTester

{

     public static void main(String[] args)

     {

           String content = "The Crocodiles (by Lewis Carroll)\n"

                             + "\n"

                             + "How doth the little crocodile\n"

                             + "Improve his shining tail,\n"

                             + "And pour the waters of the Nile\n"

                             + "On every golden scale!\n"

                             + "How cheerfully he seems to grin,\n"

                             + "How neatly spreads his claws,\n"

                             + "And welcomes little fishes in,\n"

                             + "With gently smiling jaws!";

               

           Page p = new Page(7, "POEMS", content);

           System.out.println(p);

     }

}

 

 

TASK – PART 3 – BOOK CLASS

 

Copy and paste the following Book class and complete it.

 

public class Book

{

     private String author;

     private String title;

     private Page[] pages;

    

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

     {

         //Complete this constructor.

 

         //The challenge is to create the Page array named pages.

         //Each element in the content array represents the content

         //of one of the pages.

 

         //Pages in the book start at page number 1.

         //The header of odd pages is the author.  The header of even

         //even pages is the title.

     }

    

 

     public Page getPage(int pageNumber)

     {

           //Complete this get method.  Remember that the 1st element has

           //a page number 1.

     }

    

     public String toString()

     {

          //Complete this method.  It will return a String containing

          //all of the pages (header, page number and content) in order.

          //Include a line of dashes to separate each page outputted. 

          //Make use of \n to force next lines where needed.

     }

}

 

 

 

TASK – PART 4 – BOOKTESTER CLASS

 

Copy and paste this class in order to test your Book class code.

 

public class BookTester

{

       public static void main(String[] args)

       {

             String[] content = {"It was a foggy night.\nVery foggy.  Extremely foggy.\nLike there was a lot of fog!",

                                       "The street was dark\nand damp.  The shiny glare off\nof the wet pavement was eerie.",

                                       "Did I mention it was\nfoggy?  Because it was.  And\nnot just a little foggy!",

                                       "The street disappeared into\nthe fog.  One could not\nsee any trees in the area.",

                                       "As mentioned, it was foggy!\n\nTHE END"};

            

             Book b = new Book("Campeau", "FOG", content);

             System.out.println(b);

             System.out.println(b.getPage(4));

       }

}