Reputation: 29
Having trouble creating an author object in the Book class.This is indeed Homework, I have came up with all the methods on my own and have been staring at this assignment for 2 hours now. any tips hints will be appreciated. I believe I am only allowed this one Author constructor with 3 parameters otherwise I would just have made an Author constructor with no arguments and the problem would be gone.
public class Author {
protected String name;
protected String email;
protected char gender;
public Author(String name, String email, char gender)
{
this.name = name;
this.email = email;
this.gender = gender;
}
public String getName()
{
return name;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public char getGener()
{
return gender;
}
public String toString()
{
return ( name + "(" + gender + ")@" + email);
}
}
public class Book extends Author{
private String name;
private Author author;
private double price;
private int qtyInStock = 0;
public Book(String name, Author author,Double price)
{
this.author = new author;
this.name = name;
this.price = price;
}
public Book(String name, Author author, double price, int qtyInStock)
{
this.name = name;
this.author = author;
this.price = price;
this.qtyInStock = qtyInStock;
}
public String getName()
{
return name;
}
public Author getAuthor()
{
return author;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public int getQtyInStock()
{
return qtyInStock;
}
public void setQtyInStock(int qtyInStock)
{
this.qtyInStock = qtyInStock;
}
public String toString()
{
return (name + " by " + author + "(" + super.gender + ")at" + super.email);
}
}
Upvotes: 1
Views: 276
Reputation: 3760
Seems strange that Book extends Author
. A Book
is not an Author
.
I think what you want to do is create a Book
object that has an Author
, but you are already passing in an Author
to your Book
constructor, so what is the problem?
class Book {
public Book(String title, Author author) {
this.title = title;
this.author = author;
}
}
If you're wondering how to create the Author
, just create it before you pass it to the Book
.
Author author = new Author("bob", "[email protected]", 'm');
Book book = new Book("some title", author);
Does that make sense?
Upvotes: 0
Reputation: 29
public class Book extends Author{
private String name;
//private final String email;
// private char gender;
private Author author;// = new Author(name,super.email,super.gender);
private double price;
private int qtyInStock = 0;
public Book(String name, Author author,double price)
{
//super();
this.author = author;// new Author(name,super.email,super.gender);
super.name = author.getName();
super.gender = author.getGender();
super.email = author.getEmail();
this.name = name;
this.price = price;
}
public Book(String name, Author author, double price, int qtyInStock)
{
this.author = author;
super.name = author.getName();
super.gender = author.getGender();
super.email = author.getEmail();
this.name = name;
this.price = price;
this.qtyInStock = qtyInStock;
}
public String getName()
{
return name;
}
public Author getAuthor()
{
return author;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public int getQtyInStock()
{
return qtyInStock;
}
public void setQtyInStock(int qtyInStock)
{
this.qtyInStock = qtyInStock;
}
public String toString()
{
return (super.name + " by " + this.name + "(" + super.gender + ")at" + super.email);
}
also added this to the author class
public Author()
{
}
Upvotes: 0
Reputation: 244
Besides changing tothis.author = author
.
You also need to add super(name, name, 'M');
to the two constructors of the Book
class.
Upvotes: 0
Reputation: 16
you should replace new author with author, also when you call the constructor of a sub Type, the Parent class's default constructor is invoked by default. so you need to specify a default constructor in your parent class, or to prevent this explicitly call your parametric constructor of parent class in the child class constructor .
Upvotes: 0
Reputation: 7347
There are multiple mistakes in your code:
this.author = author
, without the new
keyword.Author
class by also including a variable with the same name in the Book
class. Remove that variable and the getter and setter methods from the Book
class.Book
class by calling super(name, email, gender);
.Upvotes: 0
Reputation: 1194
You are inheriting Author
in Book
, inheritance is is a relationship
, means Book is a Author
which is not true in real world scenario , you need has a relation
which makes sense , that Book has a Author
.
So don't extend author in Book , keep a field of author in it as you have already did. Change you design.
Upvotes: 0
Reputation: 50809
It should be this.author = author;
without the new
keyword.
You are assigning the Author
object in the constructor, not creating new one.
By the way, Book
inherits from Author
which means it already has the Author
functionality. You don't have to save it as member.
Upvotes: 2