Advertisement
therrontelford

Book

Mar 16th, 2018
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. public class InheritanceIntro {
  2.  
  3.     public static void main(String[] args) {
  4.         Book a = new Book("David Copperfield", "Charles Dickens",800);
  5.         System.out.println(a);
  6.         System.out.println(a.getAuthor());
  7.         System.out.println(a.isLong());
  8.         Book b= new Book("The Cat in the Hat", "Dr.Seuss", 28);
  9.         System.out.println(b.isLong());
  10.         LibraryBook c= new LibraryBook("It", "Stephen King",1050,"kin it");
  11.         System.out.println(c.getTitle());
  12.         Book d = new LibraryBook();
  13.         //LibraryBook e = new Book();
  14.         System.out.println(c);
  15.         d.setTitle("Moby Dick");
  16.         System.out.println(d.getTitle());
  17.         Ebook f = new Ebook("Less Than Zero","Bret Easton Ellis", 470);
  18.         for (int i=1; i<f.getPages();i++) {
  19.             f.turnPage();
  20.         }
  21.         System.out.println(f.page);
  22.        
  23.  
  24.     }
  25.  
  26. }
  27. class Book{
  28.     // instance variables or data fields
  29.     private String title;
  30.     private String author;
  31.     private int pages;
  32.    
  33.     public Book() {
  34.        
  35.     }
  36.     public Book(String title, String author,int pages) {
  37.         this.title =title;
  38.         this.author=author;
  39.         this.pages=pages;
  40.     }
  41.     public String getTitle() {
  42.         return title;
  43.     }
  44.     public String getAuthor() {
  45.         return author;
  46.     }
  47.     public int getPages(){
  48.         return pages;
  49.     }
  50.     public void setTitle(String title) {
  51.         this.title = title;
  52.     }
  53.     public void setAuthor(String author) {
  54.         this.author=  author;
  55.     }
  56.     public void setPages(int pages) {
  57.         this.pages = pages;
  58.     }
  59.     public boolean isLong() {
  60.         if (pages >= 500)
  61.             return true;
  62.         else
  63.             return false;
  64.     }
  65.     public String toString() {
  66.         return title + " by author "+ author + " is "+ pages +" pages";
  67.     }
  68. }
  69. class LibraryBook extends Book{
  70.     private String callNumber;
  71.     private String isPresent;
  72.    
  73.     public LibraryBook() {
  74.         super();
  75.         isPresent = true;
  76.     }
  77.     public LibraryBook(String title, String author,int pages, String callNumber) {
  78.         super(title, author, pages);
  79.         this.callNumber = callNumber;
  80.         isPresent = true;
  81.     }
  82.     public void checkedOut() {
  83.         isPresent = false;
  84.     }
  85.     public void checkedIn() {
  86.         isPresent = true;
  87.     }
  88.     public String toString() {
  89.         if(checkedIn)
  90.         return super.toString() + " \n with call number "+ callNumber + ".  The book is in the library.";
  91.         return super.toString() + " \n with call number "+ callNumber + ".  The book is checked out.";
  92.     }
  93.     }
  94.     class Ebook extends Book{
  95.         private int page = 1;
  96.         public Ebook() {
  97.             super();
  98.         }
  99.         public Ebook(String title, String author, int pages) {
  100.             super(title, author, pages);
  101.            
  102.         }
  103.         public void turnPage() {
  104.             if (page == getPages())
  105.                 page = 1;
  106.             else
  107.             page++;
  108.         }
  109.         public void turnBack() {
  110.             if (page == 1) {
  111.                 }
  112.             else
  113.             page--;
  114.         }
  115.        
  116.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement