Question:

Need Java Help ? ? ?

by  |  earlier

0 LIKES UnLike

Create abstract class named Book. Include a String field for the book’s title and a double field for the book’s price. Within the class, include a constructor that requires the book title and add two get methods – one that returns the title and one that returns the price( ). Create two child classes of Book: Fiction and NonFiction Books. Each must include a setPrice( ) method that sets the price for all Fiction Books to $24.99 and for all NonFiction Books to $37.99. Write a constructor for each subclass, and include a call to setPrice( ) within each. Display the type, title, and price for the two classes of book you have coded.Write an application demonstrating that you can create both a Fiction and a NonFiction Book, and display their fields. Save the Book.java, Fiction.java, NonFiction.java and UseBook.java files in particular folder.

Fiction:

Scarlet Letter, Mill on the Floss, The Road Not Taken, A Tale of Two Cities, War and Peace, A Simple Plan, Disclosure, Nancy Drew

NonFiction:

Europe on $5 a Day, Introduction to Java

 Tags:

   Report

1 ANSWERS


  1. public abstract class Book {

        private String title;

        double price;

        public Book(String title) {

            this.title = title;

            this.price = 0.0;

        }

        public String getTitle() {

            return title;

        }

        public double getPrice() {

            return price;

        }

        public abstract void setPrice();

    }

    /////////////////////////////////////

    public class Fiction extends Book {

        public Fiction(String title) {

            super(title);

            setPrice();

        }

        

        public void setPrice() {

             price = 24.99;

        }

        void showInfo() {

            System.out.println("Fiction - Title: " + getTitle() + " $"+getPrice());

        }

    }

    /////////////////////////////////////

    public class NonFiction extends Book {

        public NonFiction(String title) {

            super(title);

            setPrice();

        }

        

        public void setPrice() {

             price = 37.99;

        }

        void showInfo() {

            System.out.println("NonFiction - Title: " + getTitle() + " $"+getPrice());

        }

    }

    /////////////////////////////////////

    public class UseBook {

        public static void main(String[] args) {

            Fiction aSimplePlan = new Fiction("A Simple Plan");

            NonFiction javaIntro = new NonFiction("Introduction to Java");

            aSimplePlan.showInfo();

            javaIntro.showInfo();

        }

    }

    /////////////////////////////////////

You're reading: Need Java Help ? ? ?

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions