Hello Ive gotten stuck on my homework Im on the last part of

Hello. I\'ve gotten stuck on my homework. I\'m on the last part of it. I\'ll provide the directions for the section I\'m on, and the code I\'ve got up to this point. If you can fix what I\'m doing wrong and leave comments on what I\'m doing wrong or where I\'m getting things mixed up, it would be appreciated!

Directions:

Define another driver (test) class (called TestBookV2) to test the BookV2 class:

a). Create a book instance “Head First Object-Oriented Analysis & Design, $59.99” written by three authors: (test constructor)

-Brett McLaughlin, HYPERLINK \"mailto:brett@oreilly.com\" brett@oreilly.com, m (male)

-Gary Police, HYPERLINK \"mailto:gpolice@cs.wpi.edu\" gpolice@cs.wpi.edu, m (male)

-David West, HYPERLINK \"mailto:dwest@ivarjacobson.com\" dwest@ivarjacobson.com, m (male)

b). Print the above book name and number of authors (test toString() method)

c). printing the names of all the authors of the book instance just created (test printAuthors() method)

My classes so far:

---Author.java---

package book;

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 String getEmail() {
       return email;
   }

   public void setEmail(String newEmail) {
       email = newEmail;
   }

   public char getGender() {
       return gender;
   }

   @Override
   public String toString() {
       return name + \"(\" + gender + \") \" + \"at \" + email;
   }

}

------

---Book.java---

package book;

public class Book {

   private String name;
   private Author author;
   private double price;

   public Book(String name, Author author, double price) {
       this.name = name;
       this.author = author;
       this.price = price;
   }

   public String getName() {
       return name;
   }

   public Author getAuthor() {
       return author;
   }

   public double getPrice() {
       return price;
   }

   public void setPrice(double newPrice) {
       price = newPrice;
   }

   @Override
   public String toString() {
       return name + \" by \" + author + \" for $\" + price;
   }

}

------

---TestBook.java---

package book;

public class TestBook {

   public static void main(String[] args) {
       Author A = new Author(\"William Collins\", \"collinsw@lafayette.edu\", \'m\');
       Book B = new Book(\"Data Structures and the Java Collections Framework\", A, 112.89);
       System.out.print(B);
       System.out.println(\"\");
       A.setEmail(\"collinsw@cs.lafayette.edu\");
       System.out.print(\"New email is: \" + A.getEmail());
   }
}

------

---BookV2.java---

package book;

public class BookV2 {
   String name;
   private Author[] authors = new Author[3];
   double price;

   public BookV2(String name, double price) {
       this.name = name;
       this.price = price;
   }

   public String getName() {
       return this.name;
   }

   public Author[] getAuthors() {
       return this.authors;
   }

   public double getPrice() {
       return this.price;
   }

   public void setPrice(double newPrice) {
       this.price = newPrice;
       return;
   }

   @Override
   public String toString() {
       String ret = \"\'\" + this.name + \"\' by \" + authors.length + \" author(s)\";
       return ret;
   }

   public void printAuthors() {
       for (int i = 0; i < this.getAuthors().length - 1; ++i) {
           if (this.getAuthors()[i] != null) {
               System.out.print(this.getAuthors()[i]);
               if (this.getAuthors()[i + 1] != null) {
                   System.out.print(\", \");
               }
           } else {
               System.out.print(\"\");
           }
       }
       if (this.getAuthors()[this.getAuthors().length - 1] != null) {
           System.out.print(this.getAuthors()[this.getAuthors().length - 1]);
       } else {
           System.out.print(\"\");
       }
   }

}

------

---TestBookV2.java--- (the class I\'m working in now and need help in)

package book;

public class TestBookV2 {
   public static void main(String[] args) {

       Author[] authors = new Author[3];

       authors[0] = new Author(\"Brett McLaughlin\", \"brett@oreilly.com\", \'m\');
       authors[1] = new Author(\"Gary Police\", \"gpolice@cs.wpi.edu\", \'m\');
       authors[2] = new Author(\"David West\", \"dwest@ivarjacobson.com\", \'m\');
       Book testBookV2 = new Book(\"Head First Object-Oriented Analysis & Design\", authors[], 59.99);
       System.out.print(testBookV2);
   }
}

------

Thank you for the help!

Solution

hi,

you have a error in this line of main class

Book testBookV2 = new Book(\"Head First Object-Oriented Analysis & Design\",authors[], 59.99);

package books;

public class TestBookV2 {
public static void main(String[] args) {
Author authors[] = new Author[3];
int i;
authors[0] = new Author(\"Brett McLaughlin\", \"brett@oreilly.com\", \'m\');
authors[1] = new Author(\"Gary Police\", \"gpolice@cs.wpi.edu\", \'m\');
authors[2] = new Author(\"David West\", \"dwest@ivarjacobson.com\", \'m\');
for( i=0; ; i++){
   if(i<=3){

Book testBookV2 = new Book(\"Head First Object-Oriented Analysis & Design\",authors[i], 59.99);
System.out.println(testBookV2);
}else{
   System.out.println(\"finished\");
}
}
}
}

use this code and you are not providing value to array

your all values are shown

Hello. I\'ve gotten stuck on my homework. I\'m on the last part of it. I\'ll provide the directions for the section I\'m on, and the code I\'ve got up to this p
Hello. I\'ve gotten stuck on my homework. I\'m on the last part of it. I\'ll provide the directions for the section I\'m on, and the code I\'ve got up to this p
Hello. I\'ve gotten stuck on my homework. I\'m on the last part of it. I\'ll provide the directions for the section I\'m on, and the code I\'ve got up to this p
Hello. I\'ve gotten stuck on my homework. I\'m on the last part of it. I\'ll provide the directions for the section I\'m on, and the code I\'ve got up to this p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site