Class as an object?!
I have an assignment. I would like osme help if at all necessary.
publicclass Book
{
// data members
private Stringdm_title ="no title";
privatedoubledm_price = 0.0;
privateintdm_num_books = 0;
Author dm_author =new Author();
public Book()
{
}
public Book( String title,double price )
{
dm_title = title;
dm_price = price;
}
// methods
publicvoid set_
publicvoid setTitle( String title ){
dm_title = title;
}
public String getTitle(){
return dm_title;
}
publicvoid setPrice(double price ){
dm_price = price;
}
publicdouble getPrice(){
return dm_price;
}
publicvoid setNumBooks(int num ){
dm_num_books = num;
}
publicint getNumBooks(){
return dm_num_books;
}
public String toString()
{
String str;
str ="Book Title: "+dm_title+"\n";
str = str +"Price US $"+dm_price+"\n";
str +="Num Units: "+dm_num_books+"\n";
return str;
}
}
It says to "modify Books.java to have an Author as a class member" I have dont this, by putting - Author dm_author = new Author(); - . It then says "provide a set/get method for the Author data member.
How would I go about doing a get/set method for a class?
Any and all help is appreciated.
Thanks in advance.
[3236 byte] By [
Toneza] at [2007-11-26 18:59:43]

First make that Author private.Next look at your price member: you have a variable and a get and set method for it. Look at what a price is and how it is assigned. Try to do the same for Author. If you wrote the rest of the class yourself, you should be able to figure this out!
I don't think you're supposed to instantiate an Author object right there. More likely you're supposed to let it be set by the caller, either using a setAuthor method or passed in to the constructor.
String is an object, and you have a getter/setter for that. You do the same thing for author.
By the way -- in my opinion, having no-arg constructors often makes no sense at all, and this is one of those cases. It's debatable whether one should have a Book object with no author or title, but arguably such a thing would apply with blank books. But an author who isn't anybody? It doesn't really make sense to have such a thing. Even for anonymous works, an author object suggests a human being, somewhere, and not having any fields set for it when it's constructed makes no sense. Your classes should model reality, and a person who isn't a person isn't reality.
Unfortunately, there are teachers out there who think it's a good idea to always provide a no-arg constructor, no matter what.
I already have an Author class. And this Book.java class was pre-made for me. I just have to modify it.
And paulcw...
The book title, price, and units have a default setting, specified in my data members. But there will be a main() class that will ask the user for the required information. And if something is not specified, it resorts to the deafult setting that this Book.java class has provided. And yes, my prof is one of those people who put a default constructor in, no matter what.
I try to follow how the title, price, and num books are in the code, copying the get/set methods, but it results in a compile error "incompatible types". Any ideas?
Your getter and setter should work with the same types as the member variable they are setting. So if price is a double, they should use a double. If author is an Author, what type would you need?
Oh great! Thanks!
Now... how would I modify the toString() method to display the author information. I must use the Author's data member toString() method.
A sample it gives for the output would be:
Book Title: Veinte poemas de amor y una cancion desesperada
Price US $9.60
Num Units: 11
Author Information
Name: Pablo Neruda
Language: Spanish
Thanks
Again look at what you have and try to think of what you should do!
You have an Author class, does it have a toString() method? No? Then create it in the same way as the toString() method of Book. Yes? Then use it like you say you have to...
This is starting to become una cancion desesperada
I know this is getting pathetic...
import javax.swing.*;
public class BooksApp
{
public static void main(String[] args)
{
Book book1, book2;
Author author = new Author();
book1 = new Book();
book2 = new Book( "LOTR-2", 2.99 );
book1.setTitle("LOTR");
book1.setPrice(1.99);
book1.setNumBooks(1);
book1.setAuthor();
book2.setNumBooks(2);
JOptionPane.showMessageDialog( null, book1.toString() );
JOptionPane.showMessageDialog( null, book2.toString() );
}
}
With my main() class, how to I declare one Author variable after this: Book book1, book2; ?
And how do I create an Author object using the two-argument constructor. Then add it to the book1 object using the setAuthor method ?
Again look at what you have and try to think of what you should do!
You have an Author class, does it have a two-argument constructor? If not, create it and ask yourself what should those arguments be used for? Then look at the line
Author author = new Author();
supply 2 values as argument to the Author constructor in the same way you did when creating book2.
Then in the line
book1.setAuthor();
What might be missing here?
I'm not going to describe things in the same detail as the author the books you mentioned did! And compared to this assignment Frodo's assignment was a piece of cake!
Okay. Thank you! Here is what I have.
import javax.swing.*;
public class BooksApp
{
public static void main(String[] args)
{
Book book1, book2;
Author author, author2;
author = new Author();
book1 = new Book();
book2 = new Book( "LOTR-2", 2.99 );
author2 = new Author();
book1.setTitle("LOTR");
book1.setPrice(1.99);
book1.setNumBooks(1);
book1.setAuthor(author);
book2.setNumBooks(2);
book2.setAuthor(author2);
JOptionPane.showMessageDialog( null, book1.toString() );
JOptionPane.showMessageDialog( null, book2.toString() );
}
}