1

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!

Zain
  • 13,030
  • 5
  • 26
  • 49
LaurentBaj
  • 65
  • 6
  • I managed to ignore some code in the Book Class public Book(String name, Author[] author, double price, int quantity){ this.name = name; this.author = author; this.price = price; this.quantity = quantity; } // Output public String toString(){ return "Book[name = " + this.name + ", Author[name = " + author.toString() + "], price = " + this.price + ", quantity = " + this.quantity; } } – LaurentBaj Apr 14 '20 at 23:05
  • 2
    Does this answer your question? [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – Savior Apr 14 '20 at 23:21

2 Answers2

3

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);
k5_
  • 5,140
  • 2
  • 15
  • 25
1

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);
Sport
  • 55
  • 7