• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding & Searching Data ! need help !  RSS feed

 
Java Is Hard
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys !

Can someone please guide me, I have created a program but it's incomplete, can anyone tell me what to do further!

The task is:

Write a java program which defines and implements an appropriate set of attributes for a Book class. Implement two polymorphic add() methods for adding a current instance of a Book to an Array and an instance of a book to a another Book object.

The code I wrote:

class book
{

String BookName;
String Isbn;
int NoOfStock;
String Author;

public static void main(String[]args)
{

book[] bookdata= new book[10];

bookdata[0]=new book();
bookdata[0].BookName="Visual Basic";
bookdata[0].Isbn="E12";
bookdata[0].NoOfStock=2;
bookdata[0].Author="David";

bookdata[1]=new book();
bookdata[1].BookName="Java 2";
bookdata[1].Isbn="R29";
bookdata[1].NoOfStock=1;
bookdata[1].Author="Schildt";

for(int i=0; i<2;i++){

System.out.println(bookdata[i].BookName);
System.out.println(bookdata[i].Isbn);
System.out.println(bookdata[i].NoOfStock);
System.out.println(bookdata[i].Author);

}
}
}

What to do NOW ??? I've added the books but how will I add another book and check it ! and can I do it through parameter ?

take care all.
 
Edwin Keeton
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd work on creating those add() methods.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is Hard:

Welcome to JavaRanch! Please take a moment to read our Naming Policy, and then
Change your display name to comply. (We are looking for a real-sounding, first and last name (preferrably your real one)).

As far as your question goes:

In actuality, the assignment is pretty poorly written. First, I don't think they mean polymorphic methods, but rather overloaded methods. Then, why are you adding one book to another? This does not make logical sense.... Finally, it should not be the responsibility of the Book to add itself to the array, but the reponsibility of another object (maybe a Library) to add books. But all of these points are neither here nor there; you can't actually change the assignment.

As far as the assignment goes, you need to write two add() methods. The first will place the current instance of a Book into an array, and the second will add the book to another book. So, start by writing the method signatures:


Now, as to the actual implementation of those methods, well, that's an exercise left to the reader.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!