Question
This is another simple example of Object Inheritance. The PhoneBook object is a subclass and inherits a data field and the methods from the super
This is another simple example of Object Inheritance. The PhoneBook object is a subclass and inherits a data field and the methods from the super class Book object. The PhoneBook object also has an additional data field and methods. There are only 5 bugs in this code. Four of them Netbeans will point warn you about. The fifth is hidden but will cause the pages value to be 0. Example Input Enter city for phone book Dona Ana Enter number of pages in Dona Ana's phone book 223 Example Output The phone book for Dona Ana has 223 pages. That is a small phone book.
The debug problem:
// A PhoneBook is a Book with a city import java.util.Scanner;
/** * Your Header information here */
public class DebugPhoneBook {
public static void main(String[] args) { Scanner input = new Scanner(System.in); String area, inStr; int pages;
System.out.println("Enter city for phone book"); area = input.nextLine(); System.out.println("Enter number of pages in " + area + "'s phone book"); pages = input.nextInt(); PhoneBook pb = new PhoneBook(area, pages); pb.display(); } }
//*************************************************** //** PhoneBook class starts below this box ** //*************************************************** class PhoneBook extends Book {
private String area; private String size;
PhoneBook(int pages, String city) { super(); area = city; if (pages > 300) { size = "big"; } else { size = "small"; } }
public display() { System.out.println("The phone book for " + area + " has " + pages + " pages. That is a " + size + " phone book."); } }
//****************************************************** //** Book class starts below this box ** //****************************************************** class Book {
protected int pages;
public Book(int pgs) { pgs = pages; }
public int getPages() { return pgs; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started