Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this exercise, you will start by considering a specific programming scenario

ID: 3741062 • Letter: F

Question

For this exercise, you will start by considering a specific programming scenario. You should plan out how you would solve the given problem. Once you have an understanding of the programming scenario, you will consider the generic problem and hints.

Programming Scenario

Proffesor Doolittle using an array to store the exam scores for students in your section of class A student’s ID is the index of the array, such that the first student is index/student ID 0, the second student is index/student ID 1, and so on. Please design a program to help Dr. Doolittle complete the following tasks:

Find the student with the largest ID who received a given score, such as 85.

Find the highest score and the lowest score of the exam. Remember that you do not want to modify the array since the indexes relate to student IDs.

Class uses a ten point scale for A/B/C/D grades . Count the number of students who received a given grade, such as B.

Generic Problem and Hints

The following methods should be placed in IntArrayAlgorithms.java.

Task A - lastIndexOf

Generic Problem: Write a method called lastIndexOf that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. The method should return -1 if the value is not found. For example, in the array [74, 85, 102, 99, 101, 85, 56], the last index of the value 85 is 5.

Output: Return an int which you should print: The last index of the value 85 in {74, 85, 102, 99, 101, 85, 56} is 5.

Hints: Describe an algorithm for find the last index at which a given value is found in an array. Consider traversing the array from each end, which approach would be the most efficient?

Task B - range

Generic Problem: Write a method called range that returns the range of values in an array of integers. The range is defined as 1 more than the difference between the maximum and minimum values in the array. For example, if an array called list contains the values [36, 12, 25, 19, 46, 31, 22], the call of range(list) should return 35 (46 - 12 + 1). You may assume that the array has at least one element.

Output: return an int which you should print: The range of {36, 12, 25, 19, 46, 31, 22} is 35.

Hints: Describe an algorithm for finding the maximum and minimum values in an array. As you consider different algorithms, make sure that you do not modify the array (or change the order of elements).

Task C - countInRange

Generic Problem: Write a method called countInRange that accepts an array of integers, a minimum value, and a maximum value as parameters and returns the count of how many elements from the array fall between the minimum and maximum (inclusive). For example, in the array [14, 1, 22, 17, 36, 7, –43, 5], for minimum value 4 and maximum value 17, there are four elements whose values fall between 4 and 17. You may assume that the minimum is less than or equal to the maximum.

Output: Return an int which you should print: In the array {14, 1, 22, 17, 36, 7, -43, 5}, there are 4 elements whose values fall between 4 and 17.

Hints: Describe an algorithm that traverses an array and counts the number of elements in a certain range (inclusive of minimum and maximum value).

IntArrayAlgorithms.java Code:

import java.util.Random;
import java.util.Scanner;

/**
* A program that includes several algorithms for arrays of integers.
*
* @author Dr. Doolittle
*/
public class IntArrayAlgorithms {

/**
* Starts the program and calls the other methods
*
* @param args command line arguments
*/
public static void main(String[] args) {
int[] fromUser = getArrayFromUser(5);
System.out.println("From User: " + arrayAsString(fromUser));

int[] random = getRandomArray(10, 20);
System.out.println("Random: " + arrayAsString(random));
}

/**
* Creates an array with given length and stores the values that are given
* from the user.
*
* @param length The length of the array that will be created and returned
* @return Array with length elements that were given by the user
*/
public static int[] getArrayFromUser(int length) {
Scanner console = new Scanner(System.in);
int[] ret = new int[length];
for(int i = 0; i < length; i++){
System.out.print("Enter Integer: ");
while(!console.hasNextInt()){
System.out.print("Not an int. Enter value: ");
console.next();
}
ret[i] = console.nextInt();
}
return ret;
}

/**
* Creates an array with given length and stores the random values between 0
* and (bound-1)
*
* @param length The length of the array that will be created and returned
* @param bound The upperbound for the range of values within the array
* @return Array with length elements that were assigned random integers
*/
public static int[] getRandomArray(int length, int bound) {
int[] ret = new int[length];
Random r = new Random();
for(int i = 0; i < ret.length; i++){
ret[i] = r.nextInt(bound);
}
return ret;
}

/**
* Returns the contents of the array as a String in the format
* {val,val,...val}
*
* @param arr Array to return as a String
* @return String format ({val,val,...val}) of arr
*/
public static String arrayAsString(int[] arr) {
String ret = "{";
for(int i = 0; i < arr.length; i++){
ret += arr[i];
if(i != arr.length - 1){
ret += ", ";
}
}
ret += "}";
return ret;
}
}

Explanation / Answer

ScreenShot

-----------------------------------------------------------------------------------------------------------------------------

Program

/**Packages**/

import java.util.Random;
import java.util.Scanner;

/**
* A program that includes several algorithms for arrays of integers.
*
* @author Dr. Doolittle
*/
class IntArrayAlgorithms {

   /**
    * Starts the program and calls the other methods
    *
    * @param args command line arguments
    */
   public static void main(String[] args) {
/**User inputed array check
Generate user inputed array and call three function calls **/
      int[] fromUser = getArrayFromUser(7);
      //Variables to test our function
      int val=85,init=57,end=100;         
      //System.out.println("From User: " + arrayAsString(fromUser));
      //call lastIndex of the element function and display
      int index=lastIndexOf(fromUser,val);
       System .out.println("Last index of the value "+val+" in "+arrayAsString(fromUser)+" is "+index);
       //call range of the array function and display
       int range=rangeOf(fromUser);
       System .out.println("The range of "+arrayAsString(fromUser)+" is "+range);
       //call count of numbers with in range function and display
       int count=countInRange(fromUser,init,end);
       System .out.println("In the array "+arrayAsString(fromUser)+" , there are "+count+" elements whose values fall between "+init+" and "+end);
     
        /**random array check   and call three function calls **/
      int[] random = getRandomArray(10, 20);
      //System.out.println("Random: " + arrayAsString(random));
      int index1=lastIndexOf(random,val);
       System .out.println("Last index of the value "+val+" in "+arrayAsString(random)+" is "+index1);
       int range1=rangeOf(random);
       System .out.println("The range of "+arrayAsString(random)+" is "+range1);
       int count1=countInRange(random,init,end);
       System .out.println("In the array "+arrayAsString(random)+" , there are "+count1+" elements whose values fall between "+init+" and "+end);
    
   }

   /**
    * Creates an array with given length and stores the values that are given
    * from the user.
    *
    * @param length The length of the array that will be created and returned
    * @return Array with length elements that were given by the user
    */
   public static int[] getArrayFromUser(int length) {
      Scanner console = new Scanner(System.in);
      int[] ret = new int[length];
    
      for(int i = 0; i < length; i++){
         System.out.print("Enter Integer: ");
         while(!console.hasNextInt()){
            System.out.print("Not an int. Enter value: ");
            console.next();
         }
         ret[i] = console.nextInt();
      }
      return ret;
   }

   /**
    * Creates an array with given length and stores the random values between 0
    * and (bound-1)
    *
    * @param length The length of the array that will be created and returned
    * @param bound The upperbound for the range of values within the array
    * @return Array with length elements that were assigned random integers
    */
   public static int[] getRandomArray(int length, int bound) {
      int[] ret = new int[length];
      Random r = new Random();
      for(int i = 0; i < ret.length; i++){
         ret[i] = r.nextInt(bound);
      }
      return ret;
   }

   /**
    * Returns the contents of the array as a String in the format
    * {val,val,...val}
    *
    * @param arr Array to return as a String
    * @return String format ({val,val,...val}) of arr
    */
   public static String arrayAsString(int[] arr) {
      String ret = "{";
      for(int i = 0; i < arr.length; i++){
         ret += arr[i];
         if(i != arr.length - 1){
            ret += ", ";
         }
      }
      ret += "}";
      return ret;
   }
   /** last index calculation, take arrays length and search backward to find occurance**/
   public static int lastIndexOf(int[] arr,int elem){
    for(int i=arr.length-1;i>-1;i--){
        if(arr[i]==elem){
            return i;
        }
    }
    return -1;
   }
   /** to calculate range find out minimum amd maximum of the array and ther difference add with 1**/
   public static int rangeOf(int[] arr){
    int min = arr[0]; // assume first elements as smallest number
   int max = arr[0]; // assume first elements as largest number

   for (int i = 1; i < arr.length; i++) // iterate for loop from arrays 1st index (second element)
       {
           if (arr[i] > max)
           {
               max = arr[i];
           }
           if (arr[i] < min)
           {
               min = arr[i];
           }
       }
       int rg=max-min+1;
       return rg;
   }
   /**to find out count by putting condition within range and loop through the array**/
   public static int countInRange(int[] arr,int in,int e){
    int cnt=0;
    for(int i=0;i<arr.length;i++){
         if(arr[i]>=in && arr[i]<=e){
            cnt++;
         }  
    }
    return cnt;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote