6
Mayıs
2010

Nesneye Yönelik Hesap Makinesi

NYHM, iki tane sayıyı, toplayan, çıkartan, çarpan veya bölen, NetBeans ortamı ile JAVA dilinde geliştirilmiş, Nesneye Yönelik Programlamanın (Object Oriented Programming) temellerine örnek teşkil eden basit bir programcıktır.

Bu programcıkta

  • Encapsulation (?)
  • Inheritence (Kalıtım)
  • Polymorphism (?)

adları verilen OOP’un olmazsa olmaz temel parçalarına basit (ama umuyorum anlaşılır) örnekler bulacaksınız.

Bu kaynak kodda hatalı veri girdilerinin tamamı kontrol edilmemiş olabilir, 0’a bölme ya da benzer hatalar alabilirsiniz. Ama amacınız Nesneye Yönelik Programlama alıştırmaları görmek ise, o zaman bu kod işinize yarayacaktır.

Projeyi aşağıdan indirebilir veya kaynak kodlar için yazının devamına bakabilirsiniz.

Makine.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package makine;
 
public abstract class Makine {
 
   protected int  sayi1 = 0;
   protected int  sayi2 = 0;
 
   /* TÜM ALT SINIFLAR İÇİN ORTAK İŞLEYİŞE SAHİP METOTLAR BURADA YAZILIYOR. */
   public void setSayi1(int sayi1) {
        this.sayi1 = sayi1;
   }
 
   public void setSayi2(int sayi2) {
        this.sayi2 = sayi2;
   }
 
   /* AYNI İŞLEVE SAHİP AMA İŞLEYİŞİ FARKLI METOTLAR, ALT SINIFLARDA YAZILMAK ÜZERE, TANIMLANIYOR KODLANMIYOR */
   public abstract int hesapla();
   /* Abstract (soyut) bir metot, bu metodun alt sınıflarda implement edilmesini zorunlu kılar. */
 
}

Cikar.java

1
2
3
4
5
6
7
8
9
package makine;
 
public class Cikar extends Makine {
 
    public int hesapla() {
        return super.sayi1 - super.sayi2;
    }
 
}

Bol.java

1
2
3
4
5
6
7
8
9
10
11
12
package makine;
 
public class Bol extends Makine {
 
    public int hesapla() {
        if(super.sayi2 != 0) {
            return super.sayi1 / super.sayi2;
        }
        return 0;
    }
 
}

Topla.java

1
2
3
4
5
6
7
8
package makine;
 
public class Topla extends Makine {
 
    public int hesapla() {
        return super.sayi1 + super.sayi2;
    }
}

Carp.java

1
2
3
4
5
6
7
8
9
package makine;
 
public class Carp extends Makine {
 
    public int hesapla() {
        return super.sayi1 * super.sayi2;
    }
 
}

Gorsel.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
231
232
233
234
package makine;
 
public class Gorsel extends javax.swing.JFrame {
 
    private Makine hesMak;
 
    /** Creates new form Gorsel */
    public Gorsel() {
        initComponents();
    }
 
    private int stringToInt(String s) {
 
       Integer dondurulecek = null;
       try {
            dondurulecek = Integer.parseInt(s);
       } catch (NumberFormatException numberFormatException) {
            dondurulecek = 0;
       }
       return dondurulecek;
 
    }
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
 
        jLabel1 = new javax.swing.JLabel();
        birinciSayi = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        ikinciSayi = new javax.swing.JTextField();
        toplaButon = new javax.swing.JButton();
        sonuc = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        carpButon = new javax.swing.JButton();
        bolButon = new javax.swing.JButton();
        cikarButon = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
 
        jLabel1.setLabelFor(birinciSayi);
        jLabel1.setText("Birinci sayı:");
        jLabel1.setToolTipText("ABC");
 
        jLabel2.setLabelFor(ikinciSayi);
        jLabel2.setText("İkinci Sayı:");
 
        toplaButon.setText("+");
        toplaButon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toplaButonActionPerformed(evt);
            }
        });
 
        sonuc.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        sonuc.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        sonuc.setText("sonuç gelecek");
 
        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Hesap Makinesi");
 
        carpButon.setText("X");
        carpButon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                carpButonActionPerformed(evt);
            }
        });
 
        bolButon.setText("/");
        bolButon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bolButonActionPerformed(evt);
            }
        });
 
        cikarButon.setText("-");
        cikarButon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cikarButonActionPerformed(evt);
            }
        });
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(birinciSayi)
                            .addComponent(ikinciSayi)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(toplaButon)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cikarButon)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(carpButon)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bolButon)))))
                .addContainerGap(23, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(sonuc, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
                .addGap(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(birinciSayi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(ikinciSayi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(toplaButon)
                    .addComponent(cikarButon)
                    .addComponent(carpButon)
                    .addComponent(bolButon))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(sonuc, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
 
        pack();
    }// </editor-fold>//GEN-END:initComponents
 
    private void toplaButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toplaButonActionPerformed
       String s1 = birinciSayi.getText();
       String s2 = ikinciSayi.getText();
 
       int sayi1 = stringToInt(s1);
       int sayi2 = stringToInt(s2);
 
       hesMak = new Topla();
 
       hesMak.setSayi1(sayi1);
       hesMak.setSayi2(sayi2);
 
       sonuc.setText(hesMak.hesapla() + "");      
 
    }//GEN-LAST:event_toplaButonActionPerformed
 
    private void carpButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_carpButonActionPerformed
       String s1 = birinciSayi.getText();
       String s2 = ikinciSayi.getText();
 
       int sayi1 = stringToInt(s1);
       int sayi2 = stringToInt(s2);
 
       hesMak = new Carp();
 
       hesMak.setSayi1(sayi1);
       hesMak.setSayi2(sayi2);
 
       sonuc.setText(hesMak.hesapla() + "");
    }//GEN-LAST:event_carpButonActionPerformed
 
    private void bolButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bolButonActionPerformed
       String s1 = birinciSayi.getText();
       String s2 = ikinciSayi.getText();
 
       int sayi1 = stringToInt(s1);
       int sayi2 = stringToInt(s2);
 
       hesMak = new Bol();
 
       hesMak.setSayi1(sayi1);
       hesMak.setSayi2(sayi2);
 
       sonuc.setText(hesMak.hesapla() + "");
    }//GEN-LAST:event_bolButonActionPerformed
 
    private void cikarButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cikarButonActionPerformed
       String s1 = birinciSayi.getText();
       String s2 = ikinciSayi.getText();
 
       int sayi1 = stringToInt(s1);
       int sayi2 = stringToInt(s2);
 
       hesMak = new Cikar();
 
       hesMak.setSayi1(sayi1);
       hesMak.setSayi2(sayi2);
 
       sonuc.setText(hesMak.hesapla() + "");
    }//GEN-LAST:event_cikarButonActionPerformed
 
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Gorsel().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField birinciSayi;
    private javax.swing.JButton bolButon;
    private javax.swing.JButton carpButon;
    private javax.swing.JButton cikarButon;
    private javax.swing.JTextField ikinciSayi;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel sonuc;
    private javax.swing.JButton toplaButon;
    // End of variables declaration//GEN-END:variables
 
}
bu yazı 1.762 defa okundu

Site hoşunuza gitti mi? Belki arkadaşlarınızın da gider.

İstekli

Aaa Reklam

+ Yorumunuzu Ekleyin 5 yorum

  • efe 03 Eylül 2011 16.02

    Merhaba umut bey sitenizde java kodlarını çok düzenli biçimde yazmanıza vesile olan html kodlarını öğrenebilirmiyim. ben kendi sitemde bunu —>>> <div style="overflow: auto; width: 100%; height: 400px; " >" KODLAR </div> <<<<<—- kodu ile yapıyorum ama çok düzensiz ve acemice görünüyo. Nette aramama rağmen pek bişey bulamadım. yardımcı olurmusunuz ?

    • Umut 04 Eylül 2011 21.26

      Merhaba,

      WordPress için kod renklendirici eklentiler var. Onlardan birini kullanıyorum. Yani kendim html kodları ile düzenlemiyorum.

      • efe 04 Eylül 2011 23.54

        Hımmm Teşekkür ederim cevabınız için.

  • Recep 12 Aralık 2011 21.18

    Merhaba Umut Bey,

    Bir ödevimle ilişkili bir sorum olacaktı. Geometrik şekillerin sınıflarını tanımlayıp içine gereken değerlerinin tanımlamasını yaptım fakat bir türlü GUI' de butonlarla birleştiremedim. Yardımcı olabilir misiniz?

    • Umut 12 Aralık 2011 22.43

      Merhaba,

      Yardımcı olmak isterdim, ancak ne yazık ki uzuuuuun bir süredir JAVA'da arayüz kütüphanelerinin hiçbirini kulanmıyorum. Ancak genel olarak özetlemem gerekirse yapmanız geren iş, GUI'deki butona bir tane "click" "event"i bind etmek ve bu event gerçekleşince çalışacak bir kod parçasını event handlera yazmak. Bu kod parçası, sizin yaptırmak istediğiniz iş olmalı.

Yorumunuzu Bırakın

Bu yazıya gönderilen yeni yorumları e-posta aracılığıyla bana bildir
Yeni gönderilenleri yorum yapmadan takip etmek için tıklayınız.

Yorumunuz başarıyla alındı. Onaylandıktan sonra yayımlanacaktır. Teşekkürler.

Twitler yükleniyor... 5 saniye sonra

Bıdı bıdı bıdı bıdı dıdı dıdı dudu dudu hıdı hıdı hödü hödü yüklüyoruz öhüm öhüm bıdı bıdı vs vs... 6 nanosaniye önce

Yüklenmenin geç olmasının sebebi ben değilim, Twitter API'sinin yavaş olması. Gudu gudu hıdı hödö büdü büdü... 25697 asır önce

Ha tabi bunları okumuşsan, bu sitenin çok gizli bir özelliğini bulmuşsun demektir. ;) Tebrikler. Bu "sürpiz yumurta"yı bulduğunu bana da haber verir misin? Tıkla! 6 dinazor önce

Geçen Yıllarda Bu Hafta

2011

Bunun Burada Ne İşi Var?

Bunun Burada Ne İşi Var?

Dün şehre inmek için Sayın Menderes Türel’in zamanında Hafif Metro ...

Windows 7’de Bilgisayarınızın Aldığı Puanı Değiştirin

Windows 7’de Bilgisayarınızın Aldığı Puanı Değiştirin

Biliyorsunuz Microsoft, Windows Vista’dan bu yana bilgisayarlar için bir performans ...

Dördüncü Sınıfın Birinci Döneminden Öğrenci Görüşleri

Dördüncü Sınıfın Birinci Döneminden Öğrenci Görüşleri

Dördüncü sınıfın yarısı bitti. Okuldan mezun olmak üzereyim. İyisiyle kötüsüyle bir ...

UBenzer’den Alın!

UBenzer’den Alın!

Ablam evdeki kullanılmayanları ayırmış, “Umut bunları sat.” dedi. Hazır elime ...

2009

Kısık Işık

Kısık Işık

Tavana asılmış tek beyaz floresan lambayı sevemedim bir türlü… “Ben ...

Antalya Toplu Taşıma Sisteminin Sorunları - 1

Antalya Toplu Taşıma Sisteminin Sorunları - 1

Antalya’da ulaşım bir ölüm. Trafik sıkışıklığı, haftada bir yönü değişen ...

2008

14 Şubat

14 Şubat

Biliyorsun bugün 14 Şubat. Daha iki gün öncesinden hazırdı zaten ...

Uyumadan Önce Son Boşluk

Uyumadan Önce Son Boşluk

Uykuya dalmadan önce düşünürüm… Kötü alışkanlıklarımdan biridir. Aklıma ne gelirse ......

NES Emulatörleri

NES Emulatörleri

Daha önceki şu iki yazımda (1.si, 2.si), çocukken bolca oynadığımız ...

Son Yorumlar