Advertisement
Guest User

LAB1 LAB2

a guest
Nov 17th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 42.41 KB | None | 0 0
  1. ==== LAB.1 RIMSKI BROEVI ===
  2.  
  3. import java.util.Scanner;
  4. import java.util.stream.IntStream;
  5.  
  6. public class RomanConverterTest {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int n = scanner.nextInt();
  10.         IntStream.range(0, n)
  11.         .forEach(x -> System.out.println(RomanConverter.toRoman(scanner.nextInt())));
  12.         scanner.close();
  13.     }
  14. }
  15.  
  16.  
  17. class RomanConverter {
  18.     /**
  19.      * Roman to decimal converter
  20.      *
  21.      * @param n number in decimal format
  22.      * @return string representation of the number in Roman numeral
  23.      */
  24.     public static String toRoman(int n) {
  25.         String rimski = "";
  26.         int l=0;
  27.         while(n!=0) {
  28.             l++;
  29.             int k=n%10;
  30.             n/=10;
  31.             if(l==1) {
  32.                 rimski=digitToRoman(k,"I","V","X")+rimski;
  33.             }
  34.             if(l==2) {
  35.                 rimski=digitToRoman(k,"X","L","C")+rimski;
  36.             }
  37.             if(l==3) {
  38.                 rimski=digitToRoman(k,"C","D","M")+rimski;
  39.             }
  40.             if(l==4) {
  41.                 for(int i=0;i<k;i++){
  42.                     rimski="M"+rimski;
  43.                 }
  44.             }
  45.         }
  46.         return rimski;
  47.     }
  48.     public static String digitToRoman(int k,String str1,String str2,String str3) {
  49.         if(k<=3) {
  50.             String rimski="";
  51.             for(int i=0; i<k; i++)
  52.                 rimski=str1+rimski;
  53.             return rimski;
  54.         }
  55.         else if(k==4) {
  56.             return str1+str2;
  57.         }
  58.         else if(k==5) {
  59.             return str2;
  60.         }
  61.         else if(k>=6&&k<=8) {
  62.             String rimski=str2;
  63.             for(int i=0; i<k-5; i++) {
  64.                 rimski=rimski+str1;
  65.             }
  66.             return rimski;
  67.         }
  68.         else if(k==9) {
  69.             return str1+str3;
  70.         }
  71.         return "";
  72.     }
  73. }
  74.  
  75. === LAB.1 SISTEM ZA BANKARSKO RABOTENJE ===
  76.  
  77. import java.util.*;
  78. import java.util.stream.Collectors;
  79.  
  80. class Account {
  81.     private String name;
  82.     private long id;
  83.     private String balance;
  84.     static long idCounter = 0;
  85.     public Account(String _name, String _balance){
  86.         name = _name;
  87.         balance = _balance;
  88.         id = idCounter;
  89.         idCounter++;
  90.     }
  91.     public Account(Account a){
  92.         name = a.name;
  93.         balance = a.balance;
  94.         id = a.id; 
  95.     }
  96.     public String getName(){return name;}
  97.     public long getId(){return id;}
  98.     public String getBalance(){return balance;}
  99.     public void setBalance(String _balance){balance = _balance;}
  100.     public String toString(){
  101.         return "Name: " + name +"\nBalance: " + balance + "\n";
  102.     }
  103.     public boolean equals(Account a){
  104.         if( a == null) return false;
  105.         if( !a.getName().equals(name)) return false;
  106.         if( !a.getBalance().equals(balance)) return false;
  107.         if( a.getId() != id) return false;
  108.         return true;
  109.     }
  110. }
  111. class Transaction{
  112.     protected long fromId;
  113.     protected long toId;
  114.     protected String description;
  115.     protected String amount;
  116.     public Transaction(long _fromId, long _toId, String _description, String _amount){
  117.         fromId=_fromId; toId=_toId; description=_description; amount=_amount;
  118.     }
  119.     public String getAmount(){return amount;}
  120.     public String getDescription(){return description;}
  121.     public long getFromId(){return fromId;}
  122.     public long getToId(){return toId;}
  123.     public String getFlatAmount(){return "";}
  124.     public int getPercent(){return 0;}
  125.     public boolean equals(Transaction t){
  126.         if(fromId != t.getFromId()){return false;}
  127.         if(toId != t.getToId()){return false;}
  128.         if(!amount.equals(t.getAmount())){return false;}
  129.         if(!description.equals(t.getDescription())){return false;}
  130.         return true;
  131.     }
  132. }
  133. class FlatAmountProvisionTransaction extends Transaction{
  134.     private String flatAmount;
  135.     public FlatAmountProvisionTransaction(long from_id, long to_id,String amount, String flat_amount_provision){
  136.         super(from_id, to_id, "FlatAmount", amount);
  137.         flatAmount = flat_amount_provision;
  138.     }
  139.     public String getFlatAmount(){
  140.         return flatAmount;
  141.     }
  142.     public boolean equals(Transaction t){
  143.         if(t==null){return false;}
  144.         if(!super.equals(t)){return false;}
  145.         if(!flatAmount.equals(t.getFlatAmount())){return false;}
  146.         return true;
  147.     }
  148. }
  149. class FlatPercentProvisionTransaction extends Transaction{
  150.     private int percent;
  151.     public FlatPercentProvisionTransaction(long from_id, long to_id, String amount, int _percent){
  152.         super(from_id, to_id, "FlatPercent", amount);
  153.         percent = _percent;
  154.     }
  155.     public int getPercent(){
  156.         return percent;
  157.     }
  158.     public boolean equals(Transaction t){
  159.         if(t==null){return false;}
  160.         if(!super.equals(t)){return false;}
  161.         if(percent != t.getPercent()){return false;}
  162.         return true;
  163.     }
  164. }
  165. class Bank{
  166.     private String name;
  167.     Account[] accounts;
  168.     private double totalAmountTransferred;
  169.     private double totalProvision;
  170.     public Bank(String _name, Account _accounts[]){
  171.         name = _name;
  172.         accounts = new Account[_accounts.length];
  173.         for(int i=0; i < _accounts.length; i++){
  174.             accounts[i] = new Account(_accounts[i]);
  175.         }
  176.         totalAmountTransferred = 0;
  177.         totalProvision = 0;
  178.     }
  179.     public String totalTransfers(){return String.format("%.2f$", totalAmountTransferred);}
  180.     public String totalProvision(){return String.format("%.2f$", totalProvision);}
  181.     public boolean equals(Bank b){
  182.         if(b == null) {return false;}
  183.         if(!name.equals(b.name)) { return false;}
  184.         if(accounts.length != b.accounts.length) { return false;}
  185.         for(int i = 0; i < accounts.length;i++){
  186.             if( !accounts[i].equals(b.accounts[i])) {return false;}
  187.         }
  188.         return true;
  189.     }
  190.     public Account[] getAccounts(){return accounts;}
  191.     public String toString(){
  192.         String s = "Name: "+name+"\n\n";
  193.         for(int i = 0; i <accounts.length;i++)
  194.             s+=accounts[i].toString();
  195.         return s;
  196.     }
  197.     public boolean makeTransaction(Transaction T){
  198.         boolean fromHere = false, toHere = false;
  199.         double balFrom = 0, amount = 0, balTo = 0, tax = 0;
  200.         int indexFrom = -1, indexTo = -1;
  201.         for(int i=0;i<accounts.length;i++){
  202.             if(accounts[i].getId()==T.getFromId()){
  203.                 String tempFrom = accounts[i].getBalance().substring(0, accounts[i].getBalance().length()-1);
  204.                 String tempTr = T.getAmount().substring(0,T.getAmount().length()-1);
  205.                 balFrom = Double.parseDouble(tempFrom);
  206.                 amount = Double.parseDouble(tempTr);
  207.                 indexFrom = i;
  208.                 if(balFrom>amount)
  209.                     fromHere = true;
  210.             }
  211.             if(accounts[i].getId()==T.getToId()){
  212.                 String tempTo = accounts[i].getBalance().substring(0, accounts[i].getBalance().length()-1);
  213.                 balTo = Double.parseDouble(tempTo);
  214.                 indexTo = i;
  215.                 toHere = true;
  216.             }
  217.         }
  218.         if(fromHere == true&&toHere == true){
  219.             totalAmountTransferred += amount ;
  220.             if(T.getDescription().equals("FlatAmount")){
  221.                 tax = Double.parseDouble(T.getFlatAmount().substring(0,T.getFlatAmount().length()-1));
  222.                 if(indexFrom == indexTo){
  223.                     balFrom = balFrom - tax;
  224.                     String newBalFrom = String.format("%.2f$", balFrom);
  225.                     accounts[indexFrom].setBalance(newBalFrom);
  226.                 }
  227.                 else{
  228.                     balTo = balTo + amount;
  229.                     String newBalTo = String.format("%.2f$", balTo);
  230.                     accounts[indexTo].setBalance(newBalTo);
  231.                     amount = amount + tax;
  232.                     balFrom = balFrom - amount;
  233.                     String newBalFrom = String.format("%.2f$", balFrom);
  234.                     accounts[indexFrom].setBalance(newBalFrom);
  235.                 }
  236.                 totalProvision += tax;
  237.             }
  238.             if(T.getDescription().equals("FlatPercent")){
  239.                 tax = (double)T.getPercent();
  240.                 tax = Math.floor(amount)*tax/100;
  241.                 if(indexFrom == indexTo){
  242.                     balFrom = balFrom - tax;
  243.                     String newBalFrom = String.format("%.2f$", balFrom);
  244.                     accounts[indexFrom].setBalance(newBalFrom);
  245.                 }else{
  246.                     balTo = balTo + amount;
  247.                     amount = amount + tax;
  248.                     balFrom = balFrom - amount;
  249.                     String newBalTo = String.format("%.2f$", balTo);
  250.                     accounts[indexTo].setBalance(newBalTo);
  251.                     String newBalFrom = String.format("%.2f$", balFrom);
  252.                     accounts[indexFrom].setBalance(newBalFrom);
  253.                 }
  254.                 totalProvision += tax;
  255.             }
  256.             return true;
  257.         }
  258.         return false;
  259.     }
  260. }
  261. public class BankTester {
  262.  
  263.     public static void main(String[] args) {
  264.         Scanner jin = new Scanner(System.in);
  265.         String test_type = jin.nextLine();
  266.         switch (test_type) {
  267.             case "typical_usage":
  268.                 testTypicalUsage(jin);
  269.                 break;
  270.             case "equals":
  271.                 testEquals();
  272.                 break;
  273.         }
  274.         jin.close();
  275.     }
  276.  
  277.     private static void testEquals() {
  278.         Account a1 = new Account("Andrej", "20.00$");
  279.         Account a2 = new Account("Andrej", "20.00$");
  280.         Account a3 = new Account("Andrej", "30.00$");
  281.         Account a4 = new Account("Gajduk", "20.00$");
  282.         List<Account> all = Arrays.asList(a1, a2, a3, a4);
  283.         if (!(a1.equals(a1)&&!a1.equals(a2)&&!a2.equals(a1) && !a3.equals(a1)
  284.                 && !a4.equals(a1)
  285.                 && !a1.equals(null))) {
  286.             System.out.println("Your account equals method does not work properly.");
  287.             return;
  288.         }
  289.         Set<Long> ids = all.stream().map(Account::getId).collect(Collectors.toSet());
  290.         if (ids.size() != all.size()) {
  291.             System.out.println("Different accounts have the same IDS. This is not allowed");
  292.             return;
  293.         }
  294.         FlatAmountProvisionTransaction fa1 = new FlatAmountProvisionTransaction(10, 20, "20.00$", "10.00$");
  295.         FlatAmountProvisionTransaction fa2 = new FlatAmountProvisionTransaction(20, 20, "20.00$", "10.00$");
  296.         FlatAmountProvisionTransaction fa3 = new FlatAmountProvisionTransaction(20, 10, "20.00$", "10.00$");
  297.         FlatAmountProvisionTransaction fa4 = new FlatAmountProvisionTransaction(10, 20, "50.00$", "50.00$");
  298.         FlatAmountProvisionTransaction fa5 = new FlatAmountProvisionTransaction(30, 40, "20.00$", "10.00$");
  299.         FlatPercentProvisionTransaction fp1 = new FlatPercentProvisionTransaction(10, 20, "20.00$", 10);
  300.         FlatPercentProvisionTransaction fp2 = new FlatPercentProvisionTransaction(10, 20, "20.00$", 10);
  301.         FlatPercentProvisionTransaction fp3 = new FlatPercentProvisionTransaction(10, 10, "20.00$", 10);
  302.         FlatPercentProvisionTransaction fp4 = new FlatPercentProvisionTransaction(10, 20, "50.00$", 10);
  303.         FlatPercentProvisionTransaction fp5 = new FlatPercentProvisionTransaction(10, 20, "20.00$", 30);
  304.         FlatPercentProvisionTransaction fp6 = new FlatPercentProvisionTransaction(30, 40, "20.00$", 10);
  305.         if (fa1.equals(fa1) &&
  306.                 !fa2.equals(null) &&
  307.                 fa2.equals(fa1) &&
  308.                 fa1.equals(fa2) &&
  309.                 fa1.equals(fa3) &&
  310.                 !fa1.equals(fa4) &&
  311.                 !fa1.equals(fa5) &&
  312.                 !fa1.equals(fp1) &&
  313.                 fp1.equals(fp1) &&
  314.                 !fp2.equals(null) &&
  315.                 fp2.equals(fp1) &&
  316.                 fp1.equals(fp2) &&
  317.                 fp1.equals(fp3) &&
  318.                 !fp1.equals(fp4) &&
  319.                 !fp1.equals(fp5) &&
  320.                 !fp1.equals(fp6)) {
  321.             System.out.println("Your transactions equals methods do not work properly.");
  322.             return;
  323.         }
  324.         Account accounts[] = new Account[]{a1, a2, a3, a4};
  325.         Account accounts1[] = new Account[]{a2, a1, a3, a4};
  326.         Account accounts2[] = new Account[]{a1, a2, a3};
  327.         Account accounts3[] = new Account[]{a1, a2, a3, a4};
  328.        
  329.         Bank b1 = new Bank("Test", accounts);
  330.         Bank b2 = new Bank("Test", accounts1);
  331.         Bank b3 = new Bank("Test", accounts2);
  332.         Bank b4 = new Bank("Sample", accounts);
  333.         Bank b5 = new Bank("Test", accounts3);
  334.  
  335.         if (!(b1.equals(b1) &&
  336.                 !b1.equals(null) &&
  337.                 !b1.equals(b2) &&
  338.                 !b2.equals(b1) &&
  339.                 !b1.equals(b3) &&
  340.                 !b3.equals(b1) &&
  341.                 !b1.equals(b4) &&
  342.                 b1.equals(b5))) {
  343.             System.out.println("Your bank equals method do not work properly.");
  344.             return;
  345.         }
  346.         accounts[2] = a1;
  347.         if (!b1.equals(b5)) {
  348.             System.out.println("Your bank equals method do not work properly.");
  349.             return;
  350.         }
  351.         long from_id = a2.getId();
  352.         long to_id = a3.getId();
  353.         Transaction t = new FlatAmountProvisionTransaction(from_id, to_id, "3.00$", "3.00$");
  354.         b1.makeTransaction(t);
  355.         if (b1.equals(b5)) {
  356.             System.out.println("Your bank equals method do not work properly.");
  357.             return;
  358.         }
  359.         b5.makeTransaction(t);
  360.         if (!b1.equals(b5)) {
  361.             System.out.println("Your bank equals method do not work properly.");
  362.             return;
  363.         }
  364.         System.out.println("All your equals methods work properly.");
  365.     }
  366.  
  367.     private static void testTypicalUsage(Scanner jin) {
  368.         String bank_name = jin.nextLine();
  369.         int num_accounts = jin.nextInt();
  370.         jin.nextLine();
  371.         Account accounts[] = new Account[num_accounts];
  372.         for (int i = 0; i < num_accounts; ++i)
  373.             accounts[i] = new Account(jin.nextLine(), jin.nextLine());
  374.         Bank bank = new Bank(bank_name, accounts);
  375.         while (true) {
  376.             String line = jin.nextLine();
  377.             switch (line) {
  378.                 case "stop":
  379.                     return;
  380.                 case "transaction":
  381.                     String descrption = jin.nextLine();
  382.                     String amount = jin.nextLine();
  383.                     String parameter = jin.nextLine();
  384.                     int from_idx = jin.nextInt();
  385.                     int to_idx = jin.nextInt();
  386.                     jin.nextLine();
  387.                     Transaction t = getTransaction(descrption, from_idx, to_idx, amount, parameter, bank);
  388.                     System.out.println("Transaction amount: " + t.getAmount());
  389.                     System.out.println("Transaction description: " + t.getDescription());
  390.                     System.out.println("Transaction successful? " + bank.makeTransaction(t));
  391.                     break;
  392.                 case "print":
  393.                     System.out.println(bank.toString());
  394.                     System.out.println("Total provisions: " + bank.totalProvision());
  395.                     System.out.println("Total transfers: " + bank.totalTransfers());
  396.                     System.out.println();
  397.                     break;
  398.             }
  399.         }
  400.     }
  401.  
  402.     private static Transaction getTransaction(String description, int from_idx, int to_idx, String amount, String o, Bank bank) {
  403.         switch (description) {
  404.             case "FlatAmount":
  405.                 return new FlatAmountProvisionTransaction(bank.getAccounts()[from_idx].getId(),
  406.                         bank.getAccounts()[to_idx].getId(), amount, o);
  407.             case "FlatPercent":
  408.                 return new FlatPercentProvisionTransaction(bank.getAccounts()[from_idx].getId(),
  409.                         bank.getAccounts()[to_idx].getId(), amount, Integer.parseInt(o));
  410.         }
  411.         return null;
  412.     }
  413.  
  414.  
  415. }
  416.  
  417.  
  418. === LAB.1 NIZA OD CELI BROEVI ===
  419.  
  420. import java.io.ByteArrayInputStream;
  421. import java.io.IOException;
  422. import java.io.InputStream;
  423. import java.io.InputStreamReader;
  424. import java.util.Arrays;
  425. import java.util.Random;
  426. import java.util.Scanner;
  427. import java.io.BufferedReader;
  428.  
  429. final class IntegerArray{
  430.     private int array[];
  431.     public IntegerArray(int [] _array){
  432.         array = new int [_array.length];
  433.         for(int i = 0; i < _array.length; i++)
  434.             array[i] = _array[i];
  435.     }
  436.     public int length(){
  437.         return array.length;
  438.     }
  439.     public int getElementAt(int i){
  440.         return array[i];
  441.     }
  442.     public int sum(){
  443.         int sum = 0;
  444.         for(int i = 0; i < array.length; i++)
  445.             sum+=array[i];
  446.         return sum;
  447.     }
  448.     public double average(){
  449.         return this.sum()/(double)array.length;
  450.     }
  451.     public IntegerArray getSorted(){
  452.         int temp [] = new int [array.length];
  453.         for(int i = 0; i < array.length; i++){
  454.             temp[i] = array[i];
  455.         }
  456.         for(int i=0; i < temp.length; i++){  
  457.             for(int j=1; j < temp.length-i; j++){  
  458.                      if(temp[j-1] > temp[j]){  
  459.                             int tempNum = temp[j-1];  
  460.                             temp[j-1] = temp[j];  
  461.                             temp[j] = tempNum;  
  462.                     }  
  463.                      
  464.             }  
  465.         }
  466.         IntegerArray ret = new IntegerArray(temp);
  467.         return ret;
  468.     }
  469.     public boolean equals(IntegerArray ia){
  470.         if(array.length != ia.length()) return false;
  471.         for(int i = 0; i < array.length; i++){
  472.             if(array[i] != ia.getElementAt(i))
  473.                 return false;
  474.         }
  475.         return true;
  476.     }
  477.     public IntegerArray concat(IntegerArray ia){
  478.         int temp [] = new int [array.length+ia.length()];
  479.         for(int i = 0; i < array.length; i++){
  480.             temp[i] = array[i];
  481.         }
  482.         for(int i = 0;i < ia.length(); i++){
  483.             temp[i+array.length] = ia.getElementAt(i);
  484.         }
  485.         IntegerArray ret = new IntegerArray(temp);
  486.         return ret;
  487.     }
  488.     public String toString(){
  489.         String ret = "[";
  490.         for(int i = 0; i < array.length; i++){
  491.             ret += array[i];
  492.             if(i != array.length - 1)
  493.                 ret += ", ";
  494.         }
  495.         ret += "]";
  496.         return ret;
  497.     }
  498. }
  499.  
  500. class ArrayReader{
  501.     public static IntegerArray readIntegerArray(InputStream input) throws IOException{
  502.        
  503.         BufferedReader br = new BufferedReader(new InputStreamReader(input));
  504.         String line = br.readLine();
  505.         String [] tempInt = line.split(" ");
  506.         int N = Integer.parseInt(tempInt[0]);
  507.         int [] array = new int [N];
  508.         if(tempInt.length == 1)
  509.         {
  510.             line = br.readLine();
  511.             tempInt = line.split(" ");
  512.             for(int i = 0; i<N;i++){
  513.                 array[i] = Integer.parseInt(tempInt[i]);
  514.             }
  515.         }
  516.         else{
  517.             for(int i = 0; i<N;i++){
  518.                 array[i] = Integer.parseInt(tempInt[i+1]);
  519.             }
  520.         }
  521.         IntegerArray ret = new IntegerArray (array);
  522.         return ret;
  523.     }
  524. }
  525.  
  526.  
  527. public class IntegerArrayTester {
  528.  
  529.     public static void main(String[] args) throws IOException {
  530.         Scanner scanner = new Scanner(System.in);
  531.         String s = scanner.nextLine();
  532.         IntegerArray ia = null;
  533.         switch (s) {
  534.             case "testSimpleMethods":
  535.                 ia = new IntegerArray(generateRandomArray(scanner.nextInt()));
  536.                 testSimpleMethods(ia);
  537.                 break;
  538.             case "testConcat":
  539.                 testConcat(scanner);
  540.                 break;
  541.             case "testEquals":
  542.                 testEquals(scanner);
  543.                 break;
  544.             case "testSorting":
  545.                 testSorting(scanner);
  546.                 break;
  547.             case "testReading":
  548.                 testReading(new ByteArrayInputStream(scanner.nextLine().getBytes()));
  549.                 break;
  550.             case "testImmutability":
  551.                 int a[] = generateRandomArray(scanner.nextInt());
  552.                 ia = new IntegerArray(a);
  553.                 testSimpleMethods(ia);
  554.                 testSimpleMethods(ia);
  555.                 IntegerArray sorted_ia = ia.getSorted();
  556.                 testSimpleMethods(ia);
  557.                 testSimpleMethods(sorted_ia);
  558.                 sorted_ia.getSorted();
  559.                 testSimpleMethods(sorted_ia);
  560.                 testSimpleMethods(ia);
  561.                 a[0] += 2;
  562.                 testSimpleMethods(ia);
  563.                 ia = ArrayReader.readIntegerArray(new ByteArrayInputStream(integerArrayToString(ia).getBytes()));
  564.                 testSimpleMethods(ia);
  565.                 break;
  566.         }
  567.         scanner.close();
  568.     }
  569.  
  570.     static void testReading(InputStream in) throws IOException {
  571.         IntegerArray read = ArrayReader.readIntegerArray(in);
  572.         System.out.println(read);
  573.     }
  574.  
  575.     static void testSorting(Scanner scanner) {
  576.         int[] a = readArray(scanner);
  577.         IntegerArray ia = new IntegerArray(a);
  578.         System.out.println(ia.getSorted());
  579.     }
  580.  
  581.     static void testEquals(Scanner scanner) {
  582.         int[] a = readArray(scanner);
  583.         int[] b = readArray(scanner);
  584.         int[] c = readArray(scanner);
  585.         IntegerArray ia = new IntegerArray(a);
  586.         IntegerArray ib = new IntegerArray(b);
  587.         IntegerArray ic = new IntegerArray(c);
  588.         System.out.println(ia.equals(ib));
  589.         System.out.println(ia.equals(ic));
  590.         System.out.println(ib.equals(ic));
  591.     }
  592.  
  593.     static void testConcat(Scanner scanner) {
  594.         int[] a = readArray(scanner);
  595.         int[] b = readArray(scanner);
  596.         IntegerArray array1 = new IntegerArray(a);
  597.         IntegerArray array2 = new IntegerArray(b);
  598.         IntegerArray concatenated = array1.concat(array2);
  599.         System.out.println(concatenated);
  600.     }
  601.  
  602.     static void testSimpleMethods(IntegerArray ia) {
  603.         System.out.print(integerArrayToString(ia));
  604.         System.out.println(ia);
  605.         System.out.println(ia.sum());
  606.         System.out.printf("%.2f\n", ia.average());
  607.     }
  608.  
  609.  
  610.     static String integerArrayToString(IntegerArray ia) {
  611.         StringBuilder sb = new StringBuilder();
  612.         sb.append(ia.length()).append('\n');
  613.         for (int i = 0; i < ia.length(); ++i)
  614.             sb.append(ia.getElementAt(i)).append(' ');
  615.         sb.append('\n');
  616.         return sb.toString();
  617.     }
  618.  
  619.     static int[] readArray(Scanner scanner) {
  620.         int n = scanner.nextInt();
  621.         int[] a = new int[n];
  622.         for (int i = 0; i < n; ++i) {
  623.             a[i] = scanner.nextInt();
  624.         }
  625.         return a;
  626.     }
  627.  
  628.  
  629.     static int[] generateRandomArray(int k) {
  630.         Random rnd = new Random(k);
  631.         int n = rnd.nextInt(8) + 2;
  632.         int a[] = new int[n];
  633.         for (int i = 0; i < n; ++i) {
  634.             a[i] = rnd.nextInt(20) - 5;
  635.         }
  636.         return a;
  637.     }
  638.  
  639. }
  640.  
  641. === LAB.2 MATRICA OD REALNI BROEVI ===
  642.  
  643. import java.util.*;
  644. import java.io.*;
  645. import java.text.DecimalFormat;
  646.  
  647. class InsufficientElementsException extends Exception {
  648.     public InsufficientElementsException (String message){
  649.         super(message);
  650.     }
  651. }
  652. class InvalidRowNumberException extends Exception{
  653.     public InvalidRowNumberException (String message){
  654.         super(message);
  655.     }
  656. }
  657. class InvalidColumnNumberException  extends Exception{
  658.     public InvalidColumnNumberException (String message){
  659.         super(message);
  660.     }
  661. }
  662. class DoubleMatrix{
  663.     private double [][] matrix;
  664.     private int rows;
  665.     private int columns;
  666.     public DoubleMatrix(double a[], int m, int n) throws InsufficientElementsException{
  667.         matrix = new double [m][n];
  668.         if( a.length >= m*n){
  669.             rows = m;
  670.             columns = n;
  671.             int k = a.length - m*n;
  672.             for(int i = 0; i < m; i++){
  673.                 for(int j = 0; j < n; j++){
  674.                     matrix[i][j] = a[k];
  675.                     k++;
  676.                 }
  677.             }
  678.         }
  679.         else if ( a.length < m*n) throw new InsufficientElementsException("Insufficient number of elements");
  680.     }
  681.     public String getDimensions(){
  682.         return "["+rows+" x "+columns+"]";
  683.     }
  684.     public int rows(){
  685.         return rows;
  686.     }
  687.     public int columns(){
  688.         return columns;
  689.     }
  690.     public double maxElementAtRow(int row) throws InvalidRowNumberException {
  691.         if(row<1 || row>rows)
  692.             throw new InvalidRowNumberException("Invalid row number");
  693.         double max = matrix[row-1][0];
  694.         for (int i = 1; i < columns; i ++){
  695.             if( matrix[row-1][i] > max)
  696.                 max = matrix[row-1][i];
  697.         }
  698.         return max;
  699.     }
  700.     public double maxElementAtColumn(int column) throws InvalidColumnNumberException  {
  701.         if(column<1 || column>columns)
  702.             throw new InvalidColumnNumberException ("Invalid column number");
  703.         double max = matrix[0][column-1];
  704.         for (int i = 1; i < rows; i ++){
  705.             if( matrix[i][column - 1] > max)
  706.                 max = matrix[i][column - 1];
  707.         }
  708.         return max;
  709.     }
  710.     public double sum(){
  711.         double sum = 0;
  712.         for(int i = 0; i < rows; i++){
  713.             for(int j = 0; j < columns; j++){
  714.                 sum += matrix[i][j];
  715.             }
  716.         }
  717.         return sum;
  718.     }
  719.     public double [] toSortedArray(){
  720.         Double [] array = new Double [rows*columns];
  721.         for(int i = 0, k =0; i < rows; i++){
  722.             for(int j = 0; j < columns; j++, k++){
  723.                 array[k] = new Double(matrix[i][j]);
  724.             }
  725.         }
  726.         Arrays.sort(array,Collections.reverseOrder());
  727.         double [] ret = new double [rows*columns];
  728.         for(int i = 0; i < rows*columns; i++)
  729.             ret[i] = array[i].doubleValue();
  730.         return ret;
  731.     }
  732.      public String toString()
  733.         {
  734.             StringBuilder s = new StringBuilder();
  735.             for(int i=0;i<rows();i++)
  736.             {
  737.                 for(int j=0;j<columns();j++)
  738.                 {
  739.                     s.append(String.format("%.2f",matrix[i][j]));
  740.                     if(j!=columns()-1) s.append("\t");
  741.                 }
  742.                 if(i!=rows()-1) s.append("\n");
  743.             }
  744.             return s.toString();
  745.         }
  746.      public boolean equals(Object ob){
  747.          if (this == ob)
  748.             return true;
  749.          if (ob == null)
  750.             return false;
  751.          if (getClass() != ob.getClass())
  752.             return false;
  753.          DoubleMatrix other = (DoubleMatrix) ob;
  754.          if(rows != other.rows()) return false;
  755.          if(columns != other.columns()) return false;
  756.          for( int i = 0; i < rows; i++){
  757.              for(int j = 0; j < columns; j++){
  758.                  if(matrix[i][j]!=other.matrix[i][j])
  759.                      return false;
  760.              }
  761.          }
  762.          return true;
  763.      }
  764.      public int hashCode() {
  765.             final int prime = 31;
  766.             int result = 1;
  767.             result = prime * result + Arrays.deepHashCode(matrix);
  768.             return result;
  769.         }
  770. }
  771. class MatrixReader{
  772.     public static DoubleMatrix read(InputStream input) throws Exception{
  773.         Scanner scanner = new Scanner(input);
  774.         int m = scanner.nextInt();
  775.         int n = scanner.nextInt();
  776.         double [] matrixArray = new double [m*n];
  777.         for(int i = 0; i < m*n; i++)
  778.             matrixArray[i]=scanner.nextDouble();
  779.         scanner.close();
  780.         DoubleMatrix matrix = new DoubleMatrix(matrixArray,m,n);
  781.         return matrix;
  782.     }
  783. }
  784. public class DoubleMatrixTester {
  785.  
  786.     public static void main(String[] args) throws Exception {
  787.         Scanner scanner = new Scanner(System.in);
  788.  
  789.         int tests = scanner.nextInt();
  790.         DoubleMatrix fm = null;
  791.  
  792.         double[] info = null;
  793.  
  794.         DecimalFormat format = new DecimalFormat("0.00");
  795.  
  796.         for (int t = 0; t < tests; t++) {
  797.  
  798.             String operation = scanner.next();
  799.  
  800.             switch (operation) {
  801.                 case "READ": {
  802.                     int N = scanner.nextInt();
  803.                     int R = scanner.nextInt();
  804.                     int C = scanner.nextInt();
  805.  
  806.                     double[] f = new double[N];
  807.  
  808.                     for (int i = 0; i < f.length; i++)
  809.                         f[i] = scanner.nextDouble();
  810.  
  811.                     try {
  812.                         fm = new DoubleMatrix(f, R, C);
  813.                         info = Arrays.copyOf(f, f.length);
  814.  
  815.                     } catch (InsufficientElementsException e) {
  816.                         System.out.println("Exception caught: " + e.getMessage());
  817.                     }
  818.  
  819.                     break;
  820.                 }
  821.  
  822.                 case "INPUT_TEST": {
  823.                     int R = scanner.nextInt();
  824.                     int C = scanner.nextInt();
  825.  
  826.                     StringBuilder sb = new StringBuilder();
  827.  
  828.                     sb.append(R + " " + C + "\n");
  829.  
  830.                     scanner.nextLine();
  831.  
  832.                     for (int i = 0; i < R; i++)
  833.                         sb.append(scanner.nextLine() + "\n");
  834.  
  835.                     fm = MatrixReader.read(new ByteArrayInputStream(sb
  836.                             .toString().getBytes()));
  837.  
  838.                     info = new double[R * C];
  839.                     Scanner tempScanner = new Scanner(new ByteArrayInputStream(sb
  840.                             .toString().getBytes()));
  841.                     tempScanner.nextDouble();
  842.                     tempScanner.nextDouble();
  843.                     for (int z = 0; z < R * C; z++) {
  844.                         info[z] = tempScanner.nextDouble();
  845.                     }
  846.  
  847.                     tempScanner.close();
  848.  
  849.                     break;
  850.                 }
  851.  
  852.                 case "PRINT": {
  853.                     System.out.println(fm.toString());
  854.                     break;
  855.                 }
  856.  
  857.                 case "DIMENSION": {
  858.                     System.out.println("Dimensions: " + fm.getDimensions());
  859.                     break;
  860.                 }
  861.  
  862.                 case "COUNT_ROWS": {
  863.                     System.out.println("Rows: " + fm.rows());
  864.                     break;
  865.                 }
  866.  
  867.                 case "COUNT_COLUMNS": {
  868.                     System.out.println("Columns: " + fm.columns());
  869.                     break;
  870.                 }
  871.  
  872.                 case "MAX_IN_ROW": {
  873.                     int row = scanner.nextInt();
  874.                     try {
  875.                         System.out.println("Max in row: "
  876.                                 + format.format(fm.maxElementAtRow(row)));
  877.                     } catch (InvalidRowNumberException e) {
  878.                         System.out.println("Exception caught: " + e.getMessage());
  879.                     }
  880.                     break;
  881.                 }
  882.  
  883.                 case "MAX_IN_COLUMN": {
  884.                     int col = scanner.nextInt();
  885.                     try {
  886.                         System.out.println("Max in column: "
  887.                                 + format.format(fm.maxElementAtColumn(col)));
  888.                     } catch (InvalidColumnNumberException e) {
  889.                         System.out.println("Exception caught: " + e.getMessage());
  890.                     }
  891.                     break;
  892.                 }
  893.  
  894.                 case "SUM": {
  895.                     System.out.println("Sum: " + format.format(fm.sum()));
  896.                     break;
  897.                 }
  898.  
  899.                 case "CHECK_EQUALS": {
  900.                     int val = scanner.nextInt();
  901.  
  902.                     int maxOps = val % 7;
  903.  
  904.                     for (int z = 0; z < maxOps; z++) {
  905.                         double work[] = Arrays.copyOf(info, info.length);
  906.  
  907.                         int e1 = (31 * z + 7 * val + 3 * maxOps) % info.length;
  908.                         int e2 = (17 * z + 3 * val + 7 * maxOps) % info.length;
  909.  
  910.                         if (e1 > e2) {
  911.                             double temp = work[e1];
  912.                             work[e1] = work[e2];
  913.                             work[e2] = temp;
  914.                         }
  915.  
  916.                         DoubleMatrix f1 = fm;
  917.                         DoubleMatrix f2 = new DoubleMatrix(work, fm.rows(),
  918.                                 fm.columns());
  919.                         System.out
  920.                                 .println("Equals check 1: "
  921.                                         + f1.equals(f2)
  922.                                         + " "
  923.                                         + f2.equals(f1)
  924.                                         + " "
  925.                                         + (f1.hashCode() == f2.hashCode()&&f1
  926.                                         .equals(f2)));
  927.                     }
  928.  
  929.                     if (maxOps % 2 == 0) {
  930.                         DoubleMatrix f1 = fm;
  931.                         DoubleMatrix f2 = new DoubleMatrix(new double[]{3.0, 5.0,
  932.                                 7.5}, 1, 1);
  933.  
  934.                         System.out
  935.                                 .println("Equals check 2: "
  936.                                         + f1.equals(f2)
  937.                                         + " "
  938.                                         + f2.equals(f1)
  939.                                         + " "
  940.                                         + (f1.hashCode() == f2.hashCode() && f1
  941.                                         .equals(f2)));
  942.                     }
  943.  
  944.                     break;
  945.                 }
  946.  
  947.                 case "SORTED_ARRAY": {
  948.                     double[] arr = fm.toSortedArray();
  949.  
  950.                     String arrayString = "[";
  951.  
  952.                     if (arr.length > 0)
  953.                         arrayString += format.format(arr[0]) + "";
  954.  
  955.                     for (int i = 1; i < arr.length; i++)
  956.                         arrayString += ", " + format.format(arr[i]);
  957.  
  958.                     arrayString += "]";
  959.  
  960.                     System.out.println("Sorted array: " + arrayString);
  961.                     break;
  962.                 }
  963.  
  964.             }
  965.  
  966.         }
  967.  
  968.         scanner.close();
  969.     }
  970. }
  971.  
  972. === LAB.2 KONTAKTI ===
  973.  
  974. import java.text.DecimalFormat;
  975. import java.util.Arrays;
  976. import java.util.Scanner;
  977.  
  978.  
  979. abstract class Contact{
  980.     protected String date;
  981.     public Contact(String _date){
  982.         date = _date;
  983.     }
  984.     public boolean isNewerThan(Contact c){
  985.         String [] dateArray = date.split("-");
  986.         String [] dateArrayOther = c.date.split("-");
  987.         int year = Integer.parseInt(dateArray[0]);
  988.         int yearOther = Integer.parseInt(dateArrayOther[0]);
  989.         if (year > yearOther) return true;
  990.         else if (year < yearOther) return false;
  991.         else{
  992.             int month = Integer.parseInt(dateArray[1]);
  993.             int monthOther = Integer.parseInt(dateArrayOther[1]);
  994.             if (month > monthOther) return true;
  995.             else if (month < monthOther) return false;
  996.             else{
  997.                 int day = Integer.parseInt(dateArray[2]);
  998.                 int dayOther = Integer.parseInt(dateArrayOther[2]);
  999.                 if (day > dayOther) return true;
  1000.                 else return false;
  1001.             }
  1002.         }
  1003.     }
  1004.     abstract public String getType();
  1005.     public String getEmail() {
  1006.         return "";
  1007.     }
  1008.     public String getPhone() {
  1009.         return "";
  1010.     }
  1011. }
  1012. class EmailContact extends Contact{
  1013.     private String email;
  1014.     public EmailContact(String _date, String _email){
  1015.         super(_date);
  1016.         email = _email;
  1017.     }
  1018.     public String getEmail(){
  1019.         return email;
  1020.     }
  1021.     public String getType(){
  1022.         return "Email";
  1023.     }
  1024. }
  1025. class PhoneContact extends Contact{
  1026.     enum Operator{
  1027.         VIP, ONE, TMOBILE
  1028.     }
  1029.     private String phone;
  1030.     private Operator operator;
  1031.     public PhoneContact(String _date, String _phone){
  1032.         super(_date);
  1033.         phone = _phone;
  1034.         if ( phone.charAt(2) == '0' || phone.charAt(2) == '1' || phone.charAt(2) == '2')
  1035.             operator = Operator.TMOBILE;
  1036.         else if ( phone.charAt(2) == '5' || phone.charAt(2) == '6')
  1037.             operator = Operator.ONE;
  1038.         else if ( phone.charAt(2) == '7' || phone.charAt(2) == '8')
  1039.             operator = Operator.VIP;
  1040.     }
  1041.     public String getPhone(){
  1042.         return phone;
  1043.     }
  1044.     public Operator getOperator(){
  1045.         return operator;
  1046.     }
  1047.     public String getType(){
  1048.         return "Phone";
  1049.     }
  1050. }
  1051.  
  1052. class Student{
  1053.     private String firstName;
  1054.     private String lastName;
  1055.     private String city;
  1056.     private int age;
  1057.     private long index;
  1058.     private Contact [] contacts;
  1059.     public Student(String _firstName, String _lastName, String _city, int _age, long _index){
  1060.         firstName = _firstName;
  1061.         lastName = _lastName;
  1062.         city = _city;
  1063.         age = _age;
  1064.         index = _index;
  1065.         contacts = new Contact [0];
  1066.     }
  1067.     public void addEmailContact(String date, String email){
  1068.         Contact newCon = new EmailContact (date, email);
  1069.         Contact [] temp = new Contact [contacts.length+1];
  1070.         for (int i = 0; i<contacts.length; i++)
  1071.             temp[i] = contacts[i];
  1072.         temp[contacts.length] = newCon;
  1073.         contacts = temp;
  1074.     }
  1075.     public void addPhoneContact(String date, String phone){
  1076.         Contact newCon = new PhoneContact (date, phone);
  1077.         Contact [] temp = new Contact [contacts.length+1];
  1078.         for (int i = 0; i<contacts.length; i++)
  1079.             temp[i] = contacts[i];
  1080.         temp[contacts.length] = newCon;
  1081.         contacts = temp;
  1082.     }
  1083.     public Contact [] getEmailContacts(){
  1084.         int counter = 0;
  1085.         for (int i = 0; i < contacts.length; i++){
  1086.             if ( contacts[i].getType().equals("Email"))
  1087.                 counter++;
  1088.         }
  1089.         Contact [] array = new EmailContact[counter];
  1090.         int inCounter = 0;
  1091.         for (int i = 0; i < contacts.length; i++){
  1092.             if ( contacts[i].getType().equals("Email")){
  1093.                 array[inCounter] = contacts[i];
  1094.                 inCounter++;
  1095.             }
  1096.         }
  1097.         return array;
  1098.     }
  1099.     public Contact [] getPhoneContacts(){
  1100.         int counter = 0;
  1101.         for (int i = 0; i < contacts.length; i++){
  1102.             if ( contacts[i].getType().equals("Phone"))
  1103.                 counter++;
  1104.         }
  1105.         Contact [] array = new PhoneContact[counter];
  1106.         int inCounter = 0;
  1107.         for (int i = 0; i < contacts.length; i++){
  1108.             if ( contacts[i].getType().equals("Phone")){
  1109.                 array[inCounter] = contacts[i];
  1110.                 inCounter++;
  1111.             }
  1112.         }
  1113.         return array;
  1114.     }
  1115.     public String getCity(){
  1116.         return city;
  1117.     }
  1118.     public String getFullName(){
  1119.         return firstName+" "+lastName;
  1120.     }
  1121.     public long getIndex(){
  1122.         return index;
  1123.     }
  1124.     public int getNumberContacts(){
  1125.         return contacts.length;
  1126.     }
  1127.     public Contact getLatestContact(){
  1128.         Contact ret = contacts[0];
  1129.         for( int i = 1; i < contacts.length; i++){
  1130.             if(contacts[i].isNewerThan(ret))
  1131.                 ret = contacts[i];
  1132.         }
  1133.         return ret;
  1134.     }
  1135.     public String toString(){
  1136.         boolean firstPhone = true, firstEmail = true;
  1137.        
  1138.         String ret = "{\"ime\":\"" + firstName+ "\", \"prezime\":\"" + lastName
  1139.                 +"\", \"vozrast\":" + age + ", \"grad\":\"" + city +
  1140.                 "\", \"indeks\":" + index + ", \"telefonskiKontakti\":[";
  1141.         for(int i = 0; i < contacts.length; i++){
  1142.             if(contacts[i].getType().equals("Phone")){
  1143.                 if(firstPhone == true){
  1144.                     ret+="\""+contacts[i].getPhone()+"\"";
  1145.                     firstPhone = false;
  1146.                 }
  1147.                 else{
  1148.                     ret+=", \""+contacts[i].getPhone()+"\"";
  1149.                 }
  1150.             }
  1151.         }
  1152.         ret += "], \"emailKontakti\":[";
  1153.         for(int i = 0; i < contacts.length; i++){
  1154.             if(contacts[i].getType().equals("Email")){
  1155.                 if(firstEmail == true){
  1156.                     ret+="\""+contacts[i].getEmail()+"\"";
  1157.                     firstEmail = false;
  1158.                 }
  1159.                 else{
  1160.                     ret+=", \""+contacts[i].getEmail()+"\"";
  1161.                 }
  1162.             }
  1163.         }
  1164.         ret += "]}";
  1165.         return ret;
  1166.     }
  1167. }
  1168.  
  1169. class Faculty{
  1170.     private String name;
  1171.     private Student [] students;
  1172.     public Faculty (String _name, Student [] _students){
  1173.         name = _name;
  1174.         students = new Student [_students.length];
  1175.         for ( int i = 0; i < _students.length; i++){
  1176.             students[i] = _students[i];
  1177.         }
  1178.     }
  1179.     public int countStudentsFromCity( String cityName){
  1180.         int counter = 0;
  1181.         for (int i = 0; i < students.length; i++){
  1182.             if ( students[i].getCity().equals(cityName))
  1183.                 counter++;
  1184.         }
  1185.         return counter;
  1186.     }
  1187.     public Student getStudent(long index){
  1188.         for (int i = 0; i < students.length; i++){
  1189.             if ( students[i].getIndex() == index)
  1190.                 return students[i];
  1191.         }
  1192.         return null;
  1193.     }
  1194.     public Student countStudentsFromCity(long index){
  1195.         Student ret = null;
  1196.         for (int i = 0; i < students.length; i++){
  1197.             if ( students[i].getIndex() == index)
  1198.                 ret = students[i];
  1199.         }
  1200.         return ret;
  1201.     }
  1202.     public double getAverageNumberOfContacts(){
  1203.         int sum = 0;
  1204.         for (int i = 0; i < students.length; i++){
  1205.             sum += students[i].getNumberContacts();
  1206.         }
  1207.         double average = (double)sum / students.length;
  1208.         return average;
  1209.     }
  1210.     public Student getStudentWithMostContacts(){
  1211.         Student ret = students[0];
  1212.         for (int i = 1; i < students.length; i++){
  1213.             if ( students[i].getNumberContacts() > ret.getNumberContacts())
  1214.                 ret = students[i];
  1215.             else if (students[i].getNumberContacts() == ret.getNumberContacts())
  1216.                 if( students[i].getIndex() > ret.getIndex() )
  1217.                     ret = students[i];
  1218.         }
  1219.         return ret;
  1220.     }
  1221.     public String toString(){
  1222.         boolean firstStudent = true;
  1223.         String ret = "{\"fakultet\":\"" + name + "\", \"studenti\":[";
  1224.         for ( int i = 0; i < students.length; i++){
  1225.             if(firstStudent == true){
  1226.                 ret += students[i].toString();
  1227.                 firstStudent = false;
  1228.             }
  1229.             else
  1230.                 ret += ", "+ students[i].toString();
  1231.         }
  1232.         ret += "]}";
  1233.         return ret;
  1234.     }
  1235. }
  1236.  
  1237. public class ContactsTester {
  1238.  
  1239.     public static void main(String[] args) {
  1240.         Scanner scanner = new Scanner(System.in);
  1241.  
  1242.         int tests = scanner.nextInt();
  1243.         Faculty faculty = null;
  1244.  
  1245.         int rvalue = 0;
  1246.         long rindex = -1;
  1247.  
  1248.         DecimalFormat df = new DecimalFormat("0.00");
  1249.  
  1250.         for (int t = 0; t < tests; t++) {
  1251.  
  1252.             rvalue++;
  1253.             String operation = scanner.next();
  1254.  
  1255.             switch (operation) {
  1256.             case "CREATE_FACULTY": {
  1257.                 String name = scanner.nextLine().trim();
  1258.                 int N = scanner.nextInt();
  1259.  
  1260.                 Student[] students = new Student[N];
  1261.  
  1262.                 for (int i = 0; i < N; i++) {
  1263.                     rvalue++;
  1264.  
  1265.                     String firstName = scanner.next();
  1266.                     String lastName = scanner.next();
  1267.                     String city = scanner.next();
  1268.                     int age = scanner.nextInt();
  1269.                     long index = scanner.nextLong();
  1270.  
  1271.                     if ((rindex == -1) || (rvalue % 13 == 0))
  1272.                         rindex = index;
  1273.  
  1274.                     Student student = new Student(firstName, lastName, city,
  1275.                             age, index);
  1276.                     students[i] = student;
  1277.                 }
  1278.  
  1279.                 faculty = new Faculty(name, students);
  1280.                 break;
  1281.             }
  1282.  
  1283.             case "ADD_EMAIL_CONTACT": {
  1284.                 long index = scanner.nextInt();
  1285.                 String date = scanner.next();
  1286.                 String email = scanner.next();
  1287.  
  1288.                 rvalue++;
  1289.  
  1290.                 if ((rindex == -1) || (rvalue % 3 == 0))
  1291.                     rindex = index;
  1292.  
  1293.                 faculty.getStudent(index).addEmailContact(date, email);
  1294.                 break;
  1295.             }
  1296.  
  1297.             case "ADD_PHONE_CONTACT": {
  1298.                 long index = scanner.nextInt();
  1299.                 String date = scanner.next();
  1300.                 String phone = scanner.next();
  1301.  
  1302.                 rvalue++;
  1303.  
  1304.                 if ((rindex == -1) || (rvalue % 3 == 0))
  1305.                     rindex = index;
  1306.  
  1307.                 faculty.getStudent(index).addPhoneContact(date, phone);
  1308.                 break;
  1309.             }
  1310.  
  1311.             case "CHECK_SIMPLE": {
  1312.                 System.out.println("Average number of contacts: "
  1313.                         + df.format(faculty.getAverageNumberOfContacts()));
  1314.  
  1315.                 rvalue++;
  1316.  
  1317.                 String city = faculty.getStudent(rindex).getCity();
  1318.                 System.out.println("Number of students from " + city + ": "
  1319.                         + faculty.countStudentsFromCity(city));
  1320.  
  1321.                 break;
  1322.             }
  1323.  
  1324.             case "CHECK_DATES": {
  1325.  
  1326.                 rvalue++;
  1327.  
  1328.                 System.out.print("Latest contact: ");
  1329.                 Contact latestContact = faculty.getStudent(rindex)
  1330.                         .getLatestContact();
  1331.                 if (latestContact.getType().equals("Email"))
  1332.                     System.out.println(((EmailContact) latestContact)
  1333.                             .getEmail());
  1334.                 if (latestContact.getType().equals("Phone"))
  1335.                     System.out.println(((PhoneContact) latestContact)
  1336.                             .getPhone()
  1337.                             + " ("
  1338.                             + ((PhoneContact) latestContact).getOperator()
  1339.                                     .toString() + ")");
  1340.                
  1341.                 if (faculty.getStudent(rindex).getEmailContacts().length > 0&&faculty.getStudent(rindex).getPhoneContacts().length > 0) {
  1342.                     System.out.print("Number of email and phone contacts: ");
  1343.                     System.out
  1344.                             .println(faculty.getStudent(rindex)
  1345.                                     .getEmailContacts().length
  1346.                                     + " "
  1347.                                     + faculty.getStudent(rindex)
  1348.                                             .getPhoneContacts().length);
  1349.  
  1350.                     System.out.print("Comparing dates: ");
  1351.                     int posEmail = rvalue
  1352.                             % faculty.getStudent(rindex).getEmailContacts().length;
  1353.                     int posPhone = rvalue
  1354.                             % faculty.getStudent(rindex).getPhoneContacts().length;
  1355.  
  1356.                     System.out.println(faculty.getStudent(rindex)
  1357.                             .getEmailContacts()[posEmail].isNewerThan(faculty
  1358.                             .getStudent(rindex).getPhoneContacts()[posPhone]));
  1359.                 }
  1360.  
  1361.                 break;
  1362.             }
  1363.  
  1364.             case "PRINT_FACULTY_METHODS": {
  1365.                 System.out.println("Faculty: " + faculty.toString());
  1366.                 System.out.println("Student with most contacts: "
  1367.                         + faculty.getStudentWithMostContacts().toString());
  1368.                 break;
  1369.             }
  1370.  
  1371.             }
  1372.  
  1373.         }
  1374.  
  1375.         scanner.close();
  1376.     }
  1377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement