Advertisement
Domy131097

Programiranje u Javi - B grupa

Nov 19th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1. // ZADATAK 1
  2. import java.util.Scanner;
  3.  
  4. public class Matrica {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner ulaz = new Scanner(System.in);
  8.         int M = 0;
  9.        
  10.         System.out.println("Unesite dimenziju kvadratne matrice (broj mora biti u intervalu između 2 i 6):");
  11.         do  {
  12.             M = ulaz.nextInt();
  13.         } while (M < 3 || M > 5);
  14.         int matrica[][] = new int[M][M];
  15.        
  16.         System.out.println("Ispunite matricu cijelim brojevima:");
  17.         for(int i = 0; i < M; i++) {
  18.             for(int j = 0; j < M; j++) {
  19.                 matrica[i][j] = ulaz.nextInt();
  20.             }
  21.         }
  22.        
  23.         System.out.println("Brojevi sporedne dijagonale: ");
  24.         for(int i = 0; i < M; i++) {
  25.             for(int j = 0; j < M; j++) {
  26.                 if(i + j == M - 1 && matrica[i][j] < matrica[0][0] && matrica[i][j] < matrica[M-1][M-1])
  27.                     System.out.println(matrica[i][j]);
  28.             }
  29.         }
  30.        
  31.         ulaz.close();
  32.     }
  33.  
  34. }
  35.  
  36. //ZADATAK 2
  37. import java.util.Scanner;
  38.  
  39. public class IspisPrirodnihBrojeva {
  40.  
  41.     public static void main(String[] args) {
  42.         Scanner ulaz = new Scanner(System.in);
  43.         int N, brojac = 0;
  44.        
  45.         System.out.println("Unesite prirodni broj N:");
  46.         do {
  47.             N = ulaz.nextInt();
  48.         }while(N < 1);
  49.        
  50.         System.out.println("Brojevi djeljivi s 5 u unesenom intervalu: ");
  51.         for(int i = 1; i < N; i++) {
  52.             if(i % 5 == 0) {
  53.                 if(brojac == 5) return;
  54.                 System.out.println(i);
  55.                 brojac++;
  56.             }
  57.         }
  58.        
  59.         ulaz.close();
  60.     }
  61. }
  62.  
  63. //ZADATAK 3
  64. import java.io.BufferedWriter;
  65. import java.io.FileWriter;
  66. import java.io.IOException;
  67. import java.util.Scanner;
  68.  
  69. public class ProvjeraRecenice {
  70.     public static void main(String[] args) {
  71.         Scanner ulaz = new Scanner(System.in);
  72.         String recenica = null;
  73.         int zadnjiIndeks = 0;
  74.        
  75.         while(true) {
  76.             recenica = ulaz.nextLine();
  77.            
  78.             if(recenica.equals("STOP"))
  79.                 break;
  80.            
  81.             zadnjiIndeks = recenica.length() - 1;
  82.             if(Character.isLowerCase(recenica.charAt(0))) {
  83.                 recenica = recenica.substring(0, 1).toUpperCase() + recenica.substring(1);
  84.             }
  85.             if(recenica.charAt(zadnjiIndeks) != '.') {
  86.                 recenica += '.';
  87.             }
  88.  
  89.             spremiUDatoteku(recenica);
  90.         }
  91.        
  92.         ulaz.close();
  93.     }
  94.    
  95.     public static void spremiUDatoteku(String rec) {
  96.         BufferedWriter bw = null;
  97.         FileWriter fw = null;
  98.         try {
  99.             fw = new FileWriter("recenice.txt", true);
  100.             bw = new BufferedWriter(fw);
  101.             bw.write(rec);
  102.         } catch (IOException e) {
  103.             e.printStackTrace();
  104.         } finally {
  105.             try {
  106.                 if (bw != null)
  107.                     bw.close();
  108.                 if (fw != null)
  109.                     fw.close();
  110.             } catch (IOException ex) {
  111.                 ex.printStackTrace();
  112.             }
  113.         }
  114.     }
  115. }
  116.  
  117. //ZADATAK 4
  118. public class Automobil {
  119.     private int spremnik;
  120.     private double potrosnja;
  121.     private String vrstaGoriva, tip, marka;
  122.    
  123.     Automobil() {
  124.         spremnik = 0;
  125.         potrosnja = 0.0;
  126.         vrstaGoriva = null;
  127.         tip = null;
  128.         marka = null;
  129.     }
  130.    
  131.     Automobil(int spremnik, double potrosnja, String vrstaGoriva, String tip, String marka) {
  132.         this.spremnik = spremnik;
  133.         this.potrosnja = potrosnja;
  134.         this.vrstaGoriva = vrstaGoriva;
  135.         this.tip = tip;
  136.         this.marka = marka;
  137.     }
  138.  
  139.     public int getSpremnik() {
  140.         return spremnik;
  141.     }
  142.  
  143.     public void setSpremnik(int spremnik) {
  144.         this.spremnik = spremnik;
  145.     }
  146.  
  147.     public double getPotrosnja() {
  148.         return potrosnja;
  149.     }
  150.  
  151.     public void setPotrosnja(double potrosnja) {
  152.         this.potrosnja = potrosnja;
  153.     }
  154.  
  155.     public String getVrstaGoriva() {
  156.         return vrstaGoriva;
  157.     }
  158.  
  159.     public void setVrstaGoriva(String vrstaGoriva) {
  160.         this.vrstaGoriva = vrstaGoriva;
  161.     }
  162.  
  163.     public String getTip() {
  164.         return tip;
  165.     }
  166.  
  167.     public void setTip(String tip) {
  168.         this.tip = tip;
  169.     }
  170.  
  171.     public String getMarka() {
  172.         return marka;
  173.     }
  174.  
  175.     public void setMarka(String marka) {
  176.         this.marka = marka;
  177.     }
  178. }
  179.  
  180. public class Test extends Automobil {
  181.     private double potrosio_goriva, prijeden_put;
  182.  
  183.     public double getPotrosio_goriva() {
  184.         return potrosio_goriva;
  185.     }
  186.  
  187.     public void setPotrosio_goriva(double potrosio_goriva) {
  188.         this.potrosio_goriva = potrosio_goriva;
  189.     }
  190.  
  191.     public double getPrijeden_put() {
  192.         return prijeden_put;
  193.     }
  194.  
  195.     public void setPrijeden_put(double prijeden_put) {
  196.         this.prijeden_put = prijeden_put;
  197.     }
  198. }
  199. public class Izracun {
  200.    
  201.     public static void usporedi(Test t1, Test t2) {
  202.         if((t1.getSpremnik()-t1.getPotrosio_goriva()) / (t1.getPrijeden_put() / 100) < (t2.getSpremnik()-t2.getPotrosio_goriva()) / (t2.getPrijeden_put() / 100)) {
  203.             System.out.println("Prvi automobil je stedljiviji.");
  204.         }
  205.         System.out.println("Drugi automobil je stedljiviji.");
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement