Advertisement
Filip_Markoski

1.Lab1.1 Average (Solved)

Oct 18th, 2017
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Scanner;
  5.  
  6. public class Array<E> {
  7.  
  8.     private E data[];
  9.     private int len;
  10.  
  11.     public Array(int size) {
  12.         data = (E[]) new Object[size];
  13.         this.len = size;
  14.     }
  15.  
  16.     public int getLength() {
  17.         return len;
  18.     }
  19.    
  20.     /*
  21.    
  22.     public void delete(int position) {
  23.         if (position >= 0 && position < size) {
  24.             for (int i = position - 1; i < size - 1; i++)
  25.                 data[i] = data[i + 1];
  26.             size--;
  27.         }
  28.         else throw new IndexOutOfBoundsException();
  29.     }
  30.     public int find(E o){
  31.         for (int i = 0; i < size; i++)
  32.             if (o.equals(data[i]))
  33.                 return i;
  34.         return -1;
  35.     }
  36.     public void insert(int position, E o){
  37.         if (position >= 0 && position < size) {
  38.             E[] newData = (E[])new Object[size + 1];
  39.             System.arraycopy(data, 0, newData, 0, position);
  40.             newData[position] = o;
  41.             System.arraycopy(data, position, newData, position + 1, size - position);
  42.             size = size + 1;
  43.             data = newData;
  44.         }
  45.         else throw new IndexOutOfBoundsException();
  46.     }
  47.     public void resize(int newSize){
  48.         E[] newData = (E[]) new Object[newSize];
  49.         int copySize = size;
  50.         if (newSize < size)
  51.             copySize = newSize;
  52.         for (int i = 0; i < copySize; i++)
  53.             newData[i] = data[i];
  54.         data = newData;
  55.         size = newSize;
  56.     }
  57.    
  58.     */
  59.    
  60.     public void set(int position, E obj) throws IndexOutOfBoundsException {
  61.         if (position >= 0 && position < len) {
  62.             data[position] = obj;
  63.         } else {
  64.             System.out.println("Ne moze da se vmetne element na dadenata pozicija");
  65.             throw new IndexOutOfBoundsException();
  66.         }
  67.     }
  68.  
  69.     public E get(int position) throws IndexOutOfBoundsException {
  70.         if (position >= 0 && position < len) {
  71.             return data[position];
  72.         } else {
  73.             System.out.println("Ne e validna dadenata pozicija");
  74.             throw new IndexOutOfBoundsException();
  75.         }
  76.     }
  77.  
  78.     public int sum(Array<Integer> array) {
  79.         int sum = 0;
  80.         for (int i = 0; i < array.getLength(); i++) {
  81.             //System.out.println("array.get(i): "+array.get(i)+" len: " + array.getLength() + " i: "+i);
  82.             sum += (int) array.get(i);
  83.         }
  84.         return sum;
  85.     }
  86.    
  87.     public double average(Array<Integer> array) {
  88.         return array.sum(array) / array.getLength();
  89.     }
  90.    
  91.     public static int numberToAverage(Array<Integer> array) {
  92.         double average = array.average(array);
  93.         double minDistance = Math.abs(average - array.get(0));
  94.         int index = 0;
  95.         for (int i = 0; i < array.getLength(); i++) {
  96.             //double distance = Math.abs(average) - array.get(i);
  97.             if (minDistance > Math.abs(average - array.get(i))){
  98.                 minDistance = Math.abs(average - array.get(i));
  99.                 index = i;
  100.             }
  101.             else if (minDistance == Math.abs(average - array.get(i))){
  102.                 if (array.get(i) < array.get(index)){
  103.                     minDistance = Math.abs(average - array.get(i));
  104.                     index = i;
  105.                 }
  106.             }
  107.         }
  108.         return array.get(index);
  109.     }
  110.  
  111.     public static void main(String[] args) throws IOException {
  112.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  113.        
  114.         int N = Integer.parseInt(in.readLine());
  115.         Array<Integer> arr = new Array<Integer>(N);
  116.         for (int i = 0; i < N; i++) {
  117.             arr.set(i, Integer.parseInt(in.readLine()));
  118.         }
  119.  
  120.         System.out.println(numberToAverage(arr));
  121.     }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement