Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //Design and implement a set of classes that define various types of reading
- //material.
- //
- //Start by defining a parent class called ReadingMaterial.
- class ReadingMaterial{
- //This class should containthe following instance variables:
- //• title - a String containing the title of the reading material
- //• author - a String containing the author of reading material
- //• numPages - an integer containing the number of pages in the reading material
- String title;
- String author;
- int numPages;
- //All three variables should be initialized with values passed into the constructor.
- public ReadingMaterial(String title,String author, int numPages){
- this.title = title;
- this.author = author;
- this.numPages = numPages;
- }
- //ReadingMaterial should also have two functions:
- //
- //• isLong - takes no parameters and returns a boolean.
- //
- public boolean isLong(){
- //
- //If the book has more than 250 pages, this function should return true
- //
- if (numPages > 250){
- return true;
- }
- //
- //Otherwise it returns false.
- //
- else {
- return false;
- }
- }
- //
- //• summary - takes no parameters and returns nothing. This function should
- // print out information about the book in the form "[title], written by [author]".
- public String summary(){
- return title + ", written by " + author;
- }
- }
- //Three classes should inherit from Reading Material: Novel, GraphicNovel, and Article.
- // Novel - class should have the following specifications:
- class Novel extends ReadingMaterial{
- //
- //• An instance variable called characters that contains an array of Strings
- // with the names of the principle characters in the novel (this variable should
- // be initialized with a value passed into the constructor, along with the novel's
- // title, author, and number of pages).
- //
- String [] characters;
- public Novel(String title, String author, int numPages, String [] characters){
- super(title,author,numPages);
- this.characters = characters;
- }
- //
- //• A function called listCharacters which prints out the name of all the primary
- // characters in the order that they are listed, each on their own line.
- //
- public void listCharacters(String [] array){
- for(int i=0; i<characters.length; i++){
- System.out.println(characters[i]);
- }
- }
- }
- // Article class - should have the following specifications:
- class Article extends ReadingMaterial{
- String publication;
- //
- //• A String instance variable called publication that indicates which publication
- // the article was published in. This should be initialized in the constructor.
- //
- public Article(String title, String author, int numPages, String publication){
- super(title,author,numPages);
- this.publication = publication;
- }
- //
- //• An overridden version of the summary function that prints out information about
- // the article in the folowing form: "[title], written by [author], published in [publication]"
- //
- //@overridden
- public String summary(){
- return title + ", written by " + author + ", published in " + publication;
- }
- }
- // GraphicNovel - class should inherit directly from the Novel class and have the following specifications:
- class GraphicNovel extends ReadingMaterial{
- //
- //• A String instance variable called illustrator that contains the
- // name of the person who illustrated the Graphic Novel.
- //
- String illustrator;
- //(This variable should be initialized with a value passed into the constructor, along with the novel's
- // title, author, and number of pages, and main characters.)
- //
- public GraphicNovel(String title, String author, int numPages, String illustrator){
- super(title,author,numPages);
- this.illustrator = illustrator;
- }
- //• An overridden version of the summary function that prints out information about
- // the book in the following form: "[title], written by [author], illustrated by [illustrator]."
- //
- //@overridden
- public String summary(){
- return title + ", written by " + author + ", illustrated by " + illustrator;
- }
- }
- // Driver class - Make one of each object:
- public class Driver {
- public static void main (String[] args){
- //
- //• A Novel, harryPotter, with the title Harry Potter, author JK Rowling, 303 pages,
- // and the main characters Harry Potter, Hermione Granger, Ronald Weasley, and Voldemort.
- //
- String harryPotter_Characters [] = {"Harry Potter", "Hermione Granger", "Ronald Weasley","Voldemort"};
- Novel harryPotter = new Novel("Harry Potter", "JK Rowling", 303, null);
- //• A GraphicNovel, avengers, with the title Avengers, author Stan Lee,
- // 50 pages, the characters Iron Man, Captain America, Black Widow, The Hulk, and Thor,
- // and the illustrator Jack Kirby.
- //
- String avengers_Characters [] = {"Iron Man", "Captain America", "Black Widow", "The Hulk", "Thor"};
- GraphicNovel avengers = new GraphicNovel("Avengers", "Stan Lee", 50, "Jack Kirby");
- //• An Article, pc, with the title The Social Meaning of the Personal Computer, author
- // Bryan Pfaffenberger, 10 pages, and the publication Anthropological Quarterly.
- //
- Article pc = new Article("The Social Meaning of the Personal Computer", "Bryan Pfaffenberger",
- 10, "Anthropological Quarterly");
- //In the Driver class print with each item on its own line.
- //
- // harryPotter:
- //
- // print out the summary
- System.out.println(harryPotter.summary());
- // the result of harryPotter's isLong function
- System.out.println(harryPotter.isLong());
- //
- //
- // avengers:
- //
- // the list of characters
- for (int i=0; i<avengers_Characters.length; i++){
- System.out.println(avengers_Characters[i]);
- }
- //the summary of avengers
- System.out.println(avengers.summary());
- //
- //pc:
- //
- //the result of isLong function
- System.out.println(pc.isLong());
- //the summary of pc
- System.out.println(pc.summary());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement