You are on page 1of 9

CSCI04: Java Programming Case Studies

Submitted by:

Tacata, Jerome E. Aton, Ronnel Conde, Henry Raposon, Lilmar John Chan, Manuel O. Moreno, Von Course:

10500038 10500231 10500049 10500166 10500095 10500040

Bachelor of Science in Information Technology

Sumbitted to: Engr. Darius A. Aonuevo MSIT and BSCOE Professor, AMA Las Pias

Date Submitted: December 12, 2012

II.

TABLE OF CONTENTS

I. II. III. IV. V. VI.

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.

PROGRAM SOURCE CODE EXPLANATION

Book.java public abstract class Book { String title = new String(); double price; public Book(String t) { title = t; } public String getTitle() {

return title; }

public double getPrice() { return price; } public abstract void setPrice(); }

UseBook.java public class UseBook { public static void main(String[] args) {

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; } }

BookArray.java public class BookArray { public static void main(String[] args)

{ Book Book[] = new Book[10]; int s;

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

www.google.com www.youtube.com http://www.roseindia.net/java/java-get-example/ http://www.codemiles.com/java/what-is-an-abstract-class-t578.html http://www.dreamincode.net/forums/topic/217047-using-abstract-class/ http://www.youtube.com/watch?v=TyPNvt6Zg8c

You might also like