Nesneye Yönelik Hesap Makinesi

nesneye-yonelik-hesap-makinesi nesneye-yonelik-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

 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

 package makine;

public class Cikar extends Makine {

public int hesapla() { return super.sayi1 - super.sayi2; }

}

Bol.java

 package makine;

public class Bol extends Makine {

public int hesapla() { if(super.sayi2 != 0) { return super.sayi1 / super.sayi2; } return 0; }

}

Topla.java

 package makine;

public class Topla extends Makine {

public int hesapla() { return super.sayi1 + super.sayi2; } }

Carp.java

 package makine;

public class Carp extends Makine {

public int hesapla() { return super.sayi1 * super.sayi2; }

}

Gorsel.java

 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

}