java creating an object in subclass - java

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);
}
}

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.

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", "bob#email.com", 'm');
Book book = new Book("some title", author);
Does that make sense?

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.

There are multiple mistakes in your code:
as #Guy said, you need to assign the author with this.author = author, without the new keyword.
you are shadowing the name variable from the 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.
You donĀ“t have a no parameter constructor, so you have to call the parent constructor in your Book class by calling super(name, email, gender);.

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 .

Besides changing tothis.author = author.
You also need to add super(name, name, 'M'); to the two constructors of the Book class.

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()
{
}

Related

Passing an object array to a constructor [duplicate]

This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 2 years ago.
I created an Author object that I used in the method signature of a constructor: public Book [String name, Author author1........]
However, the assignment I'm doing requires that I change the Author (instance variable) to a Author[]. Of course, now my previous constructor won't work. Here is the code
public class Author {
private String name;
private String email;
private char gender;
public String getName() {
return name;
}
public void setEmail(String email){
this.email = email;
}
public String getEmail(){
return email;
}
public char getGender(){
return gender;
}
public Author(String name, String email, char gender){
this.name = name;
this.email = email;
this.gender = gender;
}
public String toString(){
return "Author[name = " + this.name + ", email = " + this.email + ", gender = " + this.gender + "]";
}
}
public class Book {
private String name;
private Author[] author;
private double price;
private int quantity;
// Getters and Setters
public String getName(){
return name;
}
public Author[] getAuthor(){
return author;
}
public void setPrice(double price){
this.price = price;
}
public double getPrice(){
return price;
}
public void setQuantity(int quantity){
this.quantity = quantity;
}
public int getQuantity(){
return this.quantity;
}
// Constructors
public Book(String name, Author[] author, int quantity){
this.name = name;
this.author = author;
this.price = price;
}
public class BookTest {
public static void main(String[] args){
Author author2 = new Author("Ben", "Ben#hgmail.com", 'm');
Author author3 = new Author("Jennie", "Jennie#hgmail.com", 'f');
Book theIdiot = new Book("The Idiot", [author2, author3], 44.5, 33);
}
}
I apologize for any inconvenience if the way I've uploaded this was unsatisfactory. I have yet to learn to use Stack Overflow.
Thank you!
The syntax for inline array creation is new ArrayType[]{elem1, elemn2}
so
Book theIdiot = new Book("The Idiot", new Author[]{author2, author3}, 44.5, 33);
Or the not inlined version
Author[] authors = new Author[2];
authors[0] = author2;
authors[1] = author3;
Book theIdiot = new Book("The Idiot", authors, 44.5, 33);
Or as "Roddy of the Frozen Peas" mentioned in the comment
Author[] authors = {author2, author3}
Book theIdiot = new Book("The Idiot", authors, 44.5, 33);
Change the constructor from an Author[] to an Author.... The IDE forces you to set the order of the constructor to have the Author... in last place which means your constructor will look like this:
public Book(String name, int quantity, double price, Author... authors)
When the constructor is called, you can provide a single instance
new Book("The idiot", 123, 12.63, author1);
Or you can provide multiple instances
new Book("The idiot", 123, 12.63, author1, author2, author3);

Values are always returning null after building object [duplicate]

This question already has answers here:
Java Constructors
(10 answers)
Closed 2 years ago.
The idea is to print out a simple line saying the book title, author, and price. However, I cannot seem to return any value even after constructing a Book object.
I'm building a Book class, an Author class, and using a Tester to run the program.
Here's what I'm looking at:
package book;
public class Book {
String title;
Author author;
double price;
public Book(String title, Author author, double price){}
public Book(){}
public void setbookTitle(String title) {
this.title = title;
}
public void setPrice(double price) {
this.price = price;
}
public String getTitle() {
return title;
}
public Author getAuthor() {
return author;
}
public double getPrice() {
return price;
}
}
package book;
public class Author {
String name;
String email;
char gender;
public Author(String name, String email, char gender){
}
public String getName() {
return name;
}
public String getEmail(){
return email;
}
public char getGender(){
return gender;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setGender(char gender) {
this.gender = gender;
}
#Override
public String toString(){
return name;
}
}
package book;
public class BookTester {
public static void main(String[] args) {
Author author1 = new Author("Horstmann", "horstmann#gmail.com", 'm');
Book book1 = new Book("Big Java", author1, 60);
Double number = book1.getPrice();
System.out.println(number);
System.out.println ("The Book information is: ");
System.out.println(book1.getTitle() + book1.getAuthor() + book1.getPrice());
}
}
Any advice would be appreciated!
It is null because you didn't do anything in your Book's & Author's constructor:
public Author(String name, String email, char gender){
}
public Book(String title, Author author, double price){
}
You need to set the value and update it into your object's value:
public Author(String name, String email, char gender){
this.name = name;
this.email = email;
this.gender = gender;
}
public Book(String title, Author author, double price){
this.title = title;
this.author = author;
this.price = price;
}
you should change the Constructor to
public Book(String title, Author author, double price){
this.title = title;
this.author = author;
this. price = price;
}
public Author(String name, String email, char gender){
this.name = name;
this.email = email;
this.gender = gender;
}

Java changing instance type to already created class type

I'm new to Java and currently I'm learning constructor. So I have a Person class
class Person {
private String name;
private double height;
Person(String name, double height) {
this.name = name;
this.height = height;
}
}
and a Book class
public class Book {
private String author;
private String title;
public Raamat(String author, String title) {
this.author = author;
this.title = title;
}
}
and a Test class
public class TestBook {
public static void main(String[] args) {
Book harryp = new Book("Rouling", "Harry Potter");
}
}
My task is to change Book class, so that author is Person-type instead of String-type like this:
private String author --> private Person author
So the Book class will look like this:
public class Book {
private Person author;
private String title;
public Book(String author, String title) {
this.author = author;
this.title = title;
}
}
So basically it should get author's name from Person's constructor? Or how does it work? And how should I modify Test class for it to work?
You'll have to initialize the Person object first.
Person author = new Person("foo", 100d);
Then you can change the Book class as follow:
public class Book {
private Person author;
private String title;
public Book(Person author, final String title) {
this.author = author;
this.title = title;
}
}
And finally initialize the Book object as follows:
Person author = new Person("foo", 100d);
Book b = new Book(author, "My Title");
Solution 1:
public class Book {
private Person author;
private String title;
public Book(String author, String title) {
this.author = new Person(author);
this.title = title;
}
}
class Person {
private String name;
private double height;
public static final double DEFAULT_HEIGHT = 0.0;
Person(String name, double height) {
this.name = name;
this.height = height;
}
Person(String name) {
this(name, DEFAULT_HEIGHT);
}
}
You have to add one more constructor into your Person class, and instantate a new Person when you pass String author into Book
Book b = new Book("Mike", "Book Title");
Alternatively, pass person instance into Book and access name by getName():
public class Book {
private Person author;
private String title;
public Book(Person author, String title) {
this.author = author.getName();
this.title = title;
}
}
So basically it should get author's name from Person's constructor?
Not necessarily. You can keep the client code simple by creating the Person directly in the Book constructor.
public class Book {
private Person author;
private String title;
public Book(String authorName, double height, String title) {
this.author = new Person(authorName, height);
this.title = title;
}
}
You would call it :
Book book = new Book("author", 170, "book title");
Yon can also accept a Person as parameter in the constructor :
public Book(Person author, String title) {
this.author = author;
this.title = title;
}
And in this case, the client code should pass a Person and not a String and a double.
Person author = new Person("author", 170)
Book book = new Book(author, "book title");
You can even propose both by overloading the constructor.
In fact, the way that you should use is the way that is the most practical for the class clients.
author should be a new Person, whose name is drawn from the name field of Book's constructor.
There are two ways you can solve this:
By changing the parameter type on your constructor to Person:
public class Book {
private Person author;
private String title;
public Raamat(Person author, String title) {
this.author = author;
this.title = title;
}
}
By initializing the author on the constructor:
public class Book {
private Person author;
private String title;
public Raamat(String author, String title) {
this.author = new Person(author);
this.title = title;
}
}

using a java construct parameter in one class inside another class

I have this construct in .Author
package author;
public class Author {
private String name;
private String email;
private 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 char gender () {
return gender;
}
public String getEmail () {
return email;
}
public void SetEmail (String userEmail) {
email = userEmail;
}
public String toString () {
return "Author info: " + " [name: " +name + ", " + " gender: " + gender +", " + " email " + email + "]";
}
}
I have another class named .Book, I want to use the "name" variable from the Author construct inside another construct inside the .Book as shown below:
public class Book {
private String bookName;
private String author;
private double price;
private int qty;
public Book (String bookName, Author name, double price) {
this.bookName = bookName;
// here I'm getting an error saying ";" required
this.author = Author name;
this.price = price;
}
}
I'm getting an error (shown in the second code) so clearly I'm not doing it right and don't know how. Any help?
public class Book {
private String bookName;
private Author author; //it should be Author, not String
private double price;
private int qty;
public Book (String bookName, Author name, double price) {
this.bookName = bookName;
this.author = name; //here was ur mistake
this.price = price;
}
}
Try this code. Assumed, while constructing the book, you do like
Book book = new Book("name", new Author("name", "name#mail", m), 22.0);

using method to add book to a library array

in this program i need to add 4 books to a library.The code is for my book class but i need to make a method with 4 arguments in another file in my library class that will add the books into a library array.There is a third file for the main where i will print this all out but i just cant figure this out.
Example of book: author-clowney, title-individualism,price-5.50,isbn-978-52-234-43-012
private String author;
private String title;
private double price;
private int isbn;
public book(String a, String t, double p, int i){
author=a;
title=t;
price=checkPrice(p);
isbn=checkIsbn(i);
}
//First and foremost you need to create the book object
public class Book extends Library{
private String author;
private String title;
private double price;
private int isbn;
//Null Constructor-this creates
public book() {
this.author = abcd;
this.title = abcd;
this.price = 0.0;
this.isbn = 0000000;
}
//Now its time for the book with parameters
public book(String a, String t, double p, int i){
author = a;
title = t;
price = p;
isbn = i;
}
//Now it's time to set and get the above values you want
public void setAuthor(String author){
this.author = author;
}
public String getAuthor(){
return this.author;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return this.title;
}
public void setPrice(double price){
this.price = price;
}
public double getPrice(){
return this.price;
}
public void setIsbn(int isbn){
this.isbn = isbn;
}
public double getIsbn(){
return this.isbn;
}
//Now you are going to want to toString(), that way you can display the books in the library
public String toString() {
//Im assuming you have learned that Library is your super class and has a toString() as well
return(super.toString()+ "Book Author: " + this.author + "Book Title: " + this.title + "Book Price: " + this.price + "Book ISBN: " + this.isbn);
}
}
//Now simply call this method within your main method to display books in Output!!
//Hope this helps, if you have further questions please leave below!!

Categories