0% found this document useful (0 votes)
16 views

SolTD 2

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)
16 views

SolTD 2

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
You are on page 1/ 5

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 this.name = name;
9 this.email = email;
10 this.gender = gender;
11 }
12 String getName(){ return name; }
13 String getEmail(){ return email; }
14 void setEmail(String email){ this.email = 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", "[email protected]", 'm');
6 Author sara = new Author("Sara Fadil", "[email protected]", 'f');
7 //Objects manipulation
8 sara.setEmail("[email protected]");
9 System.out.println(jhon.getName()+", "+jhon.getEmail()+", "+jhon.getGender());
10 System.out.println(sara.toString());
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 this.name = name;
10 this.author = author;
11 this.price = price;
12 }
13 Book(String name, Author author, double price, int qty){
14 this(name, author, price);
15 this.qty = qty;
16 }
17 String getName(){ return name; }
18 Author getAuthor(){ return author; }
19 double getPrice(){ return price; }
20 void setPrice(double price){ this.price = price;}
21 int getQty(){ return qty;}
22 void setQty(int qty){this.qty = qty;}
23 public String toString(){ return "Book [name = "+name+", "+author.toString()+", price = "+price+", qty = "+qty+"]";}
24
25 //Other methods
26 String getAuthorName(){ return author.getName(); }
27 String getAuthorEmail(){ return author.getEmail() ;}
28 char getAuthorGender(){ return author.getGender(); }
29
30 }
TestBook
1 class TestBook {
2 public static void main(String[] args) {
3 // Objects construction
4 Author a1 = new Author("Harry Hariom Choudhary", "[email protected]", 'm');
5 Author a2 = new Author("Sarah Dessen", "[email protected]", '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 b2.setQty(50);
12 b2.setPrice(3000);
13 System.out.println(b1.getName()+", "+b1.getPrice()+", "+b1.getQty()+ ", author:"+b1.getAuthor().getName());
14 System.out.println(b2.toString());
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 this.name = name;
10 this.authors = authors;
11 this.price = price;
12 }
13 Book2(String name, Author[] authors, double price, int qty){
14 this(name, authors, price);
15 this.qty = qty;
16 }
17 String getName(){ return name; }
18 Author[] getAuthors(){ return authors; }
19 double getPrice(){ return price; }
20 void setPrice(double price){ this.price = price;}
21 int getQty(){ return qty;}
22 void setQty(int qty){this.qty = qty;}
23 public String toString(){
24 String text = "Book [name = "+name+",";
25 for(int i=0; i<authors.length; 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[authors.length];
34 for(int i=0; i<authors.length; i++)
35 text[i] = authors[i].getName();
36 return text;
37 }
38 String[] getAuthorEmails(){
39 String[] text = new String[authors.length];
40 for(int i=0; i<authors.length; i++)
41 text[i] = authors[i].getEmail();
42 return text;
43 }
44 char[] getAuthorGenders(){
45 char[] text = new char[authors.length];
46 for(int i=0; i<authors.length; 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", "[email protected]", 'm');
5 Author a2 = new Author("Yoshua Bengio", "[email protected]", 'm');
6 Author a3 = new Author("Aaron Courville", "[email protected]", 'm');
7 //Manipulation
8 Book2 b = new Book2("Deep Learning", new Author[]{a1,a2, a3}, 10000, 200);
9 System.out.println(b.toString());
10 }
11 }

You might also like