In: Computer Science
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):
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.
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.