A Simple Project in Java
A Simple Project in Java
Submitted by:
Tacata, Jerome E. Aton, Ronnel Conde, Henry Raposon, Lilmar John Chan, Manuel O. Moreno, Von Course:
Sumbitted to: Engr. Darius A. Aonuevo MSIT and BSCOE Professor, AMA Las Pias
II.
TABLE OF CONTENTS
Front Page . p. 1 Table of Contents . p. 2 Program Questionnaire p. 3 Program Source Code . pp. 4-7 Program Output . p. 8 Reference p. 9
III.
PROGRAM QUESTIONNAIRE Create an 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. Include an abstract method named setPrice() Create two child classes of book Fiction and non-Fiction each must include a setprice() methods that sets the price for all fiction books to $24.99 and for all non-fiction books to $37.99 Write a constructor for each subclass, and include a call to set price within each. Write an application demonstrating that you can create both a fiction and a nonfiction book, and display their fields. Save the file as book.java, fiction.java, nonfiction.java and usebook.java. Additionally, write an application named book array in which you create an array that holds 10 books, some fiction and some nonfiction. Using a for loop, display details about all 10 books. Save the file as bookArray,java.
IV.
Book.java public abstract class Book { String title = new String(); double price; public Book(String t) { title = t; } public String getTitle() {
return title; }
Fiction aFinn = new Fiction("Huckelberry Finn"); NonFiction aStyle = new NonFiction("Elements of Style"); System.out.println("Fiction " + aFinn.getTitle() + " costs $" + aFinn.getPrice()); System.out.println("Non-Fiction " + aStyle.getTitle() + " costs $" + aStyle.getPrice()); } }
Fiction.java public class Fiction extends Book { public Fiction(String title) { super(title); setPrice(); } public void setPrice() { super.price = 24.99; } } 5
NonFiction.java public class NonFiction extends Book { public NonFiction(String title) { super(title); setPrice(); } public void setPrice() { super.price = 37.99; } }
Book[0] = new Fiction("Scarlet Letter"); Book[1] = new NonFiction("Introduction to Java"); Book[2] = new Fiction("Mill on the Floss"); Book[3] = new NonFiction("The Road Not Taken"); Book[4] = new Fiction("A Tale of Two Cities"); Book[5] = new NonFiction("Europe on $5 a Day"); Book[6] = new Fiction("War and Peace"); Book[7] = new Fiction("A Simple Plan"); Book[8] = new Fiction("Disclosure"); Book[9] = new Fiction("Nancy Drew");
for(s = 0; s < Book.length; ++s) { System.out.println("Book: " + Book[s].getTitle() + " costs $" + Book[s].getPrice()); } } } 7
V.
PROGRAM OUTPUT
Output # 1
Output # 2
VI.
REFERENCES