The following variables/constants should be declared in your program: Step 6: Th
ID: 3558598 • Letter: T
Question
The following variables/constants should be declared in your program:
Step 6: The Logic in Your Program (Outside of the Main Method)
Note: All of the following methods should be declared outside the main method in order for your program to work correctly. The first method is shown below:
public static void printArray(int[] list)
{
int i;
System.out.println(" The array has these numbers:");
for (i = 0;i < list.length; i++)
{
System.out.println(list[i]);
}
} // end method printArray
In all of your methods, you should use the list.length when applicable, as it contains valuable information.
There are four more methods, for which the method headers are shown below:
public static int getTotalSum(int[] list) - this method will sum up all of the element's values in the array list and then return this value
public static int getTotalEvenNumbers(int[] list) - this method will only count how many even numbers there are in the array list and then return this value
public static int getTotalOddNumbers(int[] list) - this method will only count how many odd numbers there are in the array list and then return this value
public static void updateEvenNumbers(int[] list) - this method will only update those numbers that are even in the array list; so for example if an element at index 5 is an even number, multiply that number by 3 to update it.
Step 6: The Logic in Your Program (Outside of the Main Method)
Explanation / Answer
package com.chegg;
public class DoArrayOper
{
static int list[]={1,2,3,4,5,6,7,8,9};
public static void main(String[] args)
{
printArray(list);
int totalSum=getTotalSum(list);
System.out.println("The sum of these numbers is "+totalSum);
int totalEvenSum =getTotalEvenNumbers (list);
System.out.println("There are "+totalEvenSum+" even numbers");
int totalOddSum =getTotalOddNumbers (list);
System.out.println("There are "+totalOddSum+" odd numbers");
updateEvenNumbers(list);
printArray(list);
totalSum=getTotalSum(list);
System.out.println("The sum of these numbers is "+totalSum);
System.out.println("End Of Program");
System.out.println("This program was written by raghunath");
}
public static void updateEvenNumbers(int[] list)
{
for (int i = 0;i < list.length; i++)
{
if(list[i]%2==0)
DoArrayOper.list[i]=list[i]*3;
}
}
public static int getTotalOddNumbers(int[] list)
{
int count=0;
for (int i = 0;i < list.length; i++)
{
if(list[i]%2!=0)
count++;
}
return count;
}
public static int getTotalEvenNumbers(int[] list)
{
int count=0;
for (int i = 0;i < list.length; i++)
{
if(list[i]%2==0)
count++;
}
return count;
}
public static int getTotalSum(int[] list)
{
int temp=0;
for (int i = 0;i < list.length; i++)
{
temp=temp+list[i];
}
return temp;
}
public static void printArray(int[] list)
{
int i;
System.out.println(" The array has these numbers:");
for (i = 0;i < list.length; i++)
{
System.out.println(list[i]);
}
} // end method printArray
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.