DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING
Ex no: 8 IMPLEMENTATION OF RELATIONSHIP BETWEEN BOOK AND
Date: AUTHOR
Question
Write a Java program to implement the following relationship and create a Main class to invoke all
the methods.
Aim
To write a program to implement “has-a” relationship..
Code
package AuthorBook;
public class Author {
private String name;
private String email;
private char gender;
public Author(String name, String email, char gender) {
[Link] = name; [Link] = email; [Link] = gender; }
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public char getGender() {
return gender; }
@Override
20
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING
public String toString() { return "Author[name:"+name+",email:"+email+"]"; }}
package AuthorBook; public class Book { private String name; private Author author; private double
price;
private int qty = 0;
public Book(String name, Author author, double price) {
[Link] = name; [Link] = author; [Link] = price;
}
public Book(String name, Author author, double price, int qty) {
[Link] = name; [Link] = author; [Link] = price;
[Link] = qty;
}
public String getName() {
return name; }
public Author getAuthor() {
return author;
}
public double getPrice() {
return price; }
public void setPrice(double price) {
[Link] = price;
}
public int getQty() {
return qty; }
public void setQty(int qty) {
[Link] = qty; }
@Override public String
toString() {
return "Book[name=" + name + ", " + [Link]() + ", price=" + price + ", qty=" + qty + "]";
}}
package AuthorBook; public class TestAuthor {
public static void main(String[] args) {
[Link]("ROLL NO:717823P106");
[Link]("NAME:ASHWANTH");
Author author = new Author("J.K. Rowling", "jkrowling@[Link]", 'F');
Book book = new Book("Harry Potter", author, 29.99, 100);
[Link]("Book Name: " + [Link]());
[Link]("Author: " + [Link]().getName());
[Link]("Price: " + [Link]());
[Link]("Quantity: " + [Link]());
[Link]([Link]());
}}
Output
21
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING
Result
Thus, the Java program to implement “has-a” relationship has been successfully developed
and the output was verified.
22