Advertisement
ithoran

PJ Lab 2

Mar 29th, 2016
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. // Red
  2. package kolekcije;
  3.  
  4. public class Red {
  5.     private int kapacitetRed;
  6.     private int brZauzetih;
  7.     private int head;
  8.     private int tail;
  9.     private Object[] nizRed;
  10.    
  11.     public Red(int n) {
  12.         kapacitetRed = n;
  13.         brZauzetih = 0;
  14.         head = 0;
  15.         tail = -1;
  16.         nizRed = new Object[kapacitetRed];
  17.         for (int i = 0; i < kapacitetRed; i++) {
  18.             nizRed[i] = new Object();
  19.         }
  20.     }
  21.    
  22.     public void isFull() {
  23.         if (brZauzetih == kapacitetRed)
  24.             System.out.println("Pun je");
  25.         else
  26.             System.out.println("Nije pun");
  27.     }
  28.    
  29.     public void dodaj(Object l) {
  30.         if (brZauzetih == kapacitetRed) {
  31.             System.out.println("Red je prepun!");
  32.             return;
  33.         }
  34.         if (tail + 1 == kapacitetRed) {
  35.             tail = -1;
  36.         }
  37.         tail++;
  38.         brZauzetih++;
  39.         nizRed[tail] = l;
  40.     }
  41.    
  42.     public Object izbaci() {
  43.         if (brZauzetih == 0) {
  44.             System.out.println("Red je prazan!");
  45.             return 0;
  46.         }
  47.         Object vrati = nizRed[head];
  48.         if (head + 1 == kapacitetRed) {
  49.             head = -1;
  50.         }
  51.         brZauzetih--;
  52.         head++;
  53.         return vrati;
  54.     }
  55.    
  56. }
  57.  
  58. // RacionalanBroj
  59. package brojevi;
  60.  
  61. import java.util.Scanner;
  62.  
  63. public class RacionalanBroj {
  64.     private double br;
  65.     private double imen;
  66.     Scanner unesi = new Scanner(System.in);
  67.    
  68.     public RacionalanBroj() {
  69.         System.out.println("Unesite brojilac i imenilac:");
  70.         br = unesi.nextDouble();
  71.         imen = unesi.nextDouble();
  72.     }
  73.    
  74.     public String toString() {
  75.         return Double.toString(br) + "/" + Double.toString(imen);
  76.     }
  77. }
  78.  
  79. // mejn
  80. import kolekcije.*;
  81. import brojevi.*;
  82.  
  83. public class mejn {
  84.  
  85.     public static void main(String[] args) {
  86.         Red rCeli = new Red(10);
  87.         for (int i = 0; i < 3; i++) {
  88.             RacionalanBroj x = new RacionalanBroj();
  89.             rCeli.dodaj(x);
  90.         }
  91.         for (int i = 0; i < 3; i++) {
  92.             System.out.println(rCeli.izbaci());
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement