Attempting to Call a Method from a Subclass

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pixiedustjt
    New Member
    • Sep 2007
    • 7

    Attempting to Call a Method from a Subclass

    Hello,

    This is my first time to post on the forum, so I hope I'm doing this correctly. I am working on the dreading Java Inventory Program that seems to be legendary. I am currently on part 3 and have most of the program working. I created an additional feature (book author) in a subclass, which is supposed to be called in my main class and populate my array. I've created the subclass and added the new feature, but can't figure out how to call it in my main class to populate and display in my array. Any tips or advice is greatly appreciated!

    I read the guidelines, so I'm a little worried about posting too much code. I've attached a part of my subclass where I created my "new" feature, hoping this is a good starting point and someone can confirm if I set this part up correctly.

    Thanks!

    Code:
    public class Book2 extends Book
    {
    
    	// Book2 variable
    	private String bookAuthor; // Author of the book
    
    	// Book2 constructor
    	public Book2(String bookTitle, int itemNumber, int bookUnits, double bookPrice, String bookAuthor)
    	{
    
    		// call super class constructor
    		super(bookTitle, itemNumber, bookUnits, bookPrice);
    		
    		this.bookAuthor = bookAuthor; // initializes bookTitle for Book2
    	} // end constructor
    
    	// method to set the book author
    	public void setBookAuthor (String bookAuthor)
    	{
    		this.bookAuthor = bookAuthor;
    	} // end method setBookAuthor
    
    	// method to retrieve the book author
    	public String getBookAuthor()
    	{
    		return bookAuthor;
    	} // end method getBookAuthor
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    That class looks fine to me; personally I wouldn't have included that setBookAuthor
    method because it would be silly for a book being written by X suddenly has been
    written by Y. I don't see any other problems. Simply use Book2 references in
    you main method and that's it.

    kind regards,

    Jos

    Comment

    • praveen2gupta
      New Member
      • May 2007
      • 200

      #3
      Originally posted by pixiedustjt
      Hello,

      This is my first time to post on the forum, so I hope I'm doing this correctly. I am working on the dreading Java Inventory Program that seems to be legendary. I am currently on part 3 and have most of the program working. I created an additional feature (book author) in a subclass, which is supposed to be called in my main class and populate my array. I've created the subclass and added the new feature, but can't figure out how to call it in my main class to populate and display in my array. Any tips or advice is greatly appreciated!

      I read the guidelines, so I'm a little worried about posting too much code. I've attached a part of my subclass where I created my "new" feature, hoping this is a good starting point and someone can confirm if I set this part up correctly.

      Thanks!

      Code:
      public class Book2 extends Book
      {
      
      	// Book2 variable
      	private String bookAuthor; // Author of the book
      
      	// Book2 constructor
      	public Book2(String bookTitle, int itemNumber, int bookUnits, double bookPrice, String bookAuthor)
      	{
      
      		// call super class constructor
      		super(bookTitle, itemNumber, bookUnits, bookPrice);
      		
      		this.bookAuthor = bookAuthor; // initializes bookTitle for Book2
      	} // end constructor
      
      	// method to set the book author
      	public void setBookAuthor (String bookAuthor)
      	{
      		this.bookAuthor = bookAuthor;
      	} // end method setBookAuthor
      
      	// method to retrieve the book author
      	public String getBookAuthor()
      	{
      		return bookAuthor;
      	} // end method getBookAuthor
      Hi
      your code is correct and i see that there is no problem in the calling super class constructor. You should create object of class Book2 and call the methods getBookAuthor() .

      Comment

      • pixiedustjt
        New Member
        • Sep 2007
        • 7

        #4
        Hello,

        Thank you so much for the feedback, it's good to know that my subclass is ok. What I'm having problems with is creating object of class Book2 and calling the getBookAuthor() .

        When I attempt to compile my main file, I get the following 7 errors:
        C:\java>javac Inventory_Progr am3.java
        Inventory_Progr am3.java:48: cannot find symbol
        symbol : constructor Book(java.lang. String,int,int, double,java.lan g.String)
        location: class Book
        myBook[0] = new Book("The Key Triology", 1, 25, 7.99, "Nora Robe
        rts");
        ^
        Inventory_Progr am3.java:49: cannot find symbol
        symbol : constructor Book(java.lang. String,int,int, double,java.lan g.String)
        location: class Book
        myBook[1] = new Book("The Shinning", 2, 15, 5.99, "Steven King")
        ;
        ^
        Inventory_Progr am3.java:50: cannot find symbol
        symbol : constructor Book(java.lang. String,int,int, double,java.lan g.String)
        location: class Book
        myBook[2] = new Book("Wild Acre", 3, 7, 4.99, "Phillipa Gregory"
        );
        ^
        Inventory_Progr am3.java:51: cannot find symbol
        symbol : constructor Book(java.lang. String,int,int, double,java.lan g.String)
        location: class Book
        myBook[3] = new Book("Dark Paradise", 4, 2, 12.99, "Tami Hoag");

        ^
        Inventory_Progr am3.java:52: cannot find symbol
        symbol : constructor Book(java.lang. String,int,int, double,java.lan g.String)
        location: class Book
        myBook[4] = new Book("Dollhouse Murders", 5, 18, 2.95, "Betty Re
        n Wright");
        ^
        Inventory_Progr am3.java:56: cannot find symbol
        symbol : constructor Book2()
        location: class Book2
        Book2 author = new Book2();
        ^
        Inventory_Progr am3.java:80: cannot find symbol
        symbol : method getBookAuthor()
        location: class Book
        System.out.prin tln("The book author is: " + myBook[i].ge
        tBookAuthor());
        ^
        7 errors

        Here is some of my code from my main class:
        Code:
        public class Inventory_Program3
        {
        
        	// Declare an array of classes
        	private Book myBook[];	
        
        	// method to sort inventory by book title
        	public Book[] sortBookInventory()
            	{
                	for(int i = 0; i < myBook.length; i++) 
        		{
                    	String bookTitle1 = myBook[i].getBookTitle();
                    	int min = i;
                    	String bookTitle = bookTitle1;
                    		for(int j = i+1; j < myBook.length; j++) 
        			{
                        	String bookTitle2 = myBook[j].getBookTitle();
                        		if(bookTitle2.compareTo(bookTitle) < 0) 
        				{
                            		min = j;
                            		bookTitle = bookTitle2;
                        		}
                    		}
                    	if(!bookTitle.equals(bookTitle1)) 
        		{
                        	Book temp = myBook[i];
                        	myBook[i] = myBook[min];
                        	myBook[min] = temp;
                   	}
                	}
                		return myBook;
        	} // end method sortBookInventory
        
        	// constructor
        	Inventory_Program3()
        	{
        		
        		double totalInventoryValue = 0.0;
        
        	
        		// Specify how many items are in the array
        		myBook = new Book[5];
        		myBook[0] = new Book("The Key Triology", 1, 25, 7.99, "Nora Roberts");
        		myBook[1] = new Book("The Shinning", 2, 15, 5.99, "Steven King");
        		myBook[2] = new Book("Wild Acre", 3, 7, 4.99, "Phillipa Gregory");
        		myBook[3] = new Book("Dark Paradise", 4, 2, 12.99, "Tami Hoag");
        		myBook[4] = new Book("Dollhouse Murders", 5, 18, 2.95, "Betty Ren Wright");
        			
        		
        		// instantiate Book2 object
        		Book2 author = new Book2();
        
        		// call method sort book inventory for display
        		sortBookInventory();
        
        		for (int i = 0; i<5; i++)
        		{
        
        			// display the book title
        			System.out.println("The book title is: " + myBook[i].getBookTitle());
        
        			// display the item number
        			System.out.println("The item number is: " + myBook[i].getItemNumber());
        
        			// display the number of units in stock
        			System.out.println("The number of units in stock is: " + myBook[i].getBookUnits());
        
        			// display the price per book
        			System.out.printf("The price of the book is: $%.2f\n", myBook[i].getBookPrice());
        
        			// display the value of the inventory
        			System.out.printf("The value of the inventory is: $%.2f\n", myBook[i].inventoryValue());
        
        			// display the book author
        			System.out.println("The book author is: " + myBook[i].getBookAuthor());

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Perhaps you wanted to create Book2 objects instead of Book objects.
          The constructors you are calling are in Book2 not in Book.

          Comment

          • pixiedustjt
            New Member
            • Sep 2007
            • 7

            #6
            Oh my gosh - PERFECT!!!!!! Thank you so much!!!!!! IT WORKED!!!!!!!!! !!!!!!! Two days of hacking away and it worked!!!!!!

            Comment

            Working...