0% found this document useful (0 votes)
9 views5 pages

Java Author and Book Classes Example

The documents define classes for Author, Book, and Book2 with multiple authors. Author stores name, email, and gender. Book stores a name, single Author, price, and quantity. Book2 is similar but stores an array of Authors. Test classes create objects and call methods like toString() to output details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Java Author and Book Classes Example

The documents define classes for Author, Book, and Book2 with multiple authors. Author stores name, email, and gender. Book stores a name, single Author, price, and quantity. Book2 is similar but stores an array of Authors. Test classes create objects and call methods like toString() to output details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Author

1 class Author {
2 //Attributes
3 String name;
4 String email;
5 char gender;
6 //Methods
7 Author(String name, String email, char gender){
8 [Link] = name;
9 [Link] = email;
10 [Link] = gender;
11 }
12 String getName(){ return name; }
13 String getEmail(){ return email; }
14 void setEmail(String email){ [Link] = email; }
15 char getGender(){ return gender; }
16 public String toString(){ return "Author[name = "+name+", email = "+email+", gender = "+gender+"]";}
17
18 }

TestAuthor
1 class TestAuthor {
2
3 public static void main(String[] args) {
4 //Objects construction
5 Author jhon = new Author("Jhon Dac", "[Link]@[Link]", 'm');
6 Author sara = new Author("Sara Fadil", "[Link]@[Link]", 'f');
7 //Objects manipulation
8 [Link]("[Link]@[Link]");
9 [Link]([Link]()+", "+[Link]()+", "+[Link]());
10 [Link]([Link]());
11
12 }
13
14 }
Book
1 class Book {
2 //Attributes
3 String name;
4 Author author;
5 double price;
6 int qty = 0;
7 //Methods
8 Book(String name, Author author, double price){
9 [Link] = name;
10 [Link] = author;
11 [Link] = price;
12 }
13 Book(String name, Author author, double price, int qty){
14 this(name, author, price);
15 [Link] = qty;
16 }
17 String getName(){ return name; }
18 Author getAuthor(){ return author; }
19 double getPrice(){ return price; }
20 void setPrice(double price){ [Link] = price;}
21 int getQty(){ return qty;}
22 void setQty(int qty){[Link] = qty;}
23 public String toString(){ return "Book [name = "+name+", "+[Link]()+", price = "+price+", qty = "+qty+"]";}
24
25 //Other methods
26 String getAuthorName(){ return [Link](); }
27 String getAuthorEmail(){ return [Link]() ;}
28 char getAuthorGender(){ return [Link](); }
29
30 }
TestBook
1 class TestBook {
2 public static void main(String[] args) {
3 // Objects construction
4 Author a1 = new Author("Harry Hariom Choudhary", "[Link]@[Link]", 'm');
5 Author a2 = new Author("Sarah Dessen", "[Link]@[Link]", 'f');
6
7 Book b1 = new Book("Introduction to Java Programming, Comprehensive Version", a1, 8500, 20);
8 Book b2 = new Book("Once and for All", a2, 4200);
9
10 //Maniipulation
11 [Link](50);
12 [Link](3000);
13 [Link]([Link]()+", "+[Link]()+", "+[Link]()+ ", author:"+[Link]().getName());
14 [Link]([Link]());
15 }
16
17 }
Book2 (Multi-Author)
1 class Book2 {
2 //Attributes
3 String name;
4 Author[] authors;
5 double price;
6 int qty = 0;
7 //Methods
8 Book2(String name, Author[] authors, double price){
9 [Link] = name;
10 [Link] = authors;
11 [Link] = price;
12 }
13 Book2(String name, Author[] authors, double price, int qty){
14 this(name, authors, price);
15 [Link] = qty;
16 }
17 String getName(){ return name; }
18 Author[] getAuthors(){ return authors; }
19 double getPrice(){ return price; }
20 void setPrice(double price){ [Link] = price;}
21 int getQty(){ return qty;}
22 void setQty(int qty){[Link] = qty;}
23 public String toString(){
24 String text = "Book [name = "+name+",";
25 for(int i=0; i<[Link]; i++)
26 text += ' '+ authors[i].toString();
27 text += ", price = "+price+", qty = "+qty+"]";
28 return text;
29 }
30
31 //Other methods
32 String[] getAuthorNames(){
33 String[] text = new String[[Link]];
34 for(int i=0; i<[Link]; i++)
35 text[i] = authors[i].getName();
36 return text;
37 }
38 String[] getAuthorEmails(){
39 String[] text = new String[[Link]];
40 for(int i=0; i<[Link]; i++)
41 text[i] = authors[i].getEmail();
42 return text;
43 }
44 char[] getAuthorGenders(){
45 char[] text = new char[[Link]];
46 for(int i=0; i<[Link]; i++)
47 text[i] = authors[i].getGender();
48 return text;
49 }
50
51 }

TestBook2
1 public class TestBook2 {
2 public static void main(String[] args) {
3 //Construction
4 Author a1 = new Author("Ian Goodfellow", "[Link]@[Link]", 'm');
5 Author a2 = new Author("Yoshua Bengio", "[Link]@[Link]", 'm');
6 Author a3 = new Author("Aaron Courville", "[Link]@[Link]", 'm');
7 //Manipulation
8 Book2 b = new Book2("Deep Learning", new Author[]{a1,a2, a3}, 10000, 200);
9 [Link]([Link]());
10 }
11 }

You might also like