Advertisement
Domy131097

PismeniIspit

Feb 13th, 2018
142
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=================================== ZADATAK 1 =========================================//
  2. public abstract class Matrica {
  3. private String M[][];
  4. private int m;
  5.  
  6. public abstract void _xo();
  7. public String[][] getMatrica() {
  8. return M;
  9. }
  10.  
  11. public void setMatrica(String[][] M) {
  12. this.M = M;
  13. }
  14.  
  15. public int getM() {
  16. return m;
  17. }
  18.  
  19. public void setM(int m) {
  20. this.m = m;
  21. }
  22. }
  23. //=================================== ZADATAK 2 =========================================//
  24. import java.io.Serializable;
  25.  
  26. public class Student implements Serializable {
  27. private int maticnibroj;
  28. private String ime;
  29. private String prezime;
  30. private int[] popisOcjena;
  31.  
  32. public Student(int mb, String ime, String prezime, int[] popis) {
  33. this.maticnibroj = mb;
  34. this.ime = ime;
  35. this.prezime = prezime;
  36. this.popisOcjena = popis;
  37. }
  38.  
  39. public int ucitajMaticniBroj() {
  40. return maticnibroj;
  41. }
  42. public String ucitajIme() {
  43. return ime;
  44. }
  45. public String ucitajPrezime() {
  46. return prezime;
  47. }
  48. public int[] ucitajOcjene() {
  49. return popisOcjena;
  50. }
  51. }
  52. import java.io.IOException;
  53. import java.nio.file.Files;
  54. import java.nio.file.Paths;
  55. import java.util.NoSuchElementException;
  56. import java.util.Scanner;
  57. import java.io.ObjectOutputStream;
  58.  
  59. public class SpremiUDatoteku {
  60. private static ObjectOutputStream izlaz;
  61. public static void main(String[] args) {
  62. otvoriDatoteku();
  63. snimiStudente();
  64. zatvoriDatoteku();
  65. }
  66.  
  67. public static void otvoriDatoteku() {
  68. try {
  69. izlaz = new ObjectOutputStream(
  70. Files.newOutputStream(Paths.get("studenti.ser")));
  71. }
  72. catch (IOException IOgreska) {
  73. System.err.println("Pogreska otvaranja datoteke.");
  74. System.exit(1);
  75. }
  76. }
  77.  
  78. public static void snimiStudente() {
  79. Scanner input = new Scanner(System.in);
  80. for(int i = 0; i < 2; i ++) {
  81. try {
  82. int ocjene[] = new int[5];
  83. System.out.println("Unesite popis ocjena za 5 kolegija " + (i + 1) + ". studenta:");
  84. for(int j = 0; j < 5; j++) {
  85. ocjene[i] = input.nextInt();
  86. }
  87. System.out.println("Unesite maticni broj, ime, prezime" + (i + 1) + ". studenta:");
  88. Student snimanje = new Student(input.nextInt(), input.next(), input.next(), ocjene);
  89. izlaz.writeObject(snimanje);
  90. }
  91. catch (NoSuchElementException elementgreska) {
  92. System.err.println("Pogresan unos, pokusajte ponovo.");
  93. input.nextLine();
  94. }
  95. catch (IOException IOgreska) {
  96. System.err.println("Pogreska pisanja u datoteku.");
  97. break;
  98. }
  99. }
  100. }
  101.  
  102. public static void zatvoriDatoteku() {
  103. try {
  104. if (izlaz != null)
  105. izlaz.close();
  106. }
  107. catch (IOException IOgreska) {
  108. System.err.println("Pogreska zatvaranja datoteke.");
  109. }
  110. }
  111. }
  112. import java.io.EOFException;
  113. import java.io.IOException;
  114. import java.io.ObjectInputStream;
  115. import java.nio.file.Files;
  116. import java.nio.file.Paths;
  117.  
  118. public class UcitavanjeIzDatoteke {
  119. private static ObjectInputStream ulaz;
  120. public static void main(String[] args) {
  121. otvoriDatoteku();
  122. ucitajStudente();
  123. zatvoriDatoteku();
  124.  
  125. }
  126.  
  127. public static void otvoriDatoteku() {
  128. try {
  129. ulaz = new ObjectInputStream(Files.newInputStream(Paths.get("studenti.ser")));
  130. }
  131. catch (IOException ioException) {
  132. System.err.println("Pogreska otvaranja datoteke.");
  133. System.exit(1);
  134. }
  135. }
  136.  
  137. public static void ucitajStudente() {
  138. System.out.printf("%-18s%-12s%-12s%-12s%n", "Maticni broj", "Ime", "Prezime", "Popis ocjena");
  139.  
  140. try {
  141. while (true) {
  142. Student snimi = (Student) ulaz.readObject();
  143. int popis[] = new int[5];
  144. popis = snimi.ucitajOcjene();
  145. System.out.printf("%-18d%-12s%-12s%-2d%-2d%-2d%-2d%-2d%n",
  146. snimi.ucitajMaticniBroj(), snimi.ucitajIme(), snimi.ucitajPrezime(), popis[0], popis[1], popis[2], popis[3], popis[4]);
  147. }
  148. }
  149. catch (EOFException krajdatoteke) {
  150. System.out.println("Nema vise studenata.");
  151. }
  152. catch (ClassNotFoundException klasanijepronadena) {
  153. System.err.println("Pogresan tip objekta.");
  154. }
  155. catch (IOException IOgreska) {
  156. System.err.println("Pogreska citanja iz datoteke.");
  157. }
  158. }
  159.  
  160. public static void zatvoriDatoteku() {
  161. try {
  162. if (ulaz != null)
  163. ulaz.close();
  164. }
  165. catch (IOException IOgreska) {
  166. System.err.println("Pogreska zatvaranja datoteke.");
  167. System.exit(1);
  168. }
  169. }
  170. }
  171. //=================================== ZADATAK 3 =========================================//
  172. public class Brojac extends Thread {
  173. private int parametar;
  174. private String ime;
  175. Brojac(String ime, int param){
  176. this.ime = ime;
  177. parametar = param;
  178. }
  179. public void run(){
  180. Thread threadSleep = new Thread();
  181. if(parametar == 0){
  182. for(int i = 0; i < 11; i++){
  183. System.out.println(ime + ": " + i);
  184. try{
  185. this.sleep(1000);
  186. }catch(InterruptedException ie){
  187.  
  188. }
  189. }
  190. }
  191. else {
  192. for(int i = 10; i > -1; i--){
  193. System.out.println(ime + ": " + i);
  194. try{
  195. this.sleep(1000);
  196. }catch(InterruptedException ie){
  197.  
  198. }
  199. }
  200. }
  201. }
  202. }
  203. public class Test {
  204. public static void main(String[] args){
  205. Brojac brojac1 = new Brojac("Nit 1", 0);
  206. brojac1.start();
  207. Brojac brojac2 = new Brojac("Nit 2", 1);
  208. brojac2.start();
  209. }
  210. }
Advertisement
RAW Paste Data Copied
Advertisement