FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SampleCodes/Java-Programs/Encapsulation/Book.java at main · ajacreations/SampleCodes · GitHub
ajacreations
/
SampleCodes
Public
Notifications
You must be signed in to change notification settings
Fork
27
Star
5
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SampleCodes
/
Java-Programs
/
Encapsulation
/
Book.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
90 lines (86 loc) · 2.19 KB
Breadcrumbs
SampleCodes
/
Java-Programs
/
Encapsulation
/
Book.java
Copy path
File metadata and controls
90 lines (86 loc) · 2.19 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
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)
*/
class
Author
{
private
String
name
,
email
;
private
char
gender
;
Author
(
String
name
,
String
email
,
char
gender
)
{
this
.
name
=
name
;
this
.
email
=
email
;
this
.
gender
=
gender
;
}
String
getAuthorName
()
{
return
name
;
}
char
getGender
()
{
return
gender
;
}
String
getEmail
()
{
return
email
;
}
}
class
Book
{
String
name
;
Author
author
;
double
price
;
int
qtyInStock
;
Book
(
String
name
,
double
price
,
int
qtyInStock
,
Author
author
)
{
this
.
name
=
name
;
this
.
price
=
price
;
this
.
qtyInStock
=
qtyInStock
;
this
.
author
=
author
;
}
Author
getAuthor
()
{
return
author
;
}
String
getName
()
{
return
name
;
}
double
getPrice
()
{
return
price
;
}
int
getQty
()
{
return
qtyInStock
;
}
void
showDetails
()
{
System
.
out
.
println
(
"Book Details"
);
System
.
out
.
println
(
"------------"
);
System
.
out
.
println
(
"Book Name: "
+
getName
());
System
.
out
.
println
(
"Author Name: "
+
getAuthor
().
getAuthorName
());
System
.
out
.
println
(
"Author Details: "
+
getAuthor
().
getEmail
()+
"
\t
Gender: "
+
getAuthor
().
getGender
());
System
.
out
.
println
(
"Price: "
+
getPrice
());
System
.
out
.
println
(
"Quantity in Stock:"
+
getQty
());
System
.
out
.
println
(
"--------------------------------------
\n
"
);
}
public
static
void
main
(
String
[]
args
)
{
Author
author
=
new
Author
(
"Chethan Bhagat"
,
"chethan@gmail.com"
,
'm'
);
Book
b
=
new
Book
(
"Five Point Someone"
,
175
,
10
,
author
);
b
.
showDetails
();
Author
author2
=
new
Author
(
"George R.R. Martin"
,
"grrm@gmail.com"
,
'm'
);
Book
b2
=
new
Book
(
"Clash of Kings"
,
1400
,
5
,
author2
);
b2
.
showDetails
();
}
}
Back
|
FazBrowse Home
|
New Git URL