Java I

Jonah Warren
jonah@parsons.edu
http://a.parsons.edu/~java2004

Lab 4.3 Answer


4.3a: SMALLEST OF 5 no array
void setup() {
  println(smallestInt(3,5,8,-3,0));
}

int smallestInt(int a, int b, int c, int d, int e) {

  int smallest = a;

  if (b < smallest) {
    smallest = b;
  }
  else if (c < smallest) {
    smallest = c;
  }
  else if (d < smallest) {
    smallest = d;
  }
  else if (e < smallest) {
    smallest = e;
  }

  return smallest;

}

4.3b: SMALLEST OF 5 with array
void setup() {
  println(smallestInt(3,5,8,-3,0));
}

int smallestInt(int a, int b, int c, int d, int e) {
  int[] numList = new int[5];

  numList[0] = a;
  numList[1] = b;
  numList[2] = c;
  numList[3] = d;
  numList[4] = e;

  int smallest =   numList[0];

  for(int i=1;i<5;i++) {
    if (numList[i] < smallest) {
      smallest = numList[i];
    }
  }

  return smallest;

}

SMALLEST OF 50!!!
int[] numList = new int[50];

void setup() {
  for(int i=0;i<50;i++) {
    numList[i]=(int)random(-500,500);
  }
  for(int i=0;i<50;i++) {
    print(numList[i]+" ");
  }
  println();
  println("The smallest is:" + smallestInt(numList));
}

int smallestInt(int[] list) {

  int smallest = list[0];

  for(int i=1;i<50;i++) {
    if (list[i] < smallest) {
      smallest = list[i];
    }
  }

  return smallest;

}