Been following this (https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html#zz-2.2) for study java OOP.
There is three java file. Now I tested public Author getauthors() {return authors[0]}
will return the first author full into: name, email and gender.
But I want iterate the authors array to get all the author name only. what should I write in the Books java module?
One is pretty simple, used to describe
author
class. It have three attributes:private String name;private String email;private String gender;
the file already tested. All is fine.The Book file to describe the book. One book can have multiple authors.
public class Book { private String name;private int qty; private double price; Author authors[];
public Book(String name, int qty, double price) { this.name = name; this.qty = qty; this.price = price;} public Book(String name, Author authors[], int qty, double price) { this.name = name; this.authors = authors;this.qty = qty; this.price = price;} public String getName() {return name;} public void setName(String name) {this.name = name;} public double getPrice() {return price;} public void setPrice(double price) {this.price = price;} public int getQty() {return qty;} public void setQty(int qty) {this.qty =qty;} public void setauthor(Author[] author) {this.authors = authors;}}
The third file to test all the methods coded in the first two file.
public class testauthor { public static void main(String[] args) { Author[] authorstest = new Author[2]; authorstest[0] = new Author("Tan Ah Teck", "ahteck@nowhere.com", "m"); authorstest[1] = new Author("Tyler Cowen", "tcowen@gmu.edu", "M"); Book dummyBook = new Book("small steps toward a much better world",authorstest, 19, 99);System.out.println(dummyBook.getName());}}