-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencapsulation_example.java
230 lines (187 loc) · 4.92 KB
/
encapsulation_example.java
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package first_test;
import java.util.Scanner;
//kedi nesnemiz
class Cat{
public String name;
public String color;
private int age;
int hungry;
void eatFood(int foodVal) {
this.hungry += foodVal;
}
//main constructor
public Cat() {
this.name = "isim degeri girilmedi.";
this.color = "renk degeri girilmedi.";
this.age = 0;
this.hungry = 0;
}
//parameters constructor
public Cat(String name, String color, int age, int hungry) {
this.name = name;
this.color = color;
this.age = age;
this.hungry = hungry;
}
//getter age
public int getAge() {
return age;
}
//setter age
public void setAge(int age) {
this.age = age;
}
}
//kitap nesnemiz
class Book{
private String name;
private String author;
private double price;
private int pageNumb;
//main constructor
public Book() {
this.name = "name değeri girilmedi";
this.author = "yazar değeri girilmedi";
this.price = 0.0;
this.pageNumb = 0;
}
//param constructor
public Book(String name, String author, double price, int pageNumb) {
this.name = name;
this.author= author;
this.price = price;
this.pageNumb = pageNumb;
}
//getter all variable
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
public int getPageNumb() {
return pageNumb;
}
//setter all element
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(double price) {
this.price = price;
}
public void setPageNumb(int pageNumb) {
this.pageNumb = pageNumb;
}
//her nesne için tek tek yazdırma yapmak yerine, method belirleyerek
//tek kod içerisinde birden fazla işlemi yapabiliriz.
public void book_info_io() {
System.out.println("Kitap adı: " + this.name
+ "\nKitap yazarı: " + this.author
+ "\nKitap fiyatı: " + this.price
+ "\nKitap sayfa sayısı: " + this.pageNumb );
}
}
//bilgisayar nesnemiz
class Computer{
private String brand;
private String processor_name;
private double processor_ghz;
private int ram;
//main(default) constructor
public Computer() {
this.brand = "Marka değeri girilmedi";
this.processor_name = "İşlemci değeri girilmedi";
this.processor_ghz = 0.0;
this.ram = 0;
}
//params constructor
public Computer(String brand, String processor_name, double processor_ghz, int ram) {
this.brand = brand;
this.processor_name = processor_name;
this.processor_ghz = processor_ghz;
this.ram = ram;
}
//getter all element
public String getBrand() {
return brand;
}
public String getProcessorName() {
return processor_name;
}
public double getProcessorGHZ() {
return processor_ghz;
}
public int getRam() {
return ram;
}
//setter all element
public void setBrand(String brand) {
this.brand = brand;
}
public void setProcessorName(String processor_name) {
this.processor_name = processor_name;
}
public void setProcessorGHZ(double processor_ghz) {
this.processor_ghz = processor_ghz;
}
public void setRam(int ram) {
this.ram = ram;
}
//her nesne için tek tek yazdırma yapmak yerine, method belirleyerek
//tek kod içerisinde birden fazla işlemi yapabiliriz.
public void io_variable() {
System.out.println("\nbook3 nesnesinin özellikleri: ");
System.out.println(this.brand + "\n"
+ this.processor_name + "\n"
+ this.processor_ghz + "\n"
+ this.ram);
}
}
public class encapsulation_example {
public static void main(String[] args) {
// TODO encapsulation examples
//kedi nesnemiz
Cat cat1 = new Cat();
cat1.setAge(2);
System.out.println(cat1.getAge());
//kitap nesnemiz
Book book1 = new Book();
Book book2 = new Book();
book1.setName("Fırat Suyu Kan Akıyor Baksana");
book1.setAuthor("Yaşar Kemal");
book1.setPrice(13.33);
book1.setPageNumb(318);
System.out.println("\nbook1 nesnesinin özellikleri:");
book1.book_info_io();
book2.setName("Devlet Ana");
book2.setAuthor("Kemal Tahir");
book2.setPrice(22.78);
book2.setPageNumb(651);
System.out.println("\nbook2 nesnesinin özellikleri:");
System.out.println("Kitap adı: " + book2.getName() +
"\nKitap yazarı: " + book2.getAuthor() +
"\nKitap fiyatı: " + book2.getPrice() +
"\nKitap sayfa sayısı: " + book2.getPageNumb());
//bilgisayar nesnesi
Computer comp1 = new Computer();
Scanner input = new Scanner(System.in);
System.out.println("\nYeni bir bilgisayar nesnesi olusturabilmek için, aşağıdaki bilgileri giriniz.");
System.out.print("Bilgisayar markasi: ");
comp1.setBrand(input.nextLine());
System.out.print("Bilgisayar işlemci modeli: ");
comp1.setProcessorName(input.nextLine());
System.out.print("İşlemci dönüş hızı: ");
comp1.setProcessorGHZ(input.nextDouble());
System.out.print("ram boyutu: ");
comp1.setRam(input.nextInt());
comp1.io_variable();
input.close();
System.exit(0);
}
}