0% found this document useful (0 votes)
5 views3 pages

Java: Book-Author Relationship

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Java: Book-Author Relationship

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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) {
this.name = name; this.email = email; this.gender = 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) {
this.name = name; this.author = author; this.price = price;
}
public Book(String name, Author author, double price, int qty) {
this.name = name; this.author = author; this.price = price;
this.qty = qty;
}
public String getName() {
return name; }
public Author getAuthor() {
return author;
}
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; }
@Override public String
toString() {
return "Book[name=" + name + ", " + author.toString() + ", price=" + price + ", qty=" + qty + "]";
}}
package AuthorBook; public class TestAuthor {
public static void main(String[] args) {
System.out.println("ROLL NO:717823P106");
System.out.println("NAME:ASHWANTH");
Author author = new Author("J.K. Rowling", "[email protected]", 'F');
Book book = new Book("Harry Potter", author, 29.99, 100);
System.out.println("Book Name: " + book.getName());
System.out.println("Author: " + book.getAuthor().getName());
System.out.println("Price: " + book.getPrice());
System.out.println("Quantity: " + book.getQty());
System.out.println(book.toString());
}}

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

You might also like