time() && $theUser1!="1234") die(); ?> AP CS
Loading
notes intro/old submit AP Problems the dump links
 

Quiz 21 Subclasses

 

id:

 

 

1.True or false: All methods and fields of the parent class are useable by the child class. true false

2. True or false: If a subclass does not have a constructor it will automatically call the parent class's constructor. true false

 

For the remaining questions:

public class Book
{
   private int pages;


   //constructor initializes pages to 2000 by default
   public Book()
   {
       pages=2000;
   }

   //constructor initializes global pages to the parameter
   public Book(int pages)
   {
       this.pages=pages;
   }

   //----------------------------------------------------------------
   //  Prints a message about the pages of this book.
   //----------------------------------------------------------------
   public void message ()
   {
      System.out.println ("Number of pages: " + pages);
   }

   public int getPages()
   {
       return pages;
   }
}

public class Dictionary extends Book
{
   private int definitions;

   public Dictionary(int definitions)
   {
       this.definitions=definitions;
   }


   //-----------------------------------------------------------------
   //  Prints a message using both local and inherited values.
   //-----------------------------------------------------------------
   public void message ()
   {
      System.out.print ("Number of definitions: " + definitions);

      System.out.print ("Definitions per page: " + definitions/getPages());
   }

}
	   

3. If Dictionary extends Book then the relationship is best described as:

Dictionary is a Book

Book is a Dictionary

Dictionary has a Book

Book has a Dictionary

 

4. If a Dictionary called websters is created with 5200 definitions and the command websters.message() is called:

It prints "number of pages is 2000"
It prints "number of definitions is 5200 Definitions per page: 2"
It gives a compile time error as you cant access getPages from dictionary webster as its part of book
It gives a compile time error because you cant access message from dictionary webster.

 

5. Which of the following would work to create webster:

I. Book webster = new Dictionary(1500);

II. Dictionary webster = new Dictionary();

III. Dictionary webster = new Dictionary<Book>(1500);

I only II only III only I and II All of the above

 

6. If we added a constructor for Dictionary that would take in pages and also definitions. If we wanted to call the constructor of Book in the body of the new constructor we might say:
super.Book (pages)
super(pages)
Book(pages)
super.Book.pages