Monday, January 12, 2015

Arrays and Loops, Random

Here is the code for the Loops and Arrays

package com.spconger.LoopsandArrays;

import java.util.Random;

public class Program {
 /*This class consists of examples
  * of arrays, ifs and loops
  * steve conger 1/7/2015
  */
 final int SIZE = 50;
 final int MAXRANDOM = 500;

 public static void main(String[] args) {
  Program p = new Program();
  p.createArrays();

 }//end main

 private int getRandom() {
  Random r = new Random();
  int number = r.nextInt(MAXRANDOM);
  return number;
 }//end random

 private void createArrays() {
  int[] smaller = new int[SIZE];
  int[] larger = new int[SIZE];
  populateArrays(smaller,larger);
 }//end createArrays

 private void populateArrays(int[] small, int[] large) {
  int smallCounter = 0, largeCounter = 0;

  for (int i = 0; i < SIZE; i++) {

   int num = getRandom();
            
   //this writes the numbers to
   //the large array if they are
   //greater than 250
   //otherwise it writes them to
   //the smaller array
   if (num > 250) {
    large[largeCounter] = num;
    largeCounter++;
   }//end if
   else {
    small[smallCounter] = num;
    smallCounter++;
   }//end else

  }//end for
  displayArrays(small, large);
 }//end populateArrays
 
 private void displayArrays(int[] small, int[] large){
  System.out.println("these are the values 250 or less");
  for(int i = 0; i<SIZE; i++){
   if(small[i] != 0){
    System.out.println(small[i]);
  }//end if
   
   
  }//end for
  System.out.println("these are values over 250");
  int x=0;
   while(x < large.length){
    if(large[x] != 0){
     System.out.println(large[x]);
     x++;
    }//end if
   }//end while
 }

}//end class

No comments:

Post a Comment