Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. package de.hdm_stuttgart.mi.sd1.store;
  2.  
  3. /**
  4.  * A container holding a fixed
  5.  *  number of integer values.
  6.  *
  7.  */
  8. public class IntegerStore {
  9.  
  10.   final int defaulc = 4;
  11.   int[] values ; // Array containing our values
  12.   int numValues = 0;            // Number of values present in the container so far.
  13.  
  14.  
  15.   public IntegerStore(){
  16.       values = new int[defaulc];
  17.   }
  18.  
  19.   /**
  20.    * Create a new integer store being able to
  21.    * hold a fixed number of values.
  22.    *
  23.    * @param capacity The number of values this store may contain
  24.    */
  25.   public IntegerStore(int capacity) {
  26.     values = new int[capacity]; // TODO
  27.   }
  28.  
  29.   /**
  30.    * @return The number of elements the store may
  31.    * hold, see {@link #BoundedIntegerStore(int)}.
  32.    */
  33.   public int getCapacity() {
  34.     return values.length; // TODO
  35.   }
  36.  
  37.   /**
  38.    * @return The number of values being contained.
  39.    */
  40.   public int getNumValues() {
  41.     return numValues; // TODO
  42.   }
  43.  
  44.   /**
  45.    * Insert a new value into our container
  46.    *
  47.    * @param value The value to be inserted. This will increment
  48.    * {@link #getNumValues()} by one.
  49.    *
  50.    */
  51.   public void addValue(int value) {
  52.       int t = 0;
  53.       if(numValues>=values.length){
  54.           int tmp[] = values;
  55.           values = new int[tmp.length*2];
  56.           for (int i = 0; i < tmp.length; i++) {
  57.             values[i] = tmp[i];
  58.         }
  59.       }
  60.       if(values.length!=0)
  61.           for (int j = 0; j < values.length; j++) {
  62.               if(value<values[j]){
  63.                   t=values[j];
  64.                   values[j] = value;
  65.                   addValue(t);
  66.               }else{
  67.                   values[numValues++] = value;
  68.               }
  69.         }else{
  70.               values[numValues++] = value;
  71.         }    
  72.     // TODO;
  73.   }
  74.  
  75.   /**
  76.    * Access the value at a given index
  77.    *
  78.    * @param index The desired value's index
  79.    * @return The desired value. Precondition: index &lt; {@link #getNumValues()}}
  80.    *
  81.    */
  82.   public int getValue(int index) {
  83.     return values[index]; // TODO
  84.   }
  85.  
  86.   /**
  87.    * @return The array of values entered so far
  88.    */
  89.   public int[] getValues() {
  90.       int[] a = new int[numValues];
  91.       for (int i = 0; i < a.length; i++) {
  92.         a[i] = values[i];
  93.     }
  94.       return a;
  95.   }
  96.  
  97.   public double getAverage(){
  98.       double average = 0;
  99.       for (int i = 0; i < values.length; i++) {
  100.           average += values[i];
  101.       }
  102.       return average/numValues;
  103.   }
  104.  
  105.   public void clear(){
  106.       values = new int[defaulc];
  107.       numValues = 0;
  108.   }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement