Java arrays and methods issues -


i need program following below.

  1. create function called tostring returns printable string values of array in “array literal format”.

  2. create function called rangeinarray returns range of values in array. in math, range magnitude of difference between minimum , maximum (inclusive). have methods min , max use them , not need include loops method.

this code have currently.

public class westandersonasmnt9 {      /**      * @param args      */     public static void main(string[] args) {         int sample_array_1[] = {4,7,9,3,2,8,15,6,8,7};         int sample_array_2[] = {12,6,4,8,3,7,11,1,6};         integerarray arr = new integerarray(sample_array_1.length);         integerarray arr2 = new integerarray(sample_array_2.length);         integerarray arr3 = new integerarray(sample_array_1);         integerarray arr4 = new integerarray(sample_array_2);         arr.fillrandom(100, 200);         arr.printliteral();         arr2.fillrandom(100, 200);         arr2.printliteral();         arr.copy(sample_array_1);         arr.printliteral();         arr.copy(sample_array_2);         arr.printliteral();         arr3.sumofarray();         arr4.sumofarray();         arr3.maxinarray();         arr4.maxinarray();         arr3.mininarray();         arr4.mininarray();         arr3.rangeinarray();       }  }   public class integerarray {     int arr[];      public integerarray(int size) {         arr = new int[size];     }      public integerarray(int value[]) {         arr = value;     }      public void fillrandom(int from, int to) {         int range = (to - from) + 1;         (int = 0; < arr.length; i++) {             arr[i] = (int) (math.random() * range) + from;         }     }      public void printliteral() {         if (arr == null) {             system.out.println("no array");         } else if (arr.length <= 0) {             system.out.println("{}");         } else {             system.out.print("{" + arr[0]);             (int = 1; < arr.length; i++) {                 system.out.print("," + arr[i]);             }             system.out.println("}");         }     }      public void copy(int copy[]) {         arr = copy;     }      public void sumofarray() {         int sum = 0;         (int counter = 0; counter < arr.length; counter++) {             sum += arr[counter];         }         system.out.println("the sum " + sum);      }      public int maxinarray() {         int largest = arr[0];         (int = 1; < arr.length; i++)              if (arr[i] > largest) {                 largest = arr[i];         }         system.out.println("the max " + largest);         return largest     }     public int mininarray() {         int smallest = arr[0];         (int = 1; < arr.length; i++)             if (arr[i] < smallest) {                 smallest = arr[i];             }         system.out.println("the mmin " + smallest);         return smallest     }     public int rangeinarray() {     int range = maxinarray() - mininarray() + 1;  // + 1 "inclusive"     system.out.println("the range " + range);     return range;     }  } 

change maxinarray() , mininarray() return integer.

public int maxinarray() {     int largest = arr[0];     (int = 1; < arr.length; i++)          if (arr[i] > largest) {             largest = arr[i];     }     system.out.println("the max " + largest);     return largest; }  public int mininarray() {     int smallest = arr[0];     (int = 1; < arr.length; i++)         if (arr[i] < smallest) {             smallest = arr[i];         }     system.out.println("the mmin " + smallest);     return smallest; }  public int rangeinarray() {     int range = maxinarray() - mininarray() + 1;  // + 1 "inclusive"     system.out.println("the range " + range);     return range; } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -