Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=================================== ZADATAK 1 =========================================//
- public abstract class Matrica {
- private String M[][];
- private int m;
- public abstract void _xo();
- public String[][] getMatrica() {
- return M;
- }
- public void setMatrica(String[][] M) {
- this.M = M;
- }
- public int getM() {
- return m;
- }
- public void setM(int m) {
- this.m = m;
- }
- }
- //=================================== ZADATAK 2 =========================================//
- import java.io.Serializable;
- public class Student implements Serializable {
- private int maticnibroj;
- private String ime;
- private String prezime;
- private int[] popisOcjena;
- public Student(int mb, String ime, String prezime, int[] popis) {
- this.maticnibroj = mb;
- this.ime = ime;
- this.prezime = prezime;
- this.popisOcjena = popis;
- }
- public int ucitajMaticniBroj() {
- return maticnibroj;
- }
- public String ucitajIme() {
- return ime;
- }
- public String ucitajPrezime() {
- return prezime;
- }
- public int[] ucitajOcjene() {
- return popisOcjena;
- }
- }
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- import java.io.ObjectOutputStream;
- public class SpremiUDatoteku {
- private static ObjectOutputStream izlaz;
- public static void main(String[] args) {
- otvoriDatoteku();
- snimiStudente();
- zatvoriDatoteku();
- }
- public static void otvoriDatoteku() {
- try {
- izlaz = new ObjectOutputStream(
- Files.newOutputStream(Paths.get("studenti.ser")));
- }
- catch (IOException IOgreska) {
- System.err.println("Pogreska otvaranja datoteke.");
- System.exit(1);
- }
- }
- public static void snimiStudente() {
- Scanner input = new Scanner(System.in);
- for(int i = 0; i < 2; i ++) {
- try {
- int ocjene[] = new int[5];
- System.out.println("Unesite popis ocjena za 5 kolegija " + (i + 1) + ". studenta:");
- for(int j = 0; j < 5; j++) {
- ocjene[i] = input.nextInt();
- }
- System.out.println("Unesite maticni broj, ime, prezime" + (i + 1) + ". studenta:");
- Student snimanje = new Student(input.nextInt(), input.next(), input.next(), ocjene);
- izlaz.writeObject(snimanje);
- }
- catch (NoSuchElementException elementgreska) {
- System.err.println("Pogresan unos, pokusajte ponovo.");
- input.nextLine();
- }
- catch (IOException IOgreska) {
- System.err.println("Pogreska pisanja u datoteku.");
- break;
- }
- }
- }
- public static void zatvoriDatoteku() {
- try {
- if (izlaz != null)
- izlaz.close();
- }
- catch (IOException IOgreska) {
- System.err.println("Pogreska zatvaranja datoteke.");
- }
- }
- }
- import java.io.EOFException;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- public class UcitavanjeIzDatoteke {
- private static ObjectInputStream ulaz;
- public static void main(String[] args) {
- otvoriDatoteku();
- ucitajStudente();
- zatvoriDatoteku();
- }
- public static void otvoriDatoteku() {
- try {
- ulaz = new ObjectInputStream(Files.newInputStream(Paths.get("studenti.ser")));
- }
- catch (IOException ioException) {
- System.err.println("Pogreska otvaranja datoteke.");
- System.exit(1);
- }
- }
- public static void ucitajStudente() {
- System.out.printf("%-18s%-12s%-12s%-12s%n", "Maticni broj", "Ime", "Prezime", "Popis ocjena");
- try {
- while (true) {
- Student snimi = (Student) ulaz.readObject();
- int popis[] = new int[5];
- popis = snimi.ucitajOcjene();
- System.out.printf("%-18d%-12s%-12s%-2d%-2d%-2d%-2d%-2d%n",
- snimi.ucitajMaticniBroj(), snimi.ucitajIme(), snimi.ucitajPrezime(), popis[0], popis[1], popis[2], popis[3], popis[4]);
- }
- }
- catch (EOFException krajdatoteke) {
- System.out.println("Nema vise studenata.");
- }
- catch (ClassNotFoundException klasanijepronadena) {
- System.err.println("Pogresan tip objekta.");
- }
- catch (IOException IOgreska) {
- System.err.println("Pogreska citanja iz datoteke.");
- }
- }
- public static void zatvoriDatoteku() {
- try {
- if (ulaz != null)
- ulaz.close();
- }
- catch (IOException IOgreska) {
- System.err.println("Pogreska zatvaranja datoteke.");
- System.exit(1);
- }
- }
- }
- //=================================== ZADATAK 3 =========================================//
- public class Brojac extends Thread {
- private int parametar;
- private String ime;
- Brojac(String ime, int param){
- this.ime = ime;
- parametar = param;
- }
- public void run(){
- Thread threadSleep = new Thread();
- if(parametar == 0){
- for(int i = 0; i < 11; i++){
- System.out.println(ime + ": " + i);
- try{
- this.sleep(1000);
- }catch(InterruptedException ie){
- }
- }
- }
- else {
- for(int i = 10; i > -1; i--){
- System.out.println(ime + ": " + i);
- try{
- this.sleep(1000);
- }catch(InterruptedException ie){
- }
- }
- }
- }
- }
- public class Test {
- public static void main(String[] args){
- Brojac brojac1 = new Brojac("Nit 1", 0);
- brojac1.start();
- Brojac brojac2 = new Brojac("Nit 2", 1);
- brojac2.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement