Question

In: Computer Science

class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...

class Arrays1{

public int getSumOfValues(int[] arr){
// return the sum of values of array elements
int sum = 0;
int i;
for(i = 0; i < arr.length; i++){
sum += arr[1];
}
return sum;
}
public int getAverageValueInArray(int[] arr){
// return the average value of array elements
int value = 0;
for(int i = 0; i < arr.length; i++){
double average = value/ arr.length;
}
return value;
}
public int getNumberOfEvens(int[] arr){
//return the number of even values found in the array
int even = 0;
for(int i = 0; i < arr.length; i++){
if(i % 2 == 0){
}
}
return even;
}
public int getNumberOfOdds(int[] arr){
// return the number of odd vsalues found in the array
int odd = 0;
for(int i = 0; i < arr.length; i++){
if(i % 2 != 0){
}
}
return odd;
}
public int maxValue(int[] arr){
// return max value in array
int maxValue = arr[0];
for(int i = 1; i < arr.length; i++){
if(arr[i] > maxValue){
maxValue = arr[i];
}
}
return maxValue;
}
public int minValue(int[] arr){
// return min value in array
int minValue = arr[0];
for(int i = 1; i < arr.length; i++){
if(arr[i] < minValue){
minValue = arr[i];
}
}
return minValue;
}
public boolean arrayContainsWord(String[] arr, String word){
// return true if array contains specific word, false otherwise
for(int i = 0; i < arr.length; i++){
if(arr[i].equals(word));
return true;
}
return false;
}
public int getIndexOfWord(String[] arr, String word){
// if found, return the index of word, otherwise return -1
for(int i = 0; i < arr.length; i++){
if(arr[i].equals(word)){
return i;
}
}
return -1;
}
public boolean areArrays(int[] arr1, int[] arr2){
// 1. initial check: both arrays need to have the same length, return false if not the same
// 2. return true if both given arrats are equals(same values in the same indices), false otherwise
if(arr1.length != arr2.length){
return false;
}
for(int i = 0; i < arr1.length; i++){
if(arr1[i] != arr2[i]){

}
}
return true;
}
public boolean areArraysEqual(String[] arr1, String[] arr2){
// 1. initial check: both arrays need to have the same length, return false if not the same
// 2. return true if both given arrays are equals(same values in the same indices), false otherwise
if(arr1.length != arr2.length){
return false;
}
for(int i = 0; i < arr1.length; i++){
if(arr1[i].equals(arr2[i])){

}
}
return false;
}
}

Write ArraysDriver (similar design to LoopsDriver):

  • Write an infinite loop which does the following
    • Prints a menu of 11 options (0 to exit)
    • Asks the user which method to call
    • Calls the method and prints out the answer

In this assignment, you will pass arrays as arguments to these methods. You may declare and initialize these arrays before the loop and reuse them throughout your application. For this assignment, use arrays of 5 elements. You will need 2 int[] arrays and 2 String[] arrays to be declared and initialized before the main loop. You may use arbitrary values for each item/element in the arrays.

The only input you will need from the user (other than the menu option selected) is a word to use for arrayContainsWord and getIndexOfWord.

Solutions

Expert Solution

Program: In this program, we have correctly implemented the Arrays1.java class which had a few errors and incomplete codes and also the ArraysDriver class which runs the methods of Arrays1 class by taking the choice of user.

Below is the implementation:

Arrays1.java:

class Arrays1 {

    public int getSumOfValues(int[] arr) {
// return the sum of values of array elements
        int sum = 0;
        int i;

        for (i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public int getAverageValueInArray(int[] arr) {
// return the average value of array elements
        int value = 0;
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        value = sum/arr.length;
        return value;
    }

    public int getNumberOfEvens(int[] arr) {
//return the number of even values found in the array
        int even = 0;
        for (int i = 0; i < arr.length; i++) {
            if (i % 2 == 0) {
                even++;
            }
        }
        return even;
    }

    public int getNumberOfOdds(int[] arr) {
// return the number of odd vsalues found in the array
        int odd = 0;
        for (int i = 0; i < arr.length; i++) {
            if (i % 2 != 0) {
                odd++;
            }
        }
        return odd;
    }

    public int maxValue(int[] arr) {
// return max value in array
        int maxValue = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > maxValue) {
                maxValue = arr[i];
            }
        }
        return maxValue;
    }

    public int minValue(int[] arr) {
// return min value in array
        int minValue = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < minValue) {
                minValue = arr[i];
            }
        }
        return minValue;
    }

    public boolean arrayContainsWord(String[] arr, String word) {
// return true if array contains specific word, false otherwise
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals(word)) ;
            return true;
        }
        return false;
    }

    public int getIndexOfWord(String[] arr, String word) {
// if found, return the index of word, otherwise return -1
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals(word)) {
                return i;
            }
        }
        return -1;
    }

    public boolean areArrays(int[] arr1, int[] arr2) {
// 1. initial check: both arrays need to have the same length, return false if not the same
// 2. return true if both given arrays are equals(same values in the same indices), false otherwise
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    public boolean areArraysEqual(String[] arr1, String[] arr2) {
// 1. initial check: both arrays need to have the same length, return false if not the same
// 2. return true if both given arrays are equals(same values in the same indices), false otherwise
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (!arr1[i].equals(arr2[i])) {
                return false;
            }
        }
        return true;
    }
}

ArraysDriver:

import java.util.Scanner;

public class ArraysDriver {
    public static void main(String[] args) {
        // Create a scanner class object to take user input
        Scanner scanner = new Scanner(System.in);

        // Create 4 arrays 2 ints and 2 string type
        int[] arr1 = new int[5];
        arr1[0] = 2;
        arr1[1] = 6;
        arr1[2] = 5;
        arr1[3] = 4;
        arr1[4] = 7;
        int[] arr2 = new int[5];
        arr2[0] = 7;
        arr2[1] = 6;
        arr2[2] = 5;
        arr2[3] = 9;
        arr2[4] = 4;
        String[] arr3 = new String[5];
        arr3[0] = "apple";
        arr3[1] = "banana";
        arr3[2] = "mango";
        arr3[3] = "grapes";
        arr3[4] = "guava";
        String[] arr4 = new String[5];
        arr4[0] = "dog";
        arr4[1] = "cat";
        arr4[2] = "rat";
        arr4[3] = "lion";
        arr4[4] = "wolf";

        // Create a Arrays1 class object
        Arrays1 obj = new Arrays1();

        // Create an infinite loop
        while(true){

            // Display menu
            System.out.println("Please choose any of the following options:\n" + "0) Quit\n" +
                    "1) getSumOfValues\n2) getAverageValueInArray\n3) getNumberOfEvens\n" +
                    "4) getNumberOfOdds\n5) maxValue\n6) minValue\n7) arrayContainsWord\n" +
                    "8) getIndexOfWord\n9) areArrays\n10) areArraysEqual\n");

            // Take user input
            int userOption = scanner.nextInt();

            // Flag variable to check exit option
            int flag = 0;

            // Switch case to implement methods based on user input
            switch (userOption){
                case 0:
                    flag = 1;
                    break;
                case 1:
                    System.out.println("The sum of array is: " + obj.getSumOfValues(arr1));
                    break;
                case 2:
                    System.out.println("The average value of array is: "+obj.getAverageValueInArray(arr1));
                    break;
                case 3:
                    System.out.println("The number of even values are: " + obj.getNumberOfEvens(arr1));
                    break;
                case 4:
                    System.out.println("The number of odd values are: " + obj.getNumberOfOdds(arr1));
                    break;
                case 5:
                    System.out.println("The maximum value is: " + obj.maxValue(arr1));
                    break;
                case 6:
                    System.out.println("The minimum value is: " + obj.minValue(arr1));
                    break;
                case 7:
                    System.out.print("Please enter a word: ");
                    String word = scanner.next();
                    if(obj.arrayContainsWord(arr3,word)){
                        System.out.println("The array contains the word " + word);
                    }
                    else{
                        System.out.println("the array does not contain the word");
                    }
                    break;
                case 8:
                    System.out.print("Please enter a word: ");
                    word = scanner.next();
                    int index = obj.getIndexOfWord(arr3,word);
                    if(index!=-1){
                        System.out.println("Index of word is: " + index);
                    }
                    else{
                        System.out.println("The word was not found");
                    }
                    break;
                case 9:
                    if(obj.areArrays(arr1,arr2)){
                        System.out.println("Arrays are equal");
                    }
                    else {
                        System.out.println("Not equal");
                    }
                    break;
                case 10:

                    if(obj.areArraysEqual(arr3,arr4)){
                        System.out.println("Arrays are equal");
                    }
                    else {
                        System.out.println("Not equal");
                    }
                    break;
                default:
                    System.out.println("Invalid input!");
                    break;
            }

            // If user chose to quit, break the loop
            if(flag == 1){
                System.out.println("Goodbye!");
                break;
            }
            System.out.println();
        }
    }
}

Output:

#Please ask for any doubts. Thanks.


Related Solutions

Solve this comp architecture problem: Supposed you have an array int* arr = {3,7,6,4,5}. The values...
Solve this comp architecture problem: Supposed you have an array int* arr = {3,7,6,4,5}. The values in arr store in the memory of 0(x21), 8(x21), .... , 32(x21). Transform the following RISC-V code into a C program. ld x5, 16(x21) addi x6, x0, 3 sll x5, x5, x6 sd x5, 8(x21)
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return...
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return -1 } public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the same length, return false if not the same // 2. return true if both given arrats are equals(same values in the same indices), false otherwise } public boolean areArraysEqual(String[] arr1, String[] arr2){ // 1. initial check: both arrays need to have the same length, return false...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: //...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: // // 1. A constructor that takes a reference to an array, // and initializes an instance variable with this // array reference // // 2. An instance method named sum, which will calculate // the sum of the elements in the array. If the array // is empty (contains no elements), then this will // will return 0. You will need a loop for...
A.) myNums is an array of 50 elements of type int and k is an int...
A.) myNums is an array of 50 elements of type int and k is an int variable. For which one we get the index of out of bounds? a. for (k = 0; k <= 49; k++)     cout << myNums[k] << " "; b. for (k = 1; k < 50; k++)     cout << myNums[k] << " "; c. for (k = 0; k <= 50; k++)     cout << myNums[k] << " "; d. for (k =...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[...
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[ ][ ] = { {1, 2, 3, 4},                        {5, 6, 7, 8},                        {9, 10, 11, 12} }; OUTPUT: Sum of the edges = 65
// Given an int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT