Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.hdm_stuttgart.mi.sd1.store;
- /**
- * A container holding a fixed
- * number of integer values.
- *
- */
- public class IntegerStore {
- final int defaulc = 4;
- int[] values ; // Array containing our values
- int numValues = 0; // Number of values present in the container so far.
- public IntegerStore(){
- values = new int[defaulc];
- }
- /**
- * Create a new integer store being able to
- * hold a fixed number of values.
- *
- * @param capacity The number of values this store may contain
- */
- public IntegerStore(int capacity) {
- values = new int[capacity]; // TODO
- }
- /**
- * @return The number of elements the store may
- * hold, see {@link #BoundedIntegerStore(int)}.
- */
- public int getCapacity() {
- return values.length; // TODO
- }
- /**
- * @return The number of values being contained.
- */
- public int getNumValues() {
- return numValues; // TODO
- }
- /**
- * Insert a new value into our container
- *
- * @param value The value to be inserted. This will increment
- * {@link #getNumValues()} by one.
- *
- */
- public void addValue(int value) {
- int t = 0;
- if(numValues>=values.length){
- int tmp[] = values;
- values = new int[tmp.length*2];
- for (int i = 0; i < tmp.length; i++) {
- values[i] = tmp[i];
- }
- }
- if(values.length!=0)
- for (int j = 0; j < values.length; j++) {
- if(value<values[j]){
- t=values[j];
- values[j] = value;
- addValue(t);
- }else{
- values[numValues++] = value;
- }
- }else{
- values[numValues++] = value;
- }
- // TODO;
- }
- /**
- * Access the value at a given index
- *
- * @param index The desired value's index
- * @return The desired value. Precondition: index < {@link #getNumValues()}}
- *
- */
- public int getValue(int index) {
- return values[index]; // TODO
- }
- /**
- * @return The array of values entered so far
- */
- public int[] getValues() {
- int[] a = new int[numValues];
- for (int i = 0; i < a.length; i++) {
- a[i] = values[i];
- }
- return a;
- }
- public double getAverage(){
- double average = 0;
- for (int i = 0; i < values.length; i++) {
- average += values[i];
- }
- return average/numValues;
- }
- public void clear(){
- values = new int[defaulc];
- numValues = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement