简体   繁体   中英

I cant get my constructors from my subclasses to read the Strings I have put in them

I am trying to get my constructors from my subclasses to read the Strings which I have put in them. I keep getting null for the output, when it should be ChildrensBook , I am confused on what to do. Any help is appreciated thank you.

This is my assignment:

Design and implement a set of classes that define types of reading material: books, novels, magazines, technical journals, textbooks, and so on. Include data values that describe material, such as the number of pages and the names of the authors. Include methods that are named for each class and that print an appropriate message. Create a main driver class to instaniate several of the classes.

My Java code:

Driver:

public class Reading{

public static void main(String[]args)  
{  
   Book book       = new Book(null, 0, 0, 0);  
   Magazine mag    = new Magazine(null, 0, 0);  
   Journal journal = new Journal(null, 0, 0, 0);  
   Textbook txt    = new Textbook(null, 0, 0, 0);  
   Manual manual   = new Manual(null, 0, 0, 0);

   book.print();  
   mag.print();  
   journal.print();  
   txt.print();  
   manual.print();
}

SuperClass:

public class ReadingMaterial {

  String type;

  int pages, characters, numPictures;

  public ReadingMaterial(String type, int pages, int characters, int numPictures)  
  {  
      this.type        = type;  
      this.pages       = pages;  
      this.characters  = characters; 
      this.numPictures = numPictures;
  }  

  public void print()
  {     
      System.out.println("Reading Material Data: "); 
      System.out.println("");
      System.out.println("Type of reading material: " +type);  
      System.out.println("Number of pages: " +pages);  
      System.out.println("Number of primary characters: " +characters);  
      System.out.println("Number of pictures: " +numPictures);
      System.out.println("");
  }  
}  

And one of the subclasses:

public class Book extends ReadingMaterial 
{  
   public Book(String type, int pages, int characters, int numPictures)  
   {  
     super("ChildrensBook", 22, 5, 20);  
   }    
}  

Thats because you're passing null to your child classes:

    Book book = new Book(null, 0, 0, 0);  
    Magazine mag = new Magazine(null, 0, 0);  
    Journal journal= new Journal(null, 0, 0, 0);  
    Textbook txt = new Textbook(null, 0, 0, 0);  
    Manual manual= new Manual(null, 0, 0, 0);

This is what you should do. You should pass the String that you expect the type to have.

Book book = new Book("ChildrensBook", 0,0,0);

you are getting null because you are passing it null .

EDIT:

Also please use proper coding standards while naming the variables. A variable should start with a small letter and then should be camel cased.

 public Book(String nameOfTheBook, int pages, int characters, int numPictures){  
     super(nameOfTheBook,22,5,20);  
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM