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", "[email protected]", 'm');
        Author author3 = new Author("Jennie", "[email protected]", '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!

2
  • 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; } } Commented Apr 14, 2020 at 23:05
  • 2
    Does this answer your question? How do I declare and initialize an array in Java? Commented Apr 14, 2020 at 23:21

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

The not inlined version could also be: Author[] authors = { author2, author3 };
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);

3 Comments

It sounds like the asker is doing an assignment where author is explicitly an array, not a vararg.
I'm pretty sure you can still pass an array with this
Not if the constructor accepts an array not a vararg. You can pass an array to a vararg but not a vararg to an array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.