Part A: assume the array is full and the data is int; write methods that receive
ID: 2246418 • Letter: P
Question
Part A: assume the array is full and the data is int; write methods that receive (as a parameter) an array and …
returns the sum of the elements
swaps the first and last element
returns whether the array is ordered ascending
increments each element by one
also receives a lower and upper limit, and returns how many elements are in that range, inclusive
also receives an int value and returns how many times it appears in the array
counts and prints how many odd numbers are in the array
changes all positive values to zero
Explanation / Answer
Explanation::
Code in java::
import java.util.*;
public class ArrayMethods{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int choice=-1;
/*Array named arr[] is declared and initialized here*/
int arr[]={-1,-2,4,5,0,23,11,3,1,-1,3,23,56,100,12,2,90};
/*Current values of array is printed here before any application of methods.*/
System.out.println("Current status of array elements...");
printArray(arr);
do{
/*This loop will run recursively until 0 is entered*/
System.out.println(" Select any one option from below::");
System.out.println("1. Get Sum of Array Elements.");
System.out.println("2. Swap First and Last Elements.");
System.out.println("3. Check if Array is in Ascending order.");
System.out.println("4. Increment value of each element by 1.");
System.out.println("5. Get count of elements that are in Range.");
System.out.println("6. Get count of a value in array.");
System.out.println("7. Get count of odd numbers in array.");
System.out.println("8. Change all positive value to zero.");
System.out.println("0. To exit the program.");
System.out.println(" Enter your choice here::");
/*Scanning choice */
choice=sc.nextInt();
/*switch case selects respective method.*/
switch(choice){
case 1:
/*Prints Sum of array elements*/
int sum=arraySum(arr);
System.out.println("Total sum of array elements is "+sum);
break;
case 2:
/*Prints array before swapping*/
System.out.println("Array before swapping first and last::");
printArray(arr);
/*swapFirstLast function swaps the array elements*/
swapFirstLast(arr);
/*Prints array after swapping*/
System.out.println("Array after swapping first and last::");
printArray(arr);
break;
case 3:
/*Prints if array elements are in ascending order*/
if(ascendingArray(arr)){
System.out.println("Yes! Array is in ascending order.");
}else{
System.out.println("No! Array is not in ascending order");
}
break;
case 4:
/*Increments each element by one */
System.out.println("Array before increment ::");
printArray(arr);
incrementEachElement(arr);
System.out.println("Array after increment::");
printArray(arr);
break;
case 5:
/*Function to get count of elements in given range*/
int lower,upper;
while(true){
System.out.println("Enter lower and upper limit values::");
lower=sc.nextInt();
upper=sc.nextInt();
if(lower>upper){
/*If invalid range is entered*/
System.out.println("Enter valid range..");
}else{
int count=range(arr,lower,upper);
System.out.println("Total element in range "+lower+"<->"+upper+" is "+count);
break;
}
}
break;
case 6:
/*TAKES a value as input and see its count in array*/
System.out.println("Enter value to see its count::");
int value=sc.nextInt();
int count=howManyTimes(arr,value);
System.out.println("Value "+value+" appears "+count+" times.");
break;
case 7:
/*Count how many odd elemenst are present in array*/
int oddCount=oddNumberCount(arr);
System.out.println("Odd numbers in array are "+oddCount);
break;
case 8:
/*Here all postive values of updated to 0*/
System.out.println("Array before update ::");
printArray(arr);
updatePositiveToZero(arr);
System.out.println("Array after update::");
printArray(arr);
break;
default:
if(choice!=0){
System.out.println("Enter valid choice.");
}
break;
}
}while(choice!=0);
}
public static int arraySum(int arr[]){
/*Returns sum of array elements*/
int sum=0;
for(int i=0;i<arr.length;i++){
sum+=arr[i];
}
return sum;
}
public static void swapFirstLast(int arr[]){
/*Variable first stores first element*/
/*Variable last stores last element which is at index arr.length-1*/
int first=arr[0];
int last=arr[arr.length-1];
/*Swapping*/
arr[0]=last;
arr[arr.length-1]=first;
}
public static boolean ascendingArray(int arr[]){
/*Here loop starts from index 1 and see if previous value if greater or less*/
for(int i=1;i<arr.length;i++){
if(arr[i]<arr[i-1]){
/*If Current value is less then previous value then returns false */
return false;
}
}
return true;
}
public static void incrementEachElement(int arr[]){
for(int i=0;i<arr.length;i++){
/*Increment each value of array by 1*/
arr[i]+=1;
}
}
public static int range(int arr[],int lower,int upper){
int count=0;
for(int i=0;i<arr.length;i++){
if(arr[i]>=lower && arr[i]<=upper){
/*If current value lies in range lower <= arr[i] <= upper*/
count++;
}
}
return count;
}
public static int howManyTimes(int arr[],int value){
int count=0;
for(int i=0;i<arr.length;i++){
/*If current array element is equal to passed value then count is incremented*/
if(arr[i]==value){
count++;
}
}
return count;
}
public static int oddNumberCount(int arr[]){
int oddCount=0;
for(int i=0;i<arr.length;i++){
/*If current array element is odd then count is incremented*/
if(arr[i]%2!=0){
oddCount++;
}
}
return oddCount;
}
public static void updatePositiveToZero(int arr[]){
for(int i=0;i<arr.length;i++){
/*If current array element is greater then 0, then current value is updated to 0*/
if(arr[i]>0){
arr[i]=0;
}
}
}
/*
* This method below is extra method just to
* print the array to show the effects of above all methods.
*/
public static void printArray(int arr[]){
int n=arr.length;
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
Output::
C:Algo>javac ArrayMethods.java
C:Algo>java ArrayMethods
Current status of array elements...
-1 -2 4 5 0 23 11 3 1 -1 3 23 56 100 12 2 90
Select any one option from below::
1. Get Sum of Array Elements.
2. Swap First and Last Elements.
3. Check if Array is in Ascending order.
4. Increment value of each element by 1.
5. Get count of elements that are in Range.
6. Get count of a value in array.
7. Get count of odd numbers in array.
8. Change all positive value to zero.
0. To exit the program.
Enter your choice here::
1
Total sum of array elements is 329
Select any one option from below::
1. Get Sum of Array Elements.
2. Swap First and Last Elements.
3. Check if Array is in Ascending order.
4. Increment value of each element by 1.
5. Get count of elements that are in Range.
6. Get count of a value in array.
7. Get count of odd numbers in array.
8. Change all positive value to zero.
0. To exit the program.
Enter your choice here::
3
No! Array is not in ascending order
Select any one option from below::
1. Get Sum of Array Elements.
2. Swap First and Last Elements.
3. Check if Array is in Ascending order.
4. Increment value of each element by 1.
5. Get count of elements that are in Range.
6. Get count of a value in array.
7. Get count of odd numbers in array.
8. Change all positive value to zero.
0. To exit the program.
Enter your choice here::
2
Array before swapping first and last::
-1 -2 4 5 0 23 11 3 1 -1 3 23 56 100 12 2 90
Array after swapping first and last::
90 -2 4 5 0 23 11 3 1 -1 3 23 56 100 12 2 -1
Select any one option from below::
1. Get Sum of Array Elements.
2. Swap First and Last Elements.
3. Check if Array is in Ascending order.
4. Increment value of each element by 1.
5. Get count of elements that are in Range.
6. Get count of a value in array.
7. Get count of odd numbers in array.
8. Change all positive value to zero.
0. To exit the program.
Enter your choice here::
4
Array before increment ::
90 -2 4 5 0 23 11 3 1 -1 3 23 56 100 12 2 -1
Array after increment::
91 -1 5 6 1 24 12 4 2 0 4 24 57 101 13 3 0
Select any one option from below::
1. Get Sum of Array Elements.
2. Swap First and Last Elements.
3. Check if Array is in Ascending order.
4. Increment value of each element by 1.
5. Get count of elements that are in Range.
6. Get count of a value in array.
7. Get count of odd numbers in array.
8. Change all positive value to zero.
0. To exit the program.
Enter your choice here::
5
Enter lower and upper limit values::
5
9
Total element in range 5<->9 is 2
Select any one option from below::
1. Get Sum of Array Elements.
2. Swap First and Last Elements.
3. Check if Array is in Ascending order.
4. Increment value of each element by 1.
5. Get count of elements that are in Range.
6. Get count of a value in array.
7. Get count of odd numbers in array.
8. Change all positive value to zero.
0. To exit the program.
Enter your choice here::
7
Odd numbers in array are 8
Select any one option from below::
1. Get Sum of Array Elements.
2. Swap First and Last Elements.
3. Check if Array is in Ascending order.
4. Increment value of each element by 1.
5. Get count of elements that are in Range.
6. Get count of a value in array.
7. Get count of odd numbers in array.
8. Change all positive value to zero.
0. To exit the program.
Enter your choice here::
6
Enter value to see its count::
13
Value 13 appears 1 times.
Select any one option from below::
1. Get Sum of Array Elements.
2. Swap First and Last Elements.
3. Check if Array is in Ascending order.
4. Increment value of each element by 1.
5. Get count of elements that are in Range.
6. Get count of a value in array.
7. Get count of odd numbers in array.
8. Change all positive value to zero.
0. To exit the program.
Enter your choice here::
8
Array before update ::
91 -1 5 6 1 24 12 4 2 0 4 24 57 101 13 3 0
Array after update::
0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Select any one option from below::
1. Get Sum of Array Elements.
2. Swap First and Last Elements.
3. Check if Array is in Ascending order.
4. Increment value of each element by 1.
5. Get count of elements that are in Range.
6. Get count of a value in array.
7. Get count of odd numbers in array.
8. Change all positive value to zero.
0. To exit the program.
Enter your choice here::
0
C:Algo>
Thank you!!