Java-2 – YO2015 – Hafta – 4

java2_yo2015_resim1

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ornek1 implements ActionListener {

	DefaultListModel<String> lad, lvize, lfinal, lort, lharf;
	JList<String> jls1, jls2, jls3, jls4, jls5;
	JTextField jtf_ad, jtf_vize, jtf_final;
	JLabel jlab_gec, jlab_kal;

	String[] harfler = { "AA", "BA", "BB", "CB", "CC", "DC", "DD", "FD" };
	int[] sinirlar = { 90, 80, 70, 60, 50, 40, 35, 30 };

	public ornek1() {
		JFrame jfrm = new JFrame("Hafta-4, Not Listesi");
		jfrm.setSize(550, 240);
		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jfrm.setLayout(new GridLayout(1, 5));

		JPanel jpn = new JPanel();
		jfrm.add(jpn);
		JLabel jlab = new JLabel("Ad");
		jpn.add(jlab);

		lad = new DefaultListModel<String>();
		jls1 = new JList<String>(lad);
		JScrollPane jscp1 = new JScrollPane(jls1);
		jscp1.setPreferredSize(new Dimension(100, 130));
		jpn.add(jscp1);
		jtf_ad = new JTextField(9);
		jpn.add(jtf_ad);
		JButton jbtn = new JButton("Ekle");
		jbtn.setActionCommand("ekle");
		jbtn.addActionListener(this);
		jpn.add(jbtn);

		jpn = new JPanel();
		jfrm.add(jpn);
		jlab = new JLabel("Vize");
		jpn.add(jlab);
		lvize = new DefaultListModel<String>();
		jls2 = new JList<String>(lvize);
		jscp1 = new JScrollPane(jls2);
		jscp1.setPreferredSize(new Dimension(100, 130));
		jpn.add(jscp1);
		jtf_vize = new JTextField(9);
		jpn.add(jtf_vize);
		jbtn = new JButton("Sil");
		jbtn.addActionListener(this);
		jpn.add(jbtn);

		jpn = new JPanel();
		jfrm.add(jpn);
		jlab = new JLabel("Final");
		jpn.add(jlab);
		lfinal = new DefaultListModel<String>();
		jls3 = new JList<String>(lfinal);
		jscp1 = new JScrollPane(jls3);
		jscp1.setPreferredSize(new Dimension(100, 130));
		jpn.add(jscp1);
		jtf_final = new JTextField(9);
		jpn.add(jtf_final);

		jpn = new JPanel();
		jfrm.add(jpn);
		jlab = new JLabel("Ortalama");
		jpn.add(jlab);
		lort = new DefaultListModel<String>();
		jls4 = new JList<String>(lort);
		jscp1 = new JScrollPane(jls4);
		jscp1.setPreferredSize(new Dimension(100, 130));
		jpn.add(jscp1);
		jlab_gec = new JLabel("Geçen: 0");
		jpn.add(jlab_gec);

		jpn = new JPanel();
		jfrm.add(jpn);
		jlab = new JLabel("Harf");
		jpn.add(jlab);
		lharf = new DefaultListModel<String>();
		jls5 = new JList<String>(lharf);
		jscp1 = new JScrollPane(jls5);
		jscp1.setPreferredSize(new Dimension(100, 130));
		jpn.add(jscp1);
		jlab_kal = new JLabel("Kalan: 0");
		jpn.add(jlab_kal);

		jfrm.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {

		if (ae.getActionCommand().equals("ekle")) {
			// ekleme
			try {

				if (jtf_ad.getText().length() > 0
						&& jtf_vize.getText().length() > 0
						&& jtf_final.getText().length() > 0) {
					int vizenot = Integer.parseInt(jtf_vize.getText());
					int finalnot = Integer.parseInt(jtf_final.getText());

					if (vizenot >= 0 && vizenot <= 100 && finalnot >= 0
							&& finalnot <= 100) {
						double ort = vizenot * 0.4 + finalnot * 0.6;

						int not_ort = (int) Math.round(ort); // yuvarlama

						lad.addElement(jtf_ad.getText());
						lvize.addElement(jtf_vize.getText());
						lfinal.addElement(jtf_final.getText());
						lort.addElement(ort + "");
						lharf.addElement(harfhesapla(not_ort));
						jtf_ad.setText("");
						jtf_vize.setText("");
						jtf_final.setText("");
					}

				}

			} catch (Exception ex) {

			}

		} else {
			// silme
			int secilen=jls1.getSelectedIndex();
			if(secilen>-1)
			{
				lad.remove(secilen);
				lvize.remove(secilen);
				lfinal.remove(secilen);
				lort.remove(secilen);
				lharf.remove(secilen);
			}			
		}
		gecenkalan();
	}

	private void gecenkalan()
	{
		int gecen=0;
		int kalan=0;
		
		for(int i=0;i<lort.size();i++)
		{
			
			double ort = Double.parseDouble(lort.get(i));
			int not_ort=(int)Math.round(ort);
			
			if(not_ort>=50)
				gecen++;
			else
				kalan++;
			
		}
		
		
		jlab_gec.setText("Geçen: "+gecen);
		jlab_kal.setText("Kalan: "+kalan);
	}
	private String harfhesapla(int ort) {
		for (int i = 0; i < sinirlar.length; i++) {
			if (ort >= sinirlar[i])
				return harfler[i];
		}
		return "FF";
	}

	public static void main(String[] args) {
		new ornek1();
	}

}

 

java2_yo2015_resim2

 

import java.awt.event.*;
import java.awt.*;
import java.util.Arrays;

import javax.swing.*;

public class ornek2 implements ActionListener {

	JRadioButton[] jrb = new JRadioButton[6];
	JTextField jtf;
	String[] hex = { "A", "B", "C", "D", "E", "F" };

	public ornek2() {
		JFrame jfrm = new JFrame("Hafta-4, Dönüşüm");
		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jfrm.setLayout(new FlowLayout());
		jfrm.setSize(200, 200);

		jtf = new JTextField(15);
		jfrm.add(jtf);

		String[] icerik = { "İkilik", "Onluk", "Onaltılık" };

		JPanel jpn = new JPanel();
		jpn.setLayout(new GridLayout(3, 2));

		jfrm.add(jpn);
		ButtonGroup bg1 = new ButtonGroup();
		ButtonGroup bg2 = new ButtonGroup();

		for (int i = 0; i < 3; i++) {
			jrb[i] = new JRadioButton(icerik[i]);
			bg1.add(jrb[i]);
			jpn.add(jrb[i]);
			jrb[i + 3] = new JRadioButton(icerik[i]);
			bg2.add(jrb[i + 3]);
			jpn.add(jrb[i + 3]);

		}

		jrb[0].setSelected(true);
		jrb[3].setSelected(true);

		JButton jbtn = new JButton("Dönüştür");
		jbtn.addActionListener(this);
		jfrm.add(jbtn);

		jfrm.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		int[] secilen = { 0, 0 };
		int k = 0;
		for (int i = 0; i < jrb.length; i++) {
			if (jrb[i].isSelected())
				secilen[k++] = i;
		}

		if (jtf.getText().length() > 0) {

			String veri = jtf.getText().toUpperCase();

			if (!kontrol(secilen[0], veri))
				JOptionPane.showMessageDialog(null,
						"Girilen sayı seçilen formata uygun değil");
			else {

				String sonuc = "";

				if (secilen[0] == 0 && secilen[1] == 4) {
					// ikilik --> onluk
					sonuc = bin2dec(veri);
				}
				if (secilen[0] == 1 && secilen[1] == 3) {
					// onluk --> ikilik
					sonuc = dec2bin(veri);
				}
				if (secilen[0] == 1 && secilen[1] == 5) {
					// onluk --> onaltilik
					sonuc = dec2hex(veri);
				}
				if (secilen[0] == 2 && secilen[1] == 4) {
					// onaltilik --> onluk
					sonuc = hex2dec(veri);
				}
				if (secilen[0] == 0 && secilen[1] == 5) {
					// ikilik --> onaltilik
					sonuc = bin2dec(veri);
					sonuc = dec2hex(sonuc);
				}
				if (secilen[0] == 2 && secilen[1] == 3) {
					//onaltilik --> ikilik
					sonuc = hex2dec(veri);
					sonuc = dec2bin(sonuc);
				}

				jtf.setText(sonuc);
			}

		}
	}

	private String hex2dec(String deger) {

		int sonuc = 0;
		for (int index = 0; index < deger.length(); index++) {

			int ind = Arrays.binarySearch(hex, (deger.charAt(index) + ""));

			int sayi = 0;
			if (ind > -1)
				sayi = 10 + ind;
			else
				sayi = Integer.parseInt(deger.charAt(index) + "");

			sonuc += sayi * (int) Math.pow(16, deger.length() - index - 1);
		}

		return sonuc + "";
	}

	private String dec2hex(String deger) {
		String sonuc = "";
		int veri = Integer.parseInt(deger);

		while (veri > 0) {
			int kalan = veri % 16;
			veri = (veri - kalan) / 16;

			if (kalan > 9)
				sonuc = hex[kalan % 10] + sonuc;
			else
				sonuc = kalan + sonuc;
		}

		return sonuc;
	}

	private String dec2bin(String deger) {

		String sonuc = "";
		int veri = Integer.parseInt(deger);

		while (veri > 0) {
			int kalan = veri % 2;
			veri = (veri - kalan) / 2;
			sonuc = kalan + sonuc;
		}
		return sonuc;

	}

	private String bin2dec(String deger) {
		int sonuc = 0;
		for (int index = 0; index < deger.length(); index++) {

			int sayi = Integer.parseInt(deger.charAt(index) + "");

			sonuc += sayi * (int) Math.pow(2, deger.length() - index - 1);
		}

		return sonuc + "";

	}

	private boolean kontrol(int tip, String deger) {
		boolean durum = true;

		for (int index = 0; index < deger.length(); index++) {

			String veri = deger.charAt(index) + "";

			if (tip == 0) {

				if (!(veri.equals("0") || veri.equals("1")))
					return false;

			} else if (tip == 1) {
				try {
					Integer.parseInt(veri); // sayiya donustur donusmezse sayi
											// degil
				} catch (Exception ex) {
					return false;
				}

			} else {
				// onaltilik

				if (!(veri.equals("A") || veri.equals("B") || veri.equals("C")
						|| veri.equals("D") || veri.equals("E") || veri
							.equals("F"))) {

					try {
						Integer.parseInt(veri); // sayiya donustur donusmezse
												// sayi
												// degil
					} catch (Exception ex) {
						return false;
					}

				}

			}
		}

		return durum;
	}

	public static void main(String[] args) {

		new ornek2();

	}

}

 

 

Share