ΑΣΚΗΣΕΙΣ ΘΕΩΡΙΑΣ

ΠΑΡΑΣΚΕΥΗ 27 ΜΑΙΟΥ 2016

Διδάχθηκαν servlets (όχι κώδικες στην ύλη) 

ΠΑΡΑΣΚΕΥΗ 20 ΜΑΙΟΥ 2016

ΑΠΛΟ SOCKET

Client_Sock

package client_sock;

import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;

public class Client_Sock {

    public static void main(String[] args) {

        try
        {
           Socket sock = new Socket("localhost", 9999);
          
           PrintStream pr = new PrintStream(sock.getOutputStream());
          
           System.out.println("ΣΤΕΙΛΕ ΜΑΣ ΑΜΕΣΩΣ ΑΥΤΟ ΠΟΥ ΠΡΕΠΕΙ : ");
          
           InputStreamReader rd=new InputStreamReader(System.in);
          
           BufferedReader ed=new BufferedReader(rd);
          
           String temp = ed.readLine();
          
           pr.println(temp);
           
        }
      
        catch(Exception ex)
               
                {
                    
                }
    }
}


Server_Sock


package server_sock;

import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;

public class Server_Sock {

    public static void main(String[] args) {

        try
        {
       
            ServerSocket ser=new ServerSocket(9999);
            Socket sock=ser.accept();
           
            BufferedReader ed = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            String tmp=ed.readLine();
            System.out.println("FILE MOY ESTEILES AYTO " +tmp);
        }
       
        catch(Exception ex)
        {
           
    }

}

}

ΑΥΤΟΣ Ο SERVER ΛΕΙΤΟΥΡΓΕΙ ΜΕ TELNET (π.χ. ZOC TERMINAL)

package echoserver;

import java.io.*;
import java.net.*;

class EchoServer {

  public static void main(String[] args) {
    System.out.println("EchoServer started.");
    try {
      ServerSocket s = new ServerSocket(8008);
      Socket incoming = s.accept();

      System.out.println("Connected to: " + incoming.getInetAddress() +
                         " at port: " + incoming.getLocalPort());
     
      BufferedReader in
        = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
      PrintWriter out
        = new PrintWriter(new OutputStreamWriter(incoming.getOutputStream()));

      out.println("Hello! This is Java EchoServer. Enter BYE to exit.");
      out.flush();
      
      for (;;) {
        String str = in.readLine();
        if (str == null) {
          break;
        } else {
          out.println("Echo: " + str);
          out.flush();
          System.out.println("Received: " + str);
         
          if (str.trim().equals("BYE"))
            break;
        }
      }
      incoming.close();
    } catch (Exception e) {
      System.out.println("Error: " + e);
    }
    System.out.println("EchoServer stopped.");
  }



}

ΠΑΡΑΣΚΕΥΗ 13 ΜΑΙΟΥ 2016


Διδάχθηκαν τα πρωτόκολλα TCP (όχι κώδικες στην ύλη) 


ΠΑΡΑΣΚΕΥΗ 22 ΑΠΡΙΛΙΟΥ 2016 


Αντιμετώπιση προβλήματος μη υποστήριξης πολλαπλής κληρονομικότητας 


import java.io.IOException;

public class JumbleNames implements Runnable
{
  private String firstName;                         // Store for first name
  private String secondName;                        // Store for second name
  private long aWhile;                              // Delay in milliseconds

  // Constructor
  public JumbleNames(String firstName, String secondName, long delay)
  {
    this.firstName = firstName;                     // Store the first name
    this.secondName = secondName;                   // Store the second name
    aWhile = delay;                                 // Store the delay
  }

  // Method where thread execution will start
  public void run()
  {
    try
    {
      while(true)                                   // Loop indefinitely...
      {
        System.out.print(firstName);                // Output first name
        Thread.sleep(aWhile);                       // Wait aWhile msec.
        System.out.print(secondName+"\n");          // Output second name
      }
    }
    catch(InterruptedException e)                   // Handle thread interruption
    {
      System.out.println(firstName + secondName + e);     // Output the exception
    }
  }

  public static void main(String[] args)
  {
    // Create three threads
    Thread first = new Thread(new JumbleNames("Albert ", "Einstein ", 200L));
    Thread second = new Thread(new JumbleNames("Marilyn ", "Monroe ", 300L));
    Thread third = new Thread(new JumbleNames("Sigmund ", "Freud ", 500L));

    // Set threads as daemon
    first.setDaemon(true);
    second.setDaemon(true);
    third.setDaemon(true);

    System.out.println("Press Enter when you have had enough...\n");
    first.start();                                  // Start the first thread
    second.start();                                 // Start the second thread
    third.start();                                  // Start the third thread
    try
    {
      System.in.read();                             // Wait until Enter key pressed
      System.out.println("Enter pressed...\n");
    }
    catch (IOException e)                           // Handle IO exception
    {
      System.out.println(e);                        // Output the exception
    }
    System.out.println("Ending main()");
    return;
  }

}

Συγχρονισμός νημάτων

Θα δημιουργήσουμε το project παραγωγού καταναλωτή. Οι κλάσεις μας θα είναι η Conumer, η Producer η CubbyHole και η κεντρική κλάση ProducerCustomerTest. 

public class ProducerConsumerTest {
    public static void main(String[] args) {
 CubbyHole c = new CubbyHole();
 Producer p1 = new Producer(c, 1);
 Consumer c1 = new Consumer(c, 1);
 p1.start();
 c1.start();
 }
}

public class Consumer extends Thread {
 private CubbyHole cubbyhole;
 private int number;
 public Consumer(CubbyHole c, int number) {
 cubbyhole = c;
 this.number = number;
 }
 public void run() {
 int value = 0;
 for (int i = 0; i < 10; i++) {
 value = cubbyhole.get();
 System.out.println("Consumer #" + this.number
 + " got: " + value);
 }
 }

public class CubbyHole {
 private int contents;
 private boolean available = false;
 public synchronized int get() {
 while (available == false) {
 try {

 wait();
 } catch (InterruptedException e) { }
 }

 available = false;
 System.out.println("in get");
 notifyAll();
 return contents;
 }
 public synchronized void put(int value) {
 while (available == true) {
 try {

 wait();
 } catch (InterruptedException e) { }
 }
 contents = value;
 available = true;
 System.out.println("in gut");
 notifyAll();
 }


public class Producer extends Thread {
 private CubbyHole cubbyhole;
 private int number;
 public Producer(CubbyHole c, int number) {
 cubbyhole = c;
 this.number = number;
 }
 public void run() {
 for (int i = 0; i < 10; i++) {
 cubbyhole.put(i);
 System.out.println("Producer #" + this.number
 + " put: " + i);
 try {
 sleep((int)(Math.random() * 1000));
 } catch (InterruptedException e) { }
 }
 }

ΠΑΡΑΣΚΕΥΗ 15 ΑΠΡΙΛΙΟΥ 2016 

Υλοποίηση Thread

class TwoThreadsTest {
 public static void main (String[] args) {
 new SimpleThread("Jamaica").start();
 new SimpleThread("Fiji").start();
 }

class SimpleThread extends Thread {
 public SimpleThread(String str) {
 super(str);
 }
 public void run() {
 for (int i = 0; i < 10; i++) {
 System.out.println(i + " " + getName());
 try {
 sleep((int)(Math.random() * 1000));
 } catch (InterruptedException e) {}
 }
 System.out.println("DONE! " + getName());
 }
                                            ΠΑΡΑΣΚΕΥΗ 8 ΑΠΡΙΛΙΟΥ 2016 
Εξαιρέσεις 

public class TestExceptions{
public static void main(String args[]){
try {
System.out.println("Code before the error");
System.out.println(10/0);
System.out.println("Code after the error");
}
Catch (java.lang.ArithmeticException e){
System.out.println("Tmima kodika Catch");
System.out.println(e.getMessage());
System.out.println("The operation is not possible.");
}
Finally
{
System.out.println("Tmima kodika Finally");
}
}

      ΠΑΡΑΣΚΕΥΗ 1 ΑΠΡΙΛΙΟΥ 2016 

STREAMS 

Βλέπε ΑΣΚΗΣΕΙΣ ΕΡΓΑΣΤΗΡΙΟΥ 

23 και 30 ΜΑΡΤΙΟΥ 2016 / 1 Απριλίου 2016

ΠΑΡΑΣΚΕΥΗ 18 ΜΑΡΤΙΟΥ 2016 
Πίνακες: Βλέπε Ασκήσεις εργαστηρίου 

ΠΑΡΑΣΚΕΥΗ 11  ΜΑΡΤΙΟΥ 2016 


ΔΥΝΑΜΙΚΕΣ ΔΟΜΕΣ

Array list – Vector

Στις συγκεκριμένες δομές  δεδομένων μπορούμε να προσθέτουμε στοιχεία όποτε εμείς θέλουμε

Εισαγωγή βιβλιοθήκης
import java.util.ArrayList;
import java.util.Vector;

Δήλωση
ArrayList x = new ArrayList();
ArrayList<String> x = new ArrayList<String>();

Εισαγωγή στοιχείων
String s1 = "Hello";
String s2 = "World";

x.add(s1);
x.add(s2);

System.out.println(x);

Ανάκτηση πληροφορίας
System.out.println(x.get(1));

Υπολογισμός αριθμού στοιχείων
System.out.println(x.size());

Εκτύπωση όλης της Array list
for (int i = 0; i < x.size(); i++) {
System.out.println(x.get(i));
}

Έλεγχος αν κάποιο string εμπεριέχεται σε μία Array list
System.out.println("Contains Hello: " + x.contains("Hello")); // true
System.out.println("Contains Java: " + x.contains("JAVA")); // false

Διαγραφή
x.remove(0);
x.clear();

ΠΑΡΑΣΚΕΥΗ 4 ΜΑΡΤΙΟΥ 2016 

IF - SCANNER 

package javaapplication21;

import java.util.Scanner;

public class JavaApplication21 {

    public static void main(String[] args) {
     
        Scanner input = new Scanner (System.in);
     
      int x;
      int y;
      int mikrovia;
     
      System.out.println("ΔΩΣΕ ΤΟΝ ΠΡΩΤΟ ΑΡΙΘΜΟ: ");
      x=input.nextInt();
     
      System.out.println("ΔΩΣΕ ΤΟΝ ΔΕΥΤΕΡΟ ΑΡΙΘΜΟ: ");
      y=input.nextInt();
     
      mikrovia=(x+y)*x*y;
     
      System.out.println("ΤΑ ΜΙΚΡΟΒΙΑ ΕΙΝΑΙ:  " + mikrovia);
     
      if(mikrovia>10000){
      System.out.println("ΝΑ ΤΟΝ ΣΤΕΙΛΟΥΜΕ ΑΜΕΣΩΣ ΣΤΗΝ ΕΝΤΑΤΙΚΗ ");
    }
      else if(mikrovia<=10000){
         
      System.out.println("ΝΑ ΦΥΓΕΙ ΑΠΟ ΕΔΩ ... ΕΙΝΑΙ ΚΑΛΑ..." );
     
    }
      }

    }

FOR
package javaapplication20;

public class JavaApplication20 {

    public static void main(String[] args) {
      
        int i=0;
 
        for (i=1; i<=10; i++)
        {
    System.out.println(i);
        }
       
        }   
}


WHILE

package javaapplication20;

public class JavaApplication20 {

    public static void main(String[] args) {
      
        int i=0;
 
        while (i<10)
        {
           System.out.println (i);          
           i++;
        }

    }
}

DO – WHILE

package javaapplication20;

public class JavaApplication20 {

    public static void main(String[] args) {
      
       int i=0;
 
       do
        {
           System.out.println(i);
           i++;
        }
            while(i<10);

    }
}

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

4o ΜΑΘΗΜΑ  HTML