Java-2 – YO2015 – Hafta – 2

java2_yazokulu_2015_hafta2

 

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.util.*;


public class lamba implements ActionListener {

	JTextField[] jtf;	
	JLabel jlab;
	public lamba() {
		JFrame jfrm = new JFrame("Lamba");
		jfrm.setSize(150,200);
		jfrm.setLayout(new GridLayout(6,1));
		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		jtf= new JTextField[4];
				
		
		for(int i=0;i<jtf.length;i++)
		{
			JPanel jpn = new JPanel();
			JPanel jpn1= new JPanel();
			jpn.setLayout(new GridLayout(1,2));
			JLabel jlab1;
			if(i<3)
			 jlab1= new JLabel("Lamba-"+(i+1));
			else
			 jlab1= new JLabel("Tekrar");
				
			jtf[i]= new JTextField(5);
			jpn1.add(jtf[i]);
			jpn.add(jlab1);
			jpn.add(jpn1);
			jfrm.add(jpn);
		
		}
		JPanel jpn = new JPanel();
		jlab= new JLabel("Butona basınız!");
		jpn.add(jlab);
		jfrm.add(jpn);
		
		jpn = new JPanel();
		JButton jbtn= new JButton("Hesapla");
		jpn.add(jbtn);
		jfrm.add(jpn);
		
		jbtn.addActionListener(this);
		
		
		jfrm.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		
		int enkucuk;
		int carpimlar=1;
		int enbuyuk;
		
		int[] sayilar = new int[3];
		
		try{
			for(int i=0;i<jtf.length-1;i++)
			{
				sayilar[i]=Integer.parseInt(jtf[i].getText());
				carpimlar*=sayilar[i];
			}	
			
			Arrays.sort(sayilar);
			
			enkucuk = sayilar[0];
			enbuyuk = sayilar[2];
			
			
			int bulunan=enkucuk;
			int tekrar=Integer.parseInt(jtf[3].getText());
			
		/*	
		 * birinci yol
		 */
			/*for(int i=enkucuk;i<=carpimlar;i+=enkucuk)
			{
				
				if(i%sayilar[0]==0 && i%sayilar[1]==0 && i%sayilar[2]==0)
				{
					bulunan=i;
					break;
					
				}				
			}
			
			*/
			
			
			// asallar 
			
			bulunan=1;
			
			for(int i=2;i<enbuyuk;i++)
			{
				boolean durum=true;
				for(int j=2;j<=(i/2);j++)
				{
					if(i%j==0)
					{
						durum=false;
						break;
						
					}
					
				}
				if(durum)
				{
	
					while(sayilar[0]%i==0 || sayilar[1]%i==0 || sayilar[2]%i==0)
					{
						for(int a=0;a<sayilar.length;a++)
						{
							if(sayilar[a]%i==0)
								sayilar[a]/=i;
						}
						bulunan*=i;
					}
					
					
					
				}
				
				
				
			}
			
			bulunan*=tekrar;
			
			
			int saniye=bulunan%60;
			int dakika=(bulunan-saniye)/60;
			
			String sonuc=tekrar+ ". tekrar ";
			if(dakika>0)
				sonuc+=dakika+" dk ";
			
			sonuc+=saniye+" sn sonra";
			
			jlab.setText(sonuc);
			
			
			
		}
		catch(Exception ex){
			
		}
		

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new lamba();

	}

}

 

Share