Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class InheritanceIntro {
- public static void main(String[] args) {
- Book a = new Book("David Copperfield", "Charles Dickens",800);
- System.out.println(a);
- System.out.println(a.getAuthor());
- System.out.println(a.isLong());
- Book b= new Book("The Cat in the Hat", "Dr.Seuss", 28);
- System.out.println(b.isLong());
- LibraryBook c= new LibraryBook("It", "Stephen King",1050,"kin it");
- System.out.println(c.getTitle());
- Book d = new LibraryBook();
- //LibraryBook e = new Book();
- System.out.println(c);
- d.setTitle("Moby Dick");
- System.out.println(d.getTitle());
- Ebook f = new Ebook("Less Than Zero","Bret Easton Ellis", 470);
- for (int i=1; i<f.getPages();i++) {
- f.turnPage();
- }
- System.out.println(f.page);
- }
- }
- class Book{
- // instance variables or data fields
- private String title;
- private String author;
- private int pages;
- public Book() {
- }
- public Book(String title, String author,int pages) {
- this.title =title;
- this.author=author;
- this.pages=pages;
- }
- public String getTitle() {
- return title;
- }
- public String getAuthor() {
- return author;
- }
- public int getPages(){
- return pages;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public void setAuthor(String author) {
- this.author= author;
- }
- public void setPages(int pages) {
- this.pages = pages;
- }
- public boolean isLong() {
- if (pages >= 500)
- return true;
- else
- return false;
- }
- public String toString() {
- return title + " by author "+ author + " is "+ pages +" pages";
- }
- }
- class LibraryBook extends Book{
- private String callNumber;
- private String isPresent;
- public LibraryBook() {
- super();
- isPresent = true;
- }
- public LibraryBook(String title, String author,int pages, String callNumber) {
- super(title, author, pages);
- this.callNumber = callNumber;
- isPresent = true;
- }
- public void checkedOut() {
- isPresent = false;
- }
- public void checkedIn() {
- isPresent = true;
- }
- public String toString() {
- if(checkedIn)
- return super.toString() + " \n with call number "+ callNumber + ". The book is in the library.";
- return super.toString() + " \n with call number "+ callNumber + ". The book is checked out.";
- }
- }
- class Ebook extends Book{
- private int page = 1;
- public Ebook() {
- super();
- }
- public Ebook(String title, String author, int pages) {
- super(title, author, pages);
- }
- public void turnPage() {
- if (page == getPages())
- page = 1;
- else
- page++;
- }
- public void turnBack() {
- if (page == 1) {
- }
- else
- page--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement