Sei sulla pagina 1di 5

Object Oriented Programming

Assignment 1 Fall 2014

Question 1
 
 
Design and implement a set of classes that define various types of reading
material: books, novels, magazines, technical journals, text- books, and so on.
Include data values that describe various attributes of the material, such as
the number of pages and the names of the primary characters. Include
methods that are named appropriately for each class and that print an
appropriate message. Create a main driver class to instantiate and exercise
several of the classes. [assume all fields are private]

Here  is  the  code  for  BookClub  


 
public  class  BookClub  {  
       //-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  
       //    Creates  several  objects  from  classes  derived  from  the  
       //    ReadingMatter  class.  
       //-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  
       public  static  void  main  (String[]  args)  
       {  
               ReadingMatter[]  rm  =  new  ReadingMatter[5];  
 
               rm[0]  =  new  ReadingMatter  ("Myst  Strategy",  "0-­‐7615-­‐0807-­‐4");  
 
               rm[1]  =  new  Book  ("Great  Eskimo  Vocabulary  Hoax,  The",  
                               "0-­‐226-­‐68534-­‐9",  
                               "Pullum,  Geoffrey");  
 
               rm[2]  =  new  TextBook  ("Java  Software  Solutions",  
                               "0-­‐201-­‐61271-­‐2",  
                               "Lewis,  John  and  William  Loftus",  
                               true);  
 
               String[]  names  =  {  "Hazel",  "Fiver",  "Bigwig",  
                               "Blackberry",  "Dandelion"  };  
 
               rm[3]  =  new  Novel  ("Watership  Down",  
                               "0-­‐380-­‐00293-­‐0",  
                               "Adams,  Richard",  names);  
 
               rm[4]  =  new  Magazine  ("ACM  Crossroads",  
                               "0-­‐234-­‐5678-­‐0",  
                               "Perry,  Lynellen  and  others");  
 
               for  (int  index  =  0;  index  <  rm.length;  index++)  
               {  
                       rm[index].content();  
                       System.out.println();  
               }  
       }  
}  
 
 
}  
 
 
Question 2

Write a class named Book that keeps track of book objects such that the
instance data contains the book’s title and author and a unique identification
number, say id that starts form 1 for the first book object and is incremented
by 1 each time a new book with at least the title specified is created. The
required methods in the Book class are as follows:

• 3 constructors: One without parameters that sets the title and author to
“unknown” and id to 0; one with a single parameter that sets the title
to the given value as parameter, sets the author to “unknown”,
increments the sequence by 1 and sets id to this sequence number;
one with two parameters for title and author, setting the corresponding
instance data, and id as in the second constructor.
• The setter methods for title and author.
• The getter methods for title, author, and id.
• The equals method that compares the current book object with
another Book object given as a parameter and returns true if both
objects have the same title and author properties, and false otherwise.
• The toString method that returns a text including the book’s title,
author and id. Refer to the sample execution window for the test
program for details.
• The get Initials method that returns the initial letters of the author’s
first name(s) and last name, if the author’s name is known (not equal to
“unknown”, or not null).Assume that there can be at most two names
and one surname separated by a single blank. Refer to the sample
execution window for the test program for details.

Write a driver class that tests the Book class. Allow the user to enter as many
book objects as s/he wants. Take care of necessary object initializations. Store
the first and last book objects separately. Print each book object. Compare
the first and last book objects, if any, and display a proper message if they are
the same. Display the last book’s author’s initials, if the user has input any
valid book object.

Sample execution windows:


To end the input process bypass each question by typing the enter key!
Type the title of the book:
Type the name of the author:
Press any key to continue...

To end the input process bypass each question by typing the enter key!
Type the title of the book: Windows NT Server 4.0
Type the name of the author: Russel
Book No: 1 entitled "Windows NT Server 4.0" written by Russel
Type the title of the book:
Type the name of the author:
First and last books are same
Last book's author has the initials, R.
Press any key to continue...

To end the input process bypass each question by typing the enter key!
Type the title of the book: Java Software Solutions
Type the name of the author: Lewis Loftus
Book No: 1 entitled "Java Software Solutions" written by Lewis Loftus
Type the title of the book: Introduction to Java Programming with JBuilder
Type the name of the author: Yvet Daniel Liang
Book No: 2 entitled "Introduction to Java Programming with JBuilder" written
by
Yvet Daniel Liang
Type the title of the book:
Type the name of the author:
Last book's author has the initials, Y.D.L.
Press any key to continue...
 
 
Here  is  the  code  for  BookTest  class  
 
import java.util.Scanner;
public class BookTest {
public static void main ( String [ ] args ) {
Scanner oku = new Scanner (System.in) ;
String title, author;

System.out.println("To end the input process bypass each "+


"question by typing the enter key!");
System.out.print("Type the title of the book: ");
title = oku.nextLine();
System.out.print("Type the name of the author: ");
author = oku.nextLine();

Book aBook, firstBook, lastBook;

aBook = firstBook = lastBook = new Book();

while (!(title.equals("") && author.equals(""))) {


if (author.equals(""))
aBook = new Book(title);
else
aBook = new Book(title, author);

System.out.println(aBook);

if (aBook.getId()==1)
firstBook=aBook;

System.out.print("Type the title of the book: ");


title = oku.nextLine();
System.out.print("Type the name of the author: ");
author = oku.nextLine();
}

lastBook = aBook;

if (firstBook.equals(new Book()) == false) {


if (firstBook.equals(lastBook))
System.out.println("First and last books are
same");

System.out.println("Last book\'s author has the


initials, "
+ lastBook.getInitials() );
}
}
}
 
 

Potrebbero piacerti anche