Java program Encapsulation and Abstraction to Create a class Author with the information
Create a class Author with the following information.
Member variables : name (String), email (String), and gender (char)
Parameterized Constructor: To initialize the variables
Create a class Book with the following information.
Member variables : name (String), author (of the class Author you have just created), price (double), and qtyInStock (int)
[Assumption: Each book will be written by exactly one Author]
Parameterized Constructor: To initialize the variables
Getters and Setters for all the member variables
In the main method, create a book object and print all details of the book (including the author details)
Source Code:
1: class Author {
2: private String name, email;
3: private char gender;
4: Author(String name, String email, char gender){
5: this.name = name;
6: this.email = email;
7: this.gender = gender;
8: }
9: public String getName() {
10: return name;
11: }
12: public String getEmail() {
13: return email;
14: }
15: public char getGender() {
16: return gender;
17: }
18: @Override
19: public String toString() {
20: return "Author [Name: " + name + ", Email: " + email + ", Gender: " + gender + "]";
21: }
22: }
23: class Book{
24: private String name;
25: private Author author;
26: private double price;
27: private int qtyInStock;
28: Book(String name, Author author, double price, int qtyInStock){
29: this.name = name;
30: this.author = author;
31: this.price = price;
32: this.qtyInStock = qtyInStock;
33: }
34: public String getName() {
35: return name;
36: }
37: public Author getAuthor() {
38: return author;
39: }
40: public double getPrice() {
41: return price;
42: }
43: public void setPrice(double price) {
44: this.price = price;
45: }
46: public int getQtyInStock() {
47: return qtyInStock;
48: }
49: public void setQtyInStock(int qtyInStock) {
50: this.qtyInStock = qtyInStock;
51: }
52: @Override
53: public String toString() {
54: return "Book Name: " + name + ", Author: " + author + ", Price: " + price + ", QtyInStock: " + qtyInStock;
55: }
56: }
57: public class Solution {
58: public static void main(String[] args) {
59: Author author = new Author("Shubham", "skc903@gmail.com", 'M');
60: Book book = new Book("Master in Java", author, 199.0, 500);
61: System.out.println(book);
62: }
63: }