How to create the java program: - Create a Java program in both Windows and Ubun
ID: 3872139 • Letter: H
Question
How to create the java program: - Create a Java program in both Windows and Ubuntu using any of their Editors *** DO NOT USE IDE'S SUCH AS ECLIPSE, NETBEANS, ETC *** - Windows : In the Command Line (cmd), use the notepad or notepad++ editor - Ubuntu : In the Terminal Mode use the gedit, or nano or pico or vi or vim editor - Compile and run this program using the MSDOS/Terminal ex: to compile : javac ArrayMath.java to run : java ArrayMath Purpose of this program: - Create a 10 x 10 two dimensional array of int data type (Remember that arrays start with 0, not 1. - Load each index in the array with the multiplication of each (row * column) location - Using printf display the content of each index, ALIGNED. - Create an algorithm using a loop that will add all the values in all odd columns, and print their total aligned below the column. - Print the addition of all values in each index in the array. This is a sum of all the elements of the array. - Print your name and Panther ID below all other output. How: - In the main method declare the two dimension array. - From the main method, passing the two dimensions array, call a method named arrays - In the arrays method do the rest of the program Submit a zip file named FirstnameLastnameA1.zip containing the following into the Blackboard dropbox for Assignment 1. 1. a screenshot of your program running on the Windows system 2. a screenshot of your program running on the Ubuntu system. 3. The ArrayMath.java file. 4. Do not include any other files or folders in your submission.
Explanation / Answer
ArrayMath.java
public class ArrayMath {
public static void main(String[] args) {
//Creating an two Dimensional Array
int arr[][]=new int[10][10];
//calling the method by passing the array as argument
arrays(arr);
}
private static void arrays(int[][] arr) {
//Declaring variables
int sumOfOddCols=0;
int sumofAllElements=0;
//Populating the elements into the 2-D array
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[0].length;j++)
{
arr[i][j]=i*j;
}
}
//Displaying the elements in the 2-D array
System.out.println(" :: Displaying the 2-D array ::");
for(int i=0;i<arr.length;i++)
{
System.out.print(" ");
for(int j=0;j<arr[0].length;j++)
{
System.out.printf("%d ",arr[i][j]);
}
System.out.println();
}
int cnt=1;
//calculating the odd column elements the 2-D array
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[0].length;j++)
{
//Performing sum of odd column elements
if(cnt%2!=0)
{
sumOfOddCols+=arr[j][i];
}
//calculating the sum of all elements in the array
sumofAllElements+=arr[i][j];
}
cnt++;
}
System.out.println("Sum of Odd Columns :"+sumOfOddCols);
//Displaying the sum of all elements in the array
System.out.println(" Sum of All Elements :"+sumofAllElements);
}
}
_______________________
Output:
:: Displaying the 2-D array ::
0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
0 3 6 9 12 15 18 21 24 27
0 4 8 12 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81
Sum of Odd Columns :900
Sum of All Elements :2025
__________________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.