Download - CSCI04

Transcript
Page 1: CSCI04

CSCI04: Java Programming Case Studies

Submitted by:

Tacata, Jerome E. 10500038

Aton, Ronnel 10500231

Conde, Henry 10500049

Raposon, Lilmar John 10500166

Chan, Manuel O. 10500095

Moreno, Von 10500040

Course:

Bachelor of Science in Information Technology

Sumbitted to: Engr. Darius A. Añonuevo

MSIT and BSCOE

Professor, AMA Las Piñas

Date Submitted: December 12, 2012

1

Page 2: CSCI04

II. TABLE OF CONTENTS

I. Front Page ……………………………………………………………. p. 1

II. Table of Contents ……………………………………………………. p. 2

III. Program Questionnaire ……………………………………………… p. 3

IV. Program Source Code ………………………………………………. pp. 4-7

V. Program Output ………………………………………………………. p. 8

VI. Reference ……………………………………………………………… p. 9

2

Page 3: CSCI04

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 non-

fiction 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.

3

Page 4: CSCI04

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();

}

4

Page 5: CSCI04

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

Page 6: CSCI04

NonFiction.java

public class NonFiction extends Book

{

public NonFiction(String title)

{

super(title);

setPrice();

}

public void setPrice()

{

super.price = 37.99;

}

}

6

Page 7: CSCI04

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

Page 8: CSCI04

V. PROGRAM OUTPUT

Output # 1

Output # 2

8

Page 9: CSCI04

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

9


Top Related