Skip to content

Commit 6362ced

Browse files
committed
OOP: Encapsulation
1 parent c1b0c66 commit 6362ced

9 files changed

Lines changed: 324 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Sample code used to explain Object Oriented Programming.
3+
* Version History:
4+
* 1.0 - The orginal source code.
5+
* 1.1 - All the properties of Animals have been encapsulated.
6+
*
7+
* @version 1.1
8+
* @author L.Gobinath
9+
*/
10+
public class DoctorDemo {
11+
public static void main(String[] args) {
12+
Cat cat = new Cat();
13+
Dog dog = new Dog();
14+
VeterinaryDoctor doctor = new VeterinaryDoctor();
15+
cat.setAge(4);
16+
dog.setAge(2);
17+
doctor.treatCat(cat);
18+
doctor.treatDog(dog);
19+
}
20+
}
21+
22+
class VeterinaryDoctor {
23+
/**
24+
* Give treatment to a cat.
25+
* @param cat The cat.
26+
*/
27+
public void treatCat(Cat cat) {
28+
if (cat.getAge() < 3) { // Changed to getAge()
29+
System.out.println("Treatment C1");
30+
} else {
31+
System.out.println("Treatment C2");
32+
}
33+
}
34+
35+
/**
36+
* Give treatment to a dog.
37+
* @param dog The dog.
38+
*/
39+
public void treatDog(Dog dog) {
40+
if (dog.getAge() < 5) { // Changed to getAge()
41+
System.out.println("Treatment D1");
42+
} else {
43+
System.out.println("Treatment D2");
44+
}
45+
}
46+
}
47+
48+
/**
49+
* Encapsulated Cat.
50+
*/
51+
class Cat {
52+
private int age;
53+
54+
public int getAge() {
55+
return this.age;
56+
}
57+
58+
public void setAge(int age) {
59+
this.age = age;
60+
}
61+
}
62+
63+
/**
64+
* Encapsulated Dog.
65+
*/
66+
class Dog {
67+
private int age;
68+
69+
public int getAge() {
70+
return this.age;
71+
}
72+
73+
public void setAge(int age) {
74+
this.age = age;
75+
}
76+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Book class used to explain the encapsulation.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public class Book {
8+
// Title of the book.
9+
String title;
10+
// Author of the book.
11+
String author;
12+
// Price of the book.
13+
double price;
14+
15+
/**
16+
* Print the total amount for given quantity.
17+
* @param qty The quantity
18+
*/
19+
public void sell(int qty) {
20+
System.out.println("Total: $" + (qty * price));
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Testing class for Book class.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public class BookShop {
8+
public static void main(String[] args) {
9+
Book book = new Book();
10+
book.title = "Hamlet";
11+
book.author = "William Shakespeare";
12+
book.price = -5.95; // A negative price!
13+
14+
System.out.println("Title: " + book.title);
15+
System.out.println("Price: $" + book.price);
16+
book.sell(2);
17+
}
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Book class used to explain the encapsulation.
3+
*
4+
* @version 1.1
5+
* @author L.Gobinath
6+
*/
7+
public class Book {
8+
// Title of the book.
9+
String title;
10+
// Author of the book.
11+
String author;
12+
// Price of the book.
13+
double price;
14+
15+
/**
16+
* Set the price.
17+
* @param price Price of the book.
18+
*/
19+
public void setPrice(double price) {
20+
if (price > 0) {
21+
this.price = price;
22+
} else {
23+
System.out.println("Invalid price");
24+
}
25+
}
26+
27+
/**
28+
* Print the total amount for given quantity.
29+
* @param qty The quantity
30+
*/
31+
public void sell(int qty) {
32+
System.out.println("Total: $" + (qty * price));
33+
}
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Testing class for Book class.
3+
*
4+
* @version 1.1
5+
* @author L.Gobinath
6+
*/
7+
public class BookShop {
8+
public static void main(String[] args) {
9+
Book book = new Book();
10+
book.title = "Hamlet";
11+
book.author = "William Shakespeare";
12+
// Still directly assigning the value.
13+
// I am a rule breaker.
14+
book.price = -5.95;
15+
16+
System.out.println("Title: " + book.title);
17+
System.out.println("Price: $" + book.price);
18+
book.sell(2);
19+
}
20+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Book class used to explain the encapsulation.
3+
*
4+
* @version 1.2
5+
* @author L.Gobinath
6+
*/
7+
public class Book {
8+
// Title of the book.
9+
String title;
10+
// Author of the book.
11+
String author;
12+
// Price of the book.
13+
private double price;
14+
15+
/**
16+
* Set the price.
17+
* @param price Price of the book.
18+
*/
19+
public void setPrice(double price) {
20+
if (price > 0) {
21+
this.price = price;
22+
} else {
23+
System.out.println("Invalid price");
24+
}
25+
}
26+
27+
/**
28+
* Get the price.
29+
* @return Price of the book.
30+
*/
31+
public double getPrice() {
32+
return this.price;
33+
}
34+
35+
/**
36+
* Print the total amount for given quantity.
37+
* @param qty The quantity
38+
*/
39+
public void sell(int qty) {
40+
System.out.println("Total: $" + (qty * price));
41+
}
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Testing class for Book class.
3+
*
4+
* @version 1.2
5+
* @author L.Gobinath
6+
*/
7+
public class BookShop {
8+
public static void main(String[] args) {
9+
Book book = new Book();
10+
book.title = "Hamlet";
11+
book.author = "William Shakespeare";
12+
book.setPrice(-5.95); // Error message
13+
book.setPrice(5.95);
14+
15+
System.out.println("Title: " + book.title);
16+
// System.out.println("Price: $" + book.price);
17+
System.out.println("Price: $" + book.getPrice());
18+
book.sell(2);
19+
}
20+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Book class used to explain the encapsulation.
3+
*
4+
* @version 2.0
5+
* @author L.Gobinath
6+
*/
7+
public class Book {
8+
// Title of the book.
9+
private String title;
10+
// Author of the book.
11+
private String author;
12+
// Price of the book.
13+
private double price;
14+
15+
/**
16+
* Get the title.
17+
* @return The title of the book.
18+
*/
19+
public String getTitle() {
20+
return this.title;
21+
}
22+
23+
/**
24+
* Set the title.
25+
* @param title The title of the book.
26+
*/
27+
public void setTitle(String title) {
28+
this.title = title;
29+
}
30+
31+
/**
32+
* Get the author of the book.
33+
* @return The author of the book.
34+
*/
35+
public String getAuthor() {
36+
return this.author;
37+
}
38+
39+
/**
40+
* Set the author of the book.
41+
* @param author The author of the book.
42+
*/
43+
public void setAuthor(String author) {
44+
this.author = author;
45+
}
46+
47+
/**
48+
* Set the price.
49+
* @param price Price of the book.
50+
*/
51+
public void setPrice(double price) {
52+
if (price > 0) {
53+
this.price = price;
54+
} else {
55+
System.out.println("Invalid price");
56+
}
57+
}
58+
59+
/**
60+
* Get the price.
61+
* @return Price of the book.
62+
*/
63+
public double getPrice() {
64+
return this.price;
65+
}
66+
67+
/**
68+
* Print the total amount for given quantity.
69+
* @param qty The quantity
70+
*/
71+
public void sell(int qty) {
72+
System.out.println("Total: $" + (qty * price));
73+
}
74+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Testing class for Book class.
3+
*
4+
* @version 2.0
5+
* @author L.Gobinath
6+
*/
7+
public class BookShop {
8+
public static void main(String[] args) {
9+
Book book = new Book();
10+
book.setTitle("Hamlet");
11+
book.setAuthor("William Shakespeare");
12+
book.setPrice(5.95);
13+
14+
System.out.println("Title: " + book.getTitle());
15+
System.out.println("Price: $" + book.getPrice());
16+
book.sell(2);
17+
}
18+
}

0 commit comments

Comments
 (0)