473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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!

Expand|Select|Wrap|Line Numbers
  1. public class Book2 extends Book
  2. {
  3.  
  4.     // Book2 variable
  5.     private String bookAuthor; // Author of the book
  6.  
  7.     // Book2 constructor
  8.     public Book2(String bookTitle, int itemNumber, int bookUnits, double bookPrice, String bookAuthor)
  9.     {
  10.  
  11.         // call super class constructor
  12.         super(bookTitle, itemNumber, bookUnits, bookPrice);
  13.  
  14.         this.bookAuthor = bookAuthor; // initializes bookTitle for Book2
  15.     } // end constructor
  16.  
  17.     // method to set the book author
  18.     public void setBookAuthor (String bookAuthor)
  19.     {
  20.         this.bookAuthor = bookAuthor;
  21.     } // end method setBookAuthor
  22.  
  23.     // method to retrieve the book author
  24.     public String getBookAuthor()
  25.     {
  26.         return bookAuthor;
  27.     } // end method getBookAuthor
Sep 22 '07 #1
5 3620
JosAH
11,448 Expert 8TB
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
Sep 22 '07 #2
praveen2gupta
201 100+
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!

Expand|Select|Wrap|Line Numbers
  1. public class Book2 extends Book
  2. {
  3.  
  4.     // Book2 variable
  5.     private String bookAuthor; // Author of the book
  6.  
  7.     // Book2 constructor
  8.     public Book2(String bookTitle, int itemNumber, int bookUnits, double bookPrice, String bookAuthor)
  9.     {
  10.  
  11.         // call super class constructor
  12.         super(bookTitle, itemNumber, bookUnits, bookPrice);
  13.  
  14.         this.bookAuthor = bookAuthor; // initializes bookTitle for Book2
  15.     } // end constructor
  16.  
  17.     // method to set the book author
  18.     public void setBookAuthor (String bookAuthor)
  19.     {
  20.         this.bookAuthor = bookAuthor;
  21.     } // end method setBookAuthor
  22.  
  23.     // method to retrieve the book author
  24.     public String getBookAuthor()
  25.     {
  26.         return bookAuthor;
  27.     } // 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().
Sep 22 '07 #3
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_Program3.java
Inventory_Program3.java:48: cannot find symbol
symbol : constructor Book(java.lang.String,int,int,double,java.lang.Str ing)
location: class Book
myBook[0] = new Book("The Key Triology", 1, 25, 7.99, "Nora Robe
rts");
^
Inventory_Program3.java:49: cannot find symbol
symbol : constructor Book(java.lang.String,int,int,double,java.lang.Str ing)
location: class Book
myBook[1] = new Book("The Shinning", 2, 15, 5.99, "Steven King")
;
^
Inventory_Program3.java:50: cannot find symbol
symbol : constructor Book(java.lang.String,int,int,double,java.lang.Str ing)
location: class Book
myBook[2] = new Book("Wild Acre", 3, 7, 4.99, "Phillipa Gregory"
);
^
Inventory_Program3.java:51: cannot find symbol
symbol : constructor Book(java.lang.String,int,int,double,java.lang.Str ing)
location: class Book
myBook[3] = new Book("Dark Paradise", 4, 2, 12.99, "Tami Hoag");

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

Here is some of my code from my main class:
Expand|Select|Wrap|Line Numbers
  1. public class Inventory_Program3
  2. {
  3.  
  4.     // Declare an array of classes
  5.     private Book myBook[];    
  6.  
  7.     // method to sort inventory by book title
  8.     public Book[] sortBookInventory()
  9.         {
  10.             for(int i = 0; i < myBook.length; i++) 
  11.         {
  12.                 String bookTitle1 = myBook[i].getBookTitle();
  13.                 int min = i;
  14.                 String bookTitle = bookTitle1;
  15.                     for(int j = i+1; j < myBook.length; j++) 
  16.             {
  17.                     String bookTitle2 = myBook[j].getBookTitle();
  18.                         if(bookTitle2.compareTo(bookTitle) < 0) 
  19.                 {
  20.                             min = j;
  21.                             bookTitle = bookTitle2;
  22.                         }
  23.                     }
  24.                 if(!bookTitle.equals(bookTitle1)) 
  25.         {
  26.                     Book temp = myBook[i];
  27.                     myBook[i] = myBook[min];
  28.                     myBook[min] = temp;
  29.                }
  30.             }
  31.                 return myBook;
  32.     } // end method sortBookInventory
  33.  
  34.     // constructor
  35.     Inventory_Program3()
  36.     {
  37.  
  38.         double totalInventoryValue = 0.0;
  39.  
  40.  
  41.         // Specify how many items are in the array
  42.         myBook = new Book[5];
  43.         myBook[0] = new Book("The Key Triology", 1, 25, 7.99, "Nora Roberts");
  44.         myBook[1] = new Book("The Shinning", 2, 15, 5.99, "Steven King");
  45.         myBook[2] = new Book("Wild Acre", 3, 7, 4.99, "Phillipa Gregory");
  46.         myBook[3] = new Book("Dark Paradise", 4, 2, 12.99, "Tami Hoag");
  47.         myBook[4] = new Book("Dollhouse Murders", 5, 18, 2.95, "Betty Ren Wright");
  48.  
  49.  
  50.         // instantiate Book2 object
  51.         Book2 author = new Book2();
  52.  
  53.         // call method sort book inventory for display
  54.         sortBookInventory();
  55.  
  56.         for (int i = 0; i<5; i++)
  57.         {
  58.  
  59.             // display the book title
  60.             System.out.println("The book title is: " + myBook[i].getBookTitle());
  61.  
  62.             // display the item number
  63.             System.out.println("The item number is: " + myBook[i].getItemNumber());
  64.  
  65.             // display the number of units in stock
  66.             System.out.println("The number of units in stock is: " + myBook[i].getBookUnits());
  67.  
  68.             // display the price per book
  69.             System.out.printf("The price of the book is: $%.2f\n", myBook[i].getBookPrice());
  70.  
  71.             // display the value of the inventory
  72.             System.out.printf("The value of the inventory is: $%.2f\n", myBook[i].inventoryValue());
  73.  
  74.             // display the book author
  75.             System.out.println("The book author is: " + myBook[i].getBookAuthor());
Sep 22 '07 #4
r035198x
13,262 8TB
Perhaps you wanted to create Book2 objects instead of Book objects.
The constructors you are calling are in Book2 not in Book.
Sep 22 '07 #5
Oh my gosh - PERFECT!!!!!! Thank you so much!!!!!! IT WORKED!!!!!!!!!!!!!!!! Two days of hacking away and it worked!!!!!!
Sep 22 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ruud de Jong | last post by:
The question I have is: how safe / future proof / portable is the use of the __subclasses__ method that exists for new-style classes? Background: I thought I had found an easy-to-understand...
16
by: 4Space | last post by:
I've hit something of a snag. Problem: In a DSP application we have data profiles with a varying number of points. We have a number of Discrete Fourier transforms that are optimised for a...
4
by: bonono | last post by:
Hi, Suppose my class definition is like this : class A: name = "A" @classmethod def foo(cls): cls.__super.foo()
4
by: Quentin Huo | last post by:
Hi, I created a class "myClass" and another class "mySubclass" which inherited from "myClass". In the "mySubclass", there is a public method named "methodOfSubclass(...)" which is not defined in...
4
by: Phill | last post by:
I read in one of Jesse Liberty's books that: "Because ToString() is a virtual method in the base class Object, it is guaranteed to be available in every derived class." (P.179 Programming C#) ...
6
by: Karl Hungus | last post by:
Im using an xsl transformation so its not practical to use controls in the usual way, but I would still like to call methods in my codebehind class. usually Id just use onclick="<eventHandler>"...
2
by: Ryan Liu | last post by:
Hi, There is a method Test() implemented in base class and override in subclass. In base class itself, how to make sure this.Test() will just call implementation in base class only? ...
6
by: howa | last post by:
Consider example: Animal = function(age) { this.age = age; }; Animal.prototype.sleep = function() { alert("Animal Sleeping..."); };
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.