20 Ekim 2012 Cumartesi

Client - Server



Server.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package talk;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

/**
 *
 * @author Onur
 */
public class Server extends JFrame{

    private JTextField enterField;
    private JTextArea displayArea;
    private ObjectOutputStream output;
    private ObjectInputStream input;
    private ServerSocket server;
    private Socket connection;
    private int counter;


    public Server()
    {
        super("Server");
        enterField = new JTextField();
        enterField.setEditable(true);
        enterField.addActionListener(

                new ActionListener() {

            public void actionPerformed(ActionEvent event) {
              sendData(event.getActionCommand());
              enterField.setText("");
            }
        }
                );

                add(enterField, BorderLayout.NORTH);

                displayArea = new JTextArea();
                add(new JScrollPane(displayArea), BorderLayout.CENTER);

                setSize(300,150);
                setVisible(true);  
    }

    public void runServer()
    {
        try {
            server = new ServerSocket(12345, 100);

            while(true)
            {
                try {
                    waitForConnection();
                    getStreams();
                    processConnection();
                }
                catch (EOFException eofException)
                {
                    displayMessage("\n Server terminated message");
                }
                finally
                {
                    closeConnection();
                    ++counter;
                }
            }
        }
        catch (IOException ioException)
        {
            ioException.printStackTrace();
        }
    }

    private void waitForConnection() throws IOException
    {

        displayMessage("Waitinf for connection");
        connection = server.accept();
        displayMessage("Connection " + counter + " received from " + connection.getInetAddress().getHostName());
    }

    private void getStreams() throws IOException
    {
        output = new ObjectOutputStream(connection.getOutputStream());
        output.flush();

        input = new ObjectInputStream(connection.getInputStream());

        displayMessage(" got i/o streams");
    }

    private void processConnection() throws IOException
    {
        String message = "Connection successful";
        sendData(message);

        setTextFieldEditable(true);

        do
        {
            try {
                 message = (String) input.readObject();
                displayMessage("\n" + message);
            }
            catch (ClassNotFoundException classNotFoundException)
            {
                displayMessage("Unknow object type received");
            }
        }while(!message.equals("Client >>> Terminate"));
    }

    private void closeConnection()
    {
        displayMessage("Terminatind connection");
        setTextFieldEditable(true);

        try {
            output.close();
            input.close();
            connection.close();

        } catch (IOException ioException)
        {
            ioException.printStackTrace();
        }
    }

    private void displayMessage(final String messageToDisplay)
    {
        SwingUtilities.invokeLater(
             
                new Runnable() {

            public void run() {
              displayArea.append(messageToDisplay);
            }
        }
                );
    }

    private void sendData(String message)
    {
        try {
           output.writeObject("Server >>> " + message);
           output.flush();
           displayMessage("\n Server >>> " + message);

        } catch (IOException ioException)
        {
            displayArea.append("\n error waiting object");
        }
    }

    private void setTextFieldEditable(final boolean editable) {

         SwingUtilities.invokeLater(

                new Runnable() {

            public void run() {
              enterField.setEnabled(editable);
            }
        }
                );
    }

    public static void main(String args[])
    {
        Server application = new Server();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.runServer();
    }

}

Client.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package talk;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
/**
 *
 * @author Onur
 */
public class Client extends JFrame {

    private JTextField enterField;
    private JTextArea displayArea;
    private ObjectOutputStream output;
    private ObjectInputStream input;
    private String message = "";
    private String chatServer;
    private Socket client;
   
    public Client(String host)
    {
        super("Client");

        chatServer = host;

        enterField = new JTextField();
        enterField.setEditable(true);
        enterField.addActionListener(

                new ActionListener() {

            public void actionPerformed(ActionEvent event) {
              sendData(event.getActionCommand());
              enterField.setText("");
            }
        }
                );

                add(enterField, BorderLayout.NORTH);

                displayArea = new JTextArea();
                add(new JScrollPane(displayArea), BorderLayout.CENTER);

                setSize(300,150);
                setVisible(true);
    }

    public void runClient()
    {
  
           try
           {
                 connectToServer();
                    getStreams();
                    processConnection();
                }
                catch (EOFException eofException)
                {
                    displayMessage("\n Client terminated message");
                }
                catch (IOException ioException)
                 {
                   ioException.printStackTrace();
                  }
                finally
                {
                    closeConnection();                  
                }
                 
    }

    private void getStreams() throws IOException
    {
        output = new ObjectOutputStream(client.getOutputStream());
        output.flush();

        input = new ObjectInputStream(client.getInputStream());

        displayMessage(" got i/o streams");
    }

    private void processConnection() throws IOException
    {
       
        setTextFieldEditable(true);

        do
        {
            try {
                 message = (String) input.readObject();
                displayMessage("\n" + message);
            }
            catch (ClassNotFoundException classNotFoundException)
            {
                displayMessage("Unknow object type received");
            }
        }while(!message.equals("Server >>> Terminate"));
    }

    private void closeConnection()
    {
        displayMessage("Closing connection");
        setTextFieldEditable(true);

        try {
            output.close();
            input.close();
            client.close();

        } catch (IOException ioException)
        {
            ioException.printStackTrace();
        }
    }

    private void displayMessage(final String messageToDisplay)
    {
        SwingUtilities.invokeLater(

                new Runnable() {

            public void run() {
              displayArea.append(messageToDisplay);
            }
        }
                );
    }

    private void sendData(String message)
    {
        try {
           output.writeObject("Client >>> " + message);
           output.flush();
           displayMessage("\n Client >>> " + message);

        } catch (IOException ioException)
        {
            displayArea.append("\n error waiting object");
        }
    }

    private void setTextFieldEditable(final boolean editable) {

         SwingUtilities.invokeLater(

                new Runnable() {

            public void run() {
              enterField.setEnabled(editable);
            }
        }
                );
    }

    private void connectToServer() throws IOException
    {
         displayMessage("Attempting connection");
         client = new Socket(InetAddress.getByName(chatServer),12345);
         displayMessage("Connected to " + client.getInetAddress().getHostName());
    }

    public static void main(String args[])
    {
        Client application;

        if(args.length == 0)
        {
            application = new Client("127.0.0.1");
        }
        else
        {
            application = new Client(args[0]);
        }

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.runClient();
    }

}

14 Ekim 2012 Pazar

Adjacency List


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class List{
    int node;
    List next;
}

class Node {
    int node;
    List adjList;
boolean visit;
}

public class Adjacency {
    void insertVertices() throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Graph taki düğüm sayısını girin: ");
        int n=(Integer.parseInt(br.readLine()));
        Node g[]=new Node[n];
        for (int i = 0; i < n; i++) {
            g[i]=new Node();
            g[i].node=i+1;
            g[i].visit=false;
            while(true)
            {
                System.out.println("Diğer dugume geçmek için 0(Sıfır) girin");
                System.out.println((i+1)+". Düğümün bitişik düğümlerini girin");
                 int adjVertex=Integer.parseInt((br.readLine()));
                 if(adjVertex==0)
                     break;
                 else
                 {
                        List l=new List();
                        l.node=adjVertex;
                        l.next=null;
                        if(g[i].adjList==null)
                        {
                            g[i].adjList=l;
                        }
                        else
                        {
                            List p=g[i].adjList;
                            while(p.next!=null){
                                p=p.next;
                            }
                            p.next=l;
                        }
                 }
            }
        }
        System.out.println("Adjacency matrix representation");
        for (int i = 0; i < n; i++) {
            System.out.print(g[i].node+" --> ");
            List l=g[i].adjList;
            while(l!=null)
            {
                System.out.print(l.node+" --> ");
                l=l.next;
            }
            System.out.println("");
        }
    }

    public static void main(String[] args) throws IOException {
             new Adjacency().insertVertices();
    }

}

11 Ekim 2012 Perşembe

Basic WebSite Example

WebForm1.aspx


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Onur Turan</title>
    <link href="styles.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">

        var image1 = new Image()
        image1.src = "images/Desert.jpg"
        var image2 = new Image()
        image2.src = "images/Koala.jpg"
        var image3 = new Image()
        image3.src = "images/Tulips.jpg"

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="sayfa">

    <div id="banner">
 
        <div id="bannersol">

        <img src="images/Desert.jpg" name="slide" width="1100px" height="200px" />
<script>


    var step = 1
    function slideit() {
     
        if (!document.images)
            return
        document.images.slide.src = eval("image" + step + ".src")
        if (step < 3)
            step++
        else
            step = 1
     
        setTimeout("slideit()", 2500)
    }
    slideit()

</script>
     
        </div>
        <div id="bannersag">
        <%-- <script language="javascript" src="http://in.sitekodlari.com/saat6.js"></script>--%>
       <center><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="200" height="200"><param name="movie" value="http://www.simresim.com/resim/flash-takvim-saat/saat-007.swf"><param name="quality" value="high"><embed src="http://www.simresim.com/resim/flash-takvim-saat/saat-007.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="200" height="200"></embed></object><br/><a href="http://www.ben10-oyunlari.org" target="_blank">benten</a></center>
        </div>

        </div>

        <div id="menu">
           <ul id="navigation-1">
        <li><a href="#" title=".NET">.NET</a></li>
        <li><a href="#" title="Java">Java</a>
            <ul class="navigation-2">
                <li><a href="#" title="Patterns">Patterns</a></li>
                <li><a href="#" title="Data Structures">Data Structures<span>&raquo;</span></a>
                    <ul class="navigation-3">
                        <li><a href="#" title="Stack">Stack</a></li>
                        <li><a href="#" title="Linked List">Linked List</a></li>
                    </ul>
                </li>
                <li><a href="#" title="v">Gui</a></li>
                <li><a href="#" title="Oop">Oop<span>&raquo;</span></a>
                    <ul class="navigation-3">
                        <li><a href="#" title="Polymorphism">Polymorphism</a></li>
                        <li><a href="#" title="Abstract">Abstract</a></li>
                        <li><a href="#" title="Inheritance">Inheritance</a></li>
                    </ul>
                </li>
            </ul>
        </li>
        <li><a href="#" title="BilMuh">BilMuh</a>
            <ul class="navigation-2">
                <li><a href="#" title="Software">Software</a></li>
                <li><a href="#" title="Hardware">Hardware</a></li>
            </ul>
        </li>
        <li><a href="#" title="Help">Yardım</a>
            <ul class="navigation-2">
                <li><a href="#" title="FAQs">S.S.S.</a></li>
                <li><a href="#" title="Forum">Forum</a></li>
                <li><a href="#" title="Contact Us">İletişim</a></li>
            </ul>
        </li>
        <li><a href="#" title="İletişim">İletişim</a>
         </li>
    </ul>
        </div>

        <div id="orta">

        <div id="icerik">
            Lorem Ipsum is simply dummy text of the printing and typesetting industry.
          Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
          when an unknown printer took a galley of type and scrambled it to make a type specimen book.
          It has survived not only five centuries, but also the leap into electronic typesetting,
          remaining essentially unchanged.
          It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
          and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
         
            Contrary to popular belief, Lorem Ipsum is not simply random text.
          It has roots in a piece of classical Latin literature from 45 BC,
          making it over 2000 years old. Richard McClintock,
          a Latin professor at Hampden-Sydney College in Virginia,
          looked up one of the more obscure Latin words, consectetur,
          from a Lorem Ipsum passage, and going through the cites of the word in classical literature,
          discovered the undoubtable source.
          Lorem Ipsum comes from sections 1.10.32
          and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero,
          written in 45 BC. This book is a treatise on the theory of ethics,
          very popular during the Renaissance.
          The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..",
          comes from a line in section 1.10.32.
          The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested.
          Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form,
          accompanied by English versions from the 1914 translation by H. Rackham.
     
        </div>
        <div id="araclar">
          <script type="text/javascript">
              var system = "siteneekle";
              var para_birimleri = "USD-EUR-GBP-CAD-AUD";
              var arkaplan = "FFFFFF";
              var baslik = "FFFFFF";
              var cerceve = "333333";
              var turbaslik = "999999";
              var kutucuk = "FFFFFF";
              var fiyat = "CC3300";
              var genislik = "462";
              var konum = "left";
              var yatay = "3";
              var slide = "yok";
              var dikey = "0";
              var paylasim = "kapali";
</script>
<a id="altin_doviz_kuru_ekle" target="_blank"  href="http://www.altin.net.tr/">altın</a>
<script type="text/javascript" charset="UTF-8" src="http://www.altin.net.tr/js/doviz2.js"></script>

        </div>

        </div>

        <div id="footer">

        <div id = footersol>
        <center><table border="0"><tr><td bgcolor="#000000">
        <p align="center"><!--1--><iframe src="http://www.aktifhaber.com/sondakika/index1.html" height="200" width="150" frameborder="0" scrolling="No"></iframe><!--2--><br><font size="1"><a href="http://www.simresim.com/sitene-kod-ekle/haber-gazete" style="text-decoration: none" target="_blank">sitene haber ekle</a> - <a href="www.1oyunlar1.net/kategori/3/Ok-Oyunlari" style="text-decoration: none" target="_blank">ok oyunları</a></font></p></td></tr></table></center>
        </div>

        <div id= footerorta>

        <div id=footerortasol>
        <marquee align="middle" scrollamount="1" height="60" width="100%" direction="down"scrolldelay="1">Syn. Gündüz, buraya baya bi bişeyler koydum video, mp3 çalar, ot, bok ... Ama kayan yazı burda iyi oldu böyle bi duyuru modunda falan :)</marquee><br><a href="http://www.kiz-oyunlari.com/kategori/8/Kuafor-Oyunlari"><font size="1"></font></a><br>


        <object width="610" height="144"><embed src="http://www.tarihtebugun.org/bidibidi_design/tarihtebugunorg_gazeteler.swf?x=http://www.tarihtebugun.org/bidibidi_design/tarihtebugunorg_gazeteler.php" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="610" height="120"></embed></object>


        </div>
     
        <%--<script language="javascript" src="http://in.sitekodlari.com/saat6.js"></script>--%>
        <div id=footerortasag>
        Döviz işlemlerinizde hesap makinesini kullanın!
         <script language="javascript" src="http://in.sitekodlari.com/hesap.js"></script>
        </div>

        </div>

        <div id="taban">
        <FONT COLOR="gray" > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tüm hakları saklıdır. Barış Gündüz'e ithafen...
        </div>
       
        </div>
    </div>
    </form>
</body>
</html>


styles.css


#sayfa
{
 
    width: 100%;
    height: 768px;
}

#banner
{
 
    width: 1300px;
    height: 200px;
    margin: auto;
}
#bannersol
{
    float:left;
 
    width: 1100px;
    height: 200px;
 
}

#bannersag
{
    background-color:Olive;
    float:right;
     width: 200px;
    height: 200px;
}



#menu
{
 
    width: 1300px;
    height: 40px;
    margin: auto;
}
#orta
{
    width: 1300px;
    height: 200px;
    margin: auto;
}

#icerik
{
 
    float:left;
 
    width: 1000px;
    height: 200px;
    overflow:auto;

}

#araclar
{
    float:right;
 
    width: 300px;
    height: 200px;
    overflow:auto;
 
   
}

#footer
{
    clear : both;
    margin : auto;
    width: 1300px;
 
    height: 268px;
}

#footersol
{
    float : left;
    height: 268px;
    width: 300px;
 
}

#footerorta
{
    float:right;
    height: 268px;
    width : 1000px;
 
}

#footerortasol
{
    float : left;
    height: 268px;
    width: 700px;
    overflow:auto;
}

#footerortasag
{
    float :right;
    height: 268px;
    width: 300px;  
    overflow:auto;
}

#taban
{
    width: 1300px;
    height: 40px;
    margin: auto;
    background-color:Black;
    clear:both;
    font-style:italic;
    font-family:Times New Roman;
 
 
}
 

ul#navigation-1
{
    margin: 0;
    padding: 1px 0;
    list-style: none;
    width: 100%;
    height: 36px;
    border-top: 1px solid #b9121b;
    border-bottom: 1px solid #b9121b;
    font: normal 12pt verdana, arial, helvetica;
}
ul#navigation-1 li
{
    margin: 0;
    padding: 0;
    display: block;
    float: left;
    position: relative;
    width: 260px;
}

ul#navigation-1 li a:link, ul#navigation-1 li a:visited
{
    padding: 4px 0;
    display: block;
    text-align: center;
    text-decoration: none;
    background: #b9121b;
    color: #ffffff;
    width: 260px;
    height: 28px;
}

ul#navigation-1 li:hover a, ul#navigation-1 li a:hover, ul#navigation-1 li a:active
{
    padding: 4px 0;
    display: block;
    text-align: center;
    text-decoration: none;
    background: #ec454e;
    color: #ffffff;
    width: 260px;
    height: 28px;
    border-left: 1px solid #ffffff;
    border-right: 1px solid #ffffff;
}

ul#navigation-1 li ul.navigation-2
{
    margin: 0;
    padding: 1px 1px 0;
    list-style: none;
    display: none;
    background: #ffffff;
    width: 260px;
    position: absolute;
    top: 21px;
    left: -1px;
    border: 1px solid #b9121b;
    border-top: none;
}
ul#navigation-1 li:hover ul.navigation-2
{
    display: block;
}
ul#navigation-1 li ul.navigation-2 li
{
    width: 260px;
    clear: left;
    width: 260px;
}

ul#navigation-1 li ul.navigation-2 li a:link, ul#navigation-1 li ul.navigation-2 li a:visited
{
    clear: left;
    background: #b9121b;
    padding: 4px 0;
    width: 260px;
    border: none;
    border-bottom: 1px solid #ffffff;
    position: relative;
    z-index: 1000;
}
ul#navigation-1 li ul.navigation-2 li:hover a, ul#navigation-1 li ul.navigation-2 li a:active, ul#navigation-1 li ul.navigation-2 li a:hover
{
    clear: left;
    background: #ec454e;
    padding: 4px 0;
    width: 260px;
    border: none;
    border-bottom: 1px solid #ffffff;
    position: relative;
    z-index: 1000;
}

ul#navigation-1 li ul.navigation-2 li ul.navigation-3
{
    display: none;
    margin: 0;
    padding: 0;
    list-style: none;
    position: absolute;
    left: 260px;
    top: -2px;
    padding: 1px 1px 0 1px;
    border: 1px solid #b9121b;
    border-left: 1px solid #b9121b;
    background: #ffffff;
    z-index: 900;
}

ul#navigation-1 li ul.navigation-2 li:hover ul.navigation-3
{
    display: block;
}

ul#navigation-1 li ul.navigation-2 li ul.navigation-3 li a:link, ul#navigation-1 li ul.navigation-2 li ul.navigation-3 li a:visited
{
    background: #b9121b;
}

ul#navigation-1 li ul.navigation-2 li ul.navigation-3 li:hover a, ul#navigation-1 li ul.navigation-2 li ul.navigation-3 li a:hover, ul#navigation-1 li ul.navigation-2 li ul.navigation-3 li a:active
{
    background: #ec454e;
}

ul#navigation-1 li ul.navigation-2 li a span
{
    position: absolute;
    top: 0;
    left: 260px;
    font-size: 12pt;
    color: #fe676f;
}
ul#navigation-1 li ul.navigation-2 li:hover a span, ul#navigation-1 li ul.navigation-2 li a:hover span
{
    position: absolute;
    top: 0;
    left: 132px;
    font-size: 12pt;
    color: #ffffff;
}





10 Ekim 2012 Çarşamba

Insertion Sort Vol.2



public class Insertion {

public static void main(String args[])
{
int[] dizi = {1,4,5,8,9,6,3,7,2};
Sort(dizi);
for(int i=0;i<dizi.length;i++)
System.out.print(dizi[i] + " ");
}

private static void Sort(int[] dizi) {
int value = 0,j = 0,i = 0;
for(i=1;i<dizi.length;i++)
{
value = dizi[i];
j = i - 1;

while(j >= 0 && dizi[j] > value)
{
dizi[j+1] = dizi[j];
j = j - 1;
dizi[j+1] = value;
}
}
}

}

Some NP-Hard problems

Traveling Salesman Problem
Find the shortest tour through a given set of n cities that visits each city exactly once before returning to the city where it started.

Knapsack Problem

Given n items of known weights w1, …, wn and values v1, …, vn and a knapsack of capacity W.
Find the most valuable subset of the items that fit into the knapsack

Assignment Problem

There are n people who need to be assigned to execute n jobs, one person per job.
The cost of assigning the ith person to the jth job is a known quantity C[i,j] for each pair i,j=1, … , n.
Problem is to find an assignment with smallest total cost !


Basic String Comparing


import java.util.ArrayList;


public class StrCmp {

public static void main(String args[])
{
char[] cumle = {'e','g','e','l','i'};
char[] aranan = {'e','l'};

int sonuc = ara(cumle,aranan);
if(sonuc != -1)
System.out.print(sonuc + ". elemandan itbaren bulundu");

}

private static int ara(char[] cumle, char[] aranan) {
for(int i=0;i<(cumle.length - aranan.length);i++)
{
int j = 0;

while(j < cumle.length && aranan[j] == cumle[i+j])
{
j++;

if(j == aranan.length)
return i;
}

}
return -1;
}

}

Russian Peasant Multiplication


import java.util.Scanner;

import javax.swing.JOptionPane;


public class Russian {

public static void main(String args[])
{
int say1,say2;
Scanner input = new Scanner(System.in);
System.out.println("İlk çarpanı giriniz");
say1 = input.nextInt();
System.out.println("İkinci çarpanı giriniz");
say2 = input.nextInt();

Carp(say1,say2);
}

private static void Carp(int say1, int say2) {
int sonuc = 0;
while(say1 > 1)
{
while(say1 % 2 == 0)
{
say1 = say1 / 2;
say2 = say2 * 2;
}

   if(say1 == 1)
   {
    say2 = say2 * 2;
   }
sonuc += say2;
   say1 = say1 / 2;
}


System.out.println("Sonuc : " + sonuc);
}

}

Matrix



public class Matrix {

public static void main(String args[])
{
int[][] matris1 = {{2,3},{4,5}};
int[][] matris2 = {{2,1},{3,0}};

Topla(matris1,matris2);
System.out.println("Çarpım");
Carp(matris1,matris2);
}


private static void Carp(int[][] matris1, int[][] matris2) {
int[][] sonuc = new int[2][2];
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
sonuc[i][j] = 0;

   for(int k=0;k<2;k++)
   {
    sonuc[i][j] += matris1[i][k] * matris2[k][j];
   }
   System.out.print(sonuc[i][j] + " ");
   if(j == 1)
System.out.println();
}
}

}

private static void Topla(int[][] matris1, int[][] matris2) {
int[][] sonuc = new int[2][2];
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
sonuc[i][j] = matris1[i][j] + matris2[i][j];
}
}
System.out.println("Toplam");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(sonuc[i][j] + " ");
if(j == 1)
System.out.println();
}
}

}

}

8 Ekim 2012 Pazartesi

Assignment Problem



Assignment Problem
Problem Definition :
There are n people who need to be assigned to execute n jobs, one person per job.
The cost of assigning the ith person to the jth job is a known quantity C[i,j] for each pair i,j=1, … , n.
Problem is to find an assignment with smallest total cost !



import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;



public class Assignment {

static int o;
static int i;
static int a;
static int s;

public static void main(String args[])
{
ArrayList<Integer> dizi = new ArrayList<Integer>();
int[] onur = {9,2,7,8};
int[] iso = {6,4,3,7};
int[] anil = {5,8,1,8};
int[] selim = {7,6,9,4};


Random r = new Random();
Assignment ass = new Assignment();



for(int k=0;k<10000000;k++)
{
ass.o = r.nextInt(4);


do{
ass.i = r.nextInt(4);
}while(ass.i == ass.o);


do{
ass.a = r.nextInt(4);
}while(ass.a == ass.o || ass.a ==ass.i);


do{
ass.s = r.nextInt(4);
}while((ass.s == ass.o) || (ass.s == ass.i) || (ass.s == ass.a));

   if(!dizi.contains(onur[ass.o] + iso[ass.i] + anil[ass.a] + selim[ass.s]))
   {
    dizi.add(onur[ass.o] + iso[ass.i] + anil[ass.a] + selim[ass.s]);
   }

}

System.out.println(dizi);

Comparator c = Collections.reverseOrder();

Collections.sort(dizi,c);

System.out.println("Minumum cost 1/10000000 ihtimalle hatalı olmak şartıyla : " +dizi.get(dizi.size()-1));
   
}

}

7 Ekim 2012 Pazar

Merge Sort



public class MergeSort {

static int dizi[] = {5,12,8,31,6,13,26,19};

public static void main(String args[])
{
System.out.println("Sıralanmadan Önce");
listele(dizi);
mergeSort(0,7);
System.out.println("\nSıralanmadan Sonra");
listele(dizi);
}

    static void listele(int[] dizi)
    {
for(int i=0;i<dizi.length;i++)
{
System.out.print(dizi[i] + " ");
}
}
   
    static void mergeSort(int alt, int ust)
    {
    if(alt < ust)
    {
    int m = (alt + ust) / 2;
    mergeSort(alt, m);
    mergeSort(m+1, ust);
    merge(alt,m,ust);
    }
    }
   
    static void merge(int alt, int m, int ust)
    {
    int[] yeniDizi = new int[8];
    int i, j, k;
   
    for(i = alt;i<= ust;i++)
    {
    yeniDizi[i] = dizi[i];
    }
   
    i = alt;
    j = m+1;
    k = alt;
   
    while(i<=m && j<=ust)
    {
    if(yeniDizi[i] <= yeniDizi[j])
    {
    dizi[k++] = yeniDizi[i++];
    }
    else
    {
    dizi[k++] = yeniDizi[j++];
    }
    }
   
    while(i<=m)
    {
    dizi[k++] = yeniDizi[i++];
    }
    }

}

Quick Sort


public class QuickSort {
public static void main(String args[])
{
int[] dizi = {12,45,879,36,59,123,158,41,85,500};
quickSort(dizi,0,dizi.length-1);
for(int i=0;i<dizi.length;i++)
{
   System.out.print(dizi[i] + " ");
}
}

private static void quickSort(int[] dizi, int alt, int ust) {
int i = alt;
int j = ust;
int h;
int pivot = dizi[(alt+ust)/2];
do{
while(dizi[i] < pivot)
i++;
while(dizi[j] > pivot)
j--;
if(i <= j)
{
h = dizi[i];
dizi[i] = dizi[j];
dizi[j] = h;
i++;
j--;
}
}while(i <= j);
if(alt <j)
quickSort(dizi, alt, j);
if(i < ust)
quickSort(dizi, i, ust);
}

}

3 Ekim 2012 Çarşamba

Bilinmesinde Fayda Gördüğüm Hususlar


  • Kırmızı ışıkta beklerken yeşil yandığında ilk sıradaki araba ışınlanmıyor, o da sizinki gibi bir araç. Otomatik vites ise modeline göre kalkış süresi hakkında internette bilgi bulabilirsiniz. Manual vites ise bu süre 1-1,5 sn sürebilir. 
  • Herhangi bir tartışmadan sonra tartıştığınız kişiye küsecekseniz hiç girmeyin o tartışmaya. Tartışmada haksız olduğunuzu anladığınız anda işi güreşe çevirmeyin haklısın deyip geçin yoksa ilerleyen sürede daha komik duruma düşersiniz ve karşınızdakinin aile terbiyesine maruz kalırsınız.
  • Malesef seven sikilir siken sevilir. İnsanlar niye böyle anlamdım gitti.

30 Eylül 2012 Pazar

Asp.Net Theme





Theme1/Skin1.skin


<asp:Button runat="server" BackColor="SteelBlue" ForeColor="White" />

<asp:TextBox runat="server" BackColor="Red" ForeColor="Black"/>

<asp:Label runat="server" ForeColor="Green" BackColor="Yellow"/>


Theme2/Skin2.skin


<asp:Button runat="server" BackColor="Blue" ForeColor="White" />

<asp:TextBox runat="server" BackColor="Yellow" ForeColor="Red"/>

<asp:Label runat="server" ForeColor="Green" BackColor="Black"/>


WebForm1.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Tema.WebForm1"
    Theme="Theme1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Kullanıcı Adı :"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Button" />
                </td>
            </tr>
            <tr>
                <td>
                 
                    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Tema-1</asp:LinkButton>
                 
                </td>
                <td>
                 
                    <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">Tema-2</asp:LinkButton>
                 
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Tema
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["Theme"] != null)
                Page.Theme = Session["Theme"].ToString();
            else
                Page.Theme = "";
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Session["Theme"] = "Theme1";
            Response.Redirect("WebForm1.aspx");
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Session["Theme"] = "Theme2";
            Response.Redirect("WebForm1.aspx");
        }
    }
}








Asp.Net User Control



AdresBilgisi.ascx


<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AdresBilgisi.ascx.cs"
    Inherits="User.AdresBilgisi" %>
<table>
    <tr>
        <td>
            <asp:Label ID="Label6" runat="server" Text="İl"></asp:Label>
        </td>
        <td>
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>İstanbul</asp:ListItem>
                <asp:ListItem>Ankara</asp:ListItem>
                <asp:ListItem>İzmir</asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="Label7" runat="server" Text="İlçe"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="Label3" runat="server" Text="Cadde"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="Label4" runat="server" Text="Sokak"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="Label5" runat="server" Text="Posta Kodu"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        </td>
    </tr>
</table>


WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="User.WebForm1" %>
<%@ Register TagPrefix="uc" TagName="AdresBilgisi" Src="~/AdresBilgisi.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <uc:AdresBilgisi id="AdresBilgisi1" 
        runat="server" 
        MinValue="1" 
        MaxValue="10" />
    </div>
    </form>
</body>
</html>


Asp.Net Validation Controls



WebForm1.aspx


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<script type="text/javascript">
    function onlyNumber(e) {
        var keyCode = event.keyCode;
        if ((keyCode < 46 || keyCode > 57) && keyCode != 8 && keyCode != 9 && keyCode != 0 && keyCode != 47 && (keyCode < 96 || keyCode > 105)) {
            returnfalse;
        }
    }
</script>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="2" bgcolor="#FFCCFF">
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Üyelik Bilgileri" Font-Bold="True" Font-Size="Larger"
                        ForeColor="#0000CC"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Ad Soyad"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Şifrenizi Yazınız"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label4" runat="server" Text="Şifrenizi Tekrar Yazınız"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox2"
                        ControlToValidate="TextBox3" ErrorMessage="Şifre aynı değil" ForeColor="Red"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label5" runat="server" Text="Doğduğunuz Ay"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                    <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox4"
                        ErrorMessage="[1-12] olmalı " ForeColor="Red" MaximumValue="12" MinimumValue="1"
                        Type="Integer"></asp:RangeValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label6" runat="server" Text="Posta Kodu"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                    <asp:CompareValidator ID="CompareValidator3" runat="server" ControlToValidate="TextBox5"
                        ErrorMessage="Rakam olmalı" ForeColor="Red" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
                    <asp:CustomValidator ID="CustomValidator2" runat="server" ControlToValidate="TextBox5"
                        ErrorMessage="Altı haneli olmalı" ForeColor="Red" OnServerValidate="CustomValidator2_ServerValidate"></asp:CustomValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label7" runat="server" Text="E-Mail"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox6"
                        ErrorMessage="Hatalı" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label8" runat="server" Text="Telefon Numarası"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
                    <asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="TextBox7"
                        ErrorMessage="Rakam olmalı" ForeColor="Red" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
                    <asp:CustomValidator ID="CustomValidator3" runat="server" ControlToValidate="TextBox7"
                        ErrorMessage="On haneli olmalı" ForeColor="Red" OnServerValidate="CustomValidator3_ServerValidate"></asp:CustomValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label9" runat="server" Text="Çİft Sayı Giriniz"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
                    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Tek sayı girmeyin"
                        ForeColor="Red"></asp:CustomValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Üye Ol" OnClick="Button1_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Kontrol
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox8.Text != "")
            {
                if (Convert.ToInt32(TextBox8.Text) % 2 != 0)
                    CustomValidator1.IsValid = false;
                else
                    CustomValidator1.IsValid = true;
            }

        }

        protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (args.Value.Length == 6)
                args.IsValid = true;
            else
                args.IsValid = false;
        }

        protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (args.Value.Length == 10)
                args.IsValid = true;
            else
                args.IsValid = false;
        }
    }
}