0

「これが設定されている場合は、これを行う」のようなステートメントをコーディングする方法を知る助けが必要です。たとえば、Author クラスと Book クラスを作成しました。私の Book クラスでは、本のタイトルを返すメソッドが必要です。ただし、本の著者が設定されている場合 (つまり、Book に著者を設定するメソッドがある場合) は、本のタイトルと著者名を返します。

これらのクラスのコードの一部を次に示します

public class Author{
  String authorFirstName;
  String authorLastName;
  int authorBirth;
  int authorDeath;

  //creates a new author object with nothing set
  public Author(){
  }

  //String l=last name, String f=frist name
  //creates new author object by setting the last and first name
  public Author(String l, String f){
    authorLastName=l;
    authroFirstName=f;
  }
//end of Author class
 } 

public class Book{
  int bookYearPublish;
  Author bookAuthor;
  String bookNumberISBN;
  String bookTitle;

  public Book(){
  }

  //String t is the title of the book
  //creats a Book object with a title
  public Book(String t){
    bookTitle=t;
  }

  //String t is the title of the book
  //Author a is the author of the book
  //creats a Book object with a title and author
  public Book(String t, Author a){
    bookTitle=t;
    bookAuthor=a;
  }

  //Author a is the author that will be set for the Book
  //sets the author of the book
  public void setAuthor(Author a){
    bookAuthor=a;
  }

  //returns the Author of the Book
  public Author getAuthor(){
    return bookAuthor;
  }

  //returns the title of Book
  //if the author is known returns a String in the form of title. last name, first name of author
  //if the year is known returns a String in the form title (year). last name, first name of author
  public String toString(){
    String title=bookTitle;
    if(bookAuthor.equals(this.getAuthor())){//I am getting a NullPointException here so this is where my problem is
      title=title+". "+bookAuthor;
    }
    if(bookYearPublish.equals(this.getYear())){
      title=bookTitle+" ("+bookYearPublish+"). "+bookAuthor;
    }
    return title;
  }

  //ends class Book
}
4

3 に答える 3