Question

In: Computer Science

JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns...

JAVA LANGUAGE

Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns to store integer values, and then:
fill elements with values as the sum of its column index and row index, e.g., the element at row index 0 and column index 0 is (0+0=0), the element at row index 0 and column index 1 is (0+1=1).
compute the sum of elements at the second row.
compute the sum of elements at the third column.
compute the sum of all elements.
print the array in tabular form.


Write a program that declares an array list to store integers and fills the elements with users’ inputs from keyboards. Then prints the following outputs (each in one line):
Every element at an even index.
Every even element (using enhanced for loop).
Every odd element (using common loop, not enhanced for loop).
All elements in reverse order.
Only the first, the middle and the last element.
The largest and smallest element.
The alternating sum of all elements. For example, if your program reads the values from users: 1 4 9 16 10 7 4 9 11 5, then it computes 1-4+9-16+10-7+4-9+11-5=-6.

Solutions

Expert Solution

Please find below code for above 2 questions and don't forget to give a like.

1)


package com.example;

public class Main{
  
public static void main(String[] args) {
int array[][];
int result=0;
array= new int[4][4];
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
array[i][j]=i+j;
result+=array[i][j];
}
  
}
  
System.out.println("The 2nd row values are");
  
for(int j=0;j<array.length;j++){
int i=1;
System.out.print(array[i][j]+" ");
}

System.out.println();
System.out.println("The third column values are");
  
for(int i=0;i<array.length;i++){
int j=2;
System.out.print(array[i][j]+" ");
System.out.println();
}

System.out.print("Sum of all the elements is"+result);
System.out.println();
System.out.println("The tabular form is");
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
}

2)

We use scanner to take the input and asking user to enter size of list and then using that length we will get the input from user.

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
       int result=0;
Scanner input1 = new Scanner(System.in);
System.out.print("Enter the size of list");
int number=input1.nextInt();
int numb[]=new int[number];
System.out.println("Enter the the elements");
for(int i=0;i<number;i++){
numb[i]=input1.nextInt();
}
  
for (int i=0;i<numb.length;i++){
if(i%2==0){
result+=numb[i];
System.out.print(result);
}
else{
result-=numb[i];
}
}
System.out.print("The result is"+result);
   }
}


Related Solutions

Question 2. Write a complete C++ program that uses a 2-dimensional array with 4 rows and...
Question 2. Write a complete C++ program that uses a 2-dimensional array with 4 rows and 30 columns. Row represents sections of a course and column represents the students, value inside each position of the array is the final exam grade for each students. Fill the array with random numbers between 40 and 100. Calculate the total, average, maximum, minimum for each section. Please do it simple. code
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4...
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4 grocery stores. Each store has 3 departments where product presentation affects sales (produce, meat, frozen). Every so often a department in a store gets a bonus for doing a really good job. You need to create a program that keeps a table of bonuses in the system for departments. Create a program that has a two dimensional array for these bonuses. The stores can...
Part 1:Write a program in Java that declares an array of 5 elements and displays the...
Part 1:Write a program in Java that declares an array of 5 elements and displays the contents of the array. Your program should attempt to access the 6th element in the array (which does not exist) and using try. catch your program should prevent the run-time error and display your error message to the user. The sample output including the error message is provided below. Part (1) Printing an element out of bounds 5 7 11 3 0 You went...
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements b) Inputs an integer n from 1-30 from the keyboard. If n < 1 set n = 1. If n > 30 set n = 30. the program should keep asking the user the input n one by one, followed by printing of the value of n (n=n if bigger than 1 and smaller than 30, 1 if smaller than 1 and 30 if...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT