Advertisement
jTruBela

ReadingMaterial

Mar 6th, 2021
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1. //
  2. //Design and implement a set of classes that define various types of reading
  3. //material.
  4. //
  5.  
  6. //Start by defining a parent class called ReadingMaterial.
  7. class ReadingMaterial{
  8.  
  9. //This class should containthe following instance variables:
  10.     //• title - a String containing the title of the reading material
  11.     //• author - a String containing the author of reading material
  12.     //• numPages - an integer containing the number of pages in the reading material
  13.  
  14.     String title;
  15.     String author;
  16.     int numPages;
  17.  
  18. //All three variables should be initialized with values passed into the constructor.
  19.     public ReadingMaterial(String title,String author, int numPages){
  20.         this.title = title;
  21.         this.author = author;
  22.         this.numPages = numPages;
  23.     }
  24.        
  25. //ReadingMaterial should also have two functions:
  26.     //
  27.     //• isLong - takes no parameters and returns a boolean.
  28.     //
  29.     public boolean isLong(){
  30.         //
  31.         //If the book has more than 250 pages, this function should return true
  32.         //
  33.         if (numPages > 250){
  34.             return true;
  35.         }
  36.         //
  37.         //Otherwise it returns false.
  38.         //
  39.         else {
  40.             return false;
  41.         }
  42.     }
  43.    
  44.     //
  45.     //• summary - takes no parameters and returns nothing. This function should
  46.     //            print out information about the book in the form "[title], written by [author]".
  47.     public String summary(){
  48.         return title + ", written by " + author;
  49.     }
  50. }
  51.  
  52.  
  53.  
  54. //Three classes should inherit from Reading Material: Novel, GraphicNovel, and Article.
  55.  
  56.     // Novel - class should have the following specifications:
  57. class Novel extends ReadingMaterial{
  58.    
  59.     //
  60.     //• An instance variable called characters that contains an array of Strings
  61.     //  with the names of the principle characters in the novel (this variable should
  62.     //  be initialized with a value passed into the constructor, along with the novel's
  63.     //  title, author, and number of pages).
  64.     //
  65.     String [] characters;
  66.     public Novel(String title, String author, int numPages, String [] characters){
  67.         super(title,author,numPages);      
  68.         this.characters = characters;
  69.     }
  70.  
  71.     //
  72.     //• A function called listCharacters which prints out the name of all the primary
  73.     //  characters in the order that they are listed, each on their own line.
  74.     //
  75.     public void listCharacters(String [] array){
  76.         for(int i=0; i<characters.length; i++){
  77.             System.out.println(characters[i]);
  78.         }
  79.     }
  80. }
  81.  
  82.     // Article class - should have the following specifications:
  83. class Article extends ReadingMaterial{
  84.         String publication;
  85.     //
  86.     //• A String instance variable called publication that indicates which publication
  87.     //  the article was published in. This should be initialized in the constructor.
  88.     //
  89.     public Article(String title, String author, int numPages, String publication){
  90.         super(title,author,numPages);
  91.         this.publication = publication;
  92.     }
  93.    
  94.     //
  95.     //• An overridden version of the summary function that prints out information about
  96.     //  the article in the folowing form: "[title], written by [author], published in [publication]"
  97.     //
  98.     //@overridden
  99.     public String summary(){
  100.         return title + ", written by " + author + ", published in " + publication;
  101.     }
  102. }
  103.  
  104.     // GraphicNovel - class should inherit directly from the Novel class and have the following specifications:
  105. class GraphicNovel extends ReadingMaterial{
  106.     //
  107.     //• A String instance variable called illustrator that contains the
  108.     //  name of the person who illustrated the Graphic Novel.
  109.     //
  110.     String illustrator;
  111.     //(This variable should be initialized with a value passed into the constructor, along with the novel's
  112.     //  title, author, and number of pages, and main characters.)
  113.     //
  114.     public GraphicNovel(String title, String author, int numPages, String illustrator){
  115.         super(title,author,numPages);
  116.         this.illustrator = illustrator;
  117.     }
  118.     //• An overridden version of the summary function that prints out information about
  119.     //  the book in the following form: "[title], written by [author], illustrated by [illustrator]."
  120.     //
  121.     //@overridden
  122.     public String summary(){
  123.         return title + ", written by " + author + ", illustrated by " + illustrator;
  124.     }
  125. }
  126.  
  127.     // Driver class - Make one of each object:
  128. public class Driver {
  129.     public static void main (String[] args){
  130.        
  131.         //
  132.         //• A Novel, harryPotter, with the title Harry Potter, author JK Rowling, 303 pages,
  133.         //  and the main characters Harry Potter, Hermione Granger, Ronald Weasley, and Voldemort.
  134.         //
  135.         String harryPotter_Characters [] = {"Harry Potter", "Hermione Granger", "Ronald Weasley","Voldemort"};
  136.         Novel harryPotter = new Novel("Harry Potter", "JK Rowling", 303, null);
  137.                                      
  138.        
  139.         //• A GraphicNovel, avengers, with the title Avengers, author Stan Lee,
  140.         //  50 pages, the characters Iron Man, Captain America, Black Widow, The Hulk, and Thor,
  141.         //  and the illustrator Jack Kirby.
  142.         //
  143.         String avengers_Characters [] = {"Iron Man", "Captain America", "Black Widow", "The Hulk", "Thor"};
  144.         GraphicNovel avengers = new GraphicNovel("Avengers", "Stan Lee", 50, "Jack Kirby");
  145.            
  146.         //• An Article, pc, with the title The Social Meaning of the Personal Computer, author
  147.         //  Bryan Pfaffenberger, 10 pages, and the publication Anthropological Quarterly.
  148.         //
  149.         Article pc = new Article("The Social Meaning of the Personal Computer", "Bryan Pfaffenberger",
  150.                                  10, "Anthropological Quarterly");
  151.  
  152.         //In the Driver class print with each item on its own line.
  153.         //
  154.         // harryPotter:
  155.         //
  156.         // print out the summary
  157.         System.out.println(harryPotter.summary());
  158.        
  159.         // the result of harryPotter's isLong function
  160.         System.out.println(harryPotter.isLong());
  161.         //
  162.        
  163.         //
  164.         // avengers:
  165.         //
  166.         // the list of characters
  167.         for (int i=0; i<avengers_Characters.length; i++){
  168.             System.out.println(avengers_Characters[i]);
  169.         }
  170.        
  171.         //the summary of avengers
  172.         System.out.println(avengers.summary());
  173.        
  174.         //
  175.         //pc:
  176.         //
  177.         //the result of isLong function
  178.         System.out.println(pc.isLong());
  179.        
  180.         //the summary of pc
  181.         System.out.println(pc.summary());
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement