Advertisement
pseudocreator

OPS1, k1, z1, RacunanjeElemenata

Oct 26th, 2015
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. /*
  2.  * Svaka od niti nezavisno izracunava i ispisuje prvih 5 elemenata svog niza prirodnih brojeva.
  3.  * Za to vreme glavna nit ucitava red po red sa tastature i zavrsava rad celog programa kada
  4.  * se ucita "Kraj".
  5.  *
  6.  * nitF --> fibonaci
  7.  * nitK --> K_0 = 1, K_n = n^2;
  8.  **/
  9.  
  10. //ISPRAVITI POSTOJECU/E GRESKU/E
  11.  
  12. import java.io.*;
  13.  
  14. public class Vezba1 extends Thread
  15. {
  16.  
  17.   //number of iterations
  18.   private final int n = 50;
  19.   //element n && element n+1
  20.   private int temp, next;
  21.   //Thread name
  22.   private String name;
  23.  
  24.   public Vezba1(int temp, int next, String name){
  25.     setName("nit" + name);
  26.     this.name = name;
  27.     setDaemon(true);
  28.     this.temp = temp;
  29.     this.next = next;
  30.   }
  31.  
  32.   public void run() {
  33.       System.out.println(getName() + " " + temp);
  34.       System.out.println(getName() + " " + next);
  35.       for(int i = 0; i < 2; i++){
  36.  
  37.         if(isInterrupted())
  38.           break;
  39.        
  40.         int t = izracunaj(temp, next, i);
  41.         temp = next;
  42.         next = t;
  43.         System.out.println(getName() + ", medju rezultat: " + t);
  44.       }
  45.   }
  46.  
  47.   private int izracunaj(int temp, int next, int t){
  48.     if(name.equals("f")){
  49.       return temp + next;
  50.     } else if(name.equals("k")){
  51.       return (int)(Math.pow(t, 2));
  52.     } else {
  53.       System.out.println("Greska: ime nije validno");
  54.       return 0;
  55.     }
  56.   }
  57.   public static void main(String[] args){
  58.    
  59.     Thread nitF = new Vezba1(0, 1, "f");
  60.     Thread nitK = new Vezba1(0, (int)(Math.pow(1, 2)), "k");
  61.    
  62.     nitF.start();
  63.     nitK.start();
  64.    
  65.    
  66.     BufferedReader reader = null;
  67.     try{
  68.       reader = new BufferedReader(new InputStreamReader(System.in));
  69.       String line = null;
  70.       while(line.equals("Kraj")){
  71.         line = reader.readLine();
  72.       }
  73.     }catch(Exception e){
  74.       e.printStackTrace();
  75.     }finally{
  76.       try{
  77.         reader.close();
  78.       }catch(Exception ex){
  79.         ex.printStackTrace();
  80.       }
  81.     }
  82.  
  83.   }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement