Question

In: Computer Science

Write the following program in Java. Ask the user to enter the number of rows and...

Write the following program in Java.

Ask the user to enter the number of rows and columns for a 2-D array. Create the
array and fill it with random integers using (int)(Math.random() * 20) (or whatever
number you choose). Then, provide the user with several menu choices.
1. See the entire array.
2. See a specific row.
3. See a specific column.
4. See a specific value.
5. Calculate the average of each row.
6. Calculate the total of each column.
7. Exit
If the user chooses option:
1 - output the entire array in chart format.
2 - ask the user for the specific row number and output the values from that row,
all columns, going across the screen.
3 - ask the user for the specific column number and output the values from that
column, all rows, going down the screen.
4 - ask the user for the specific row and column number and output the value at
that position (1 value only).
5 - output the information in chart format, including placing the average at the
end of each row.
6 - output the information in chart format, including placing the total below each
column.
7 - thank the user and exit the program
Any other option should produce an error message.
The program should continue until the user chooses option 7.

Please try to use basic coding(nothing too complicated so I can understand)

Solutions

Expert Solution

/* Below is your required code,please upvote for the efforts*/

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
   int rows,cols;
   System.out.println("Enter rows and columns");
   rows=sc.nextInt();
   cols=sc.nextInt();
   int a[][]=new int[rows][cols];
   for(int i=0;i<rows;i++)
   for(int j=0;j<cols;j++)
   a[i][j]=(int)(Math.random() * 20);
  
   while(true){
   System.out.println("\n1. See the entire array. \n2. See a specific row.\n3. See a specific column.\n4. See a specific value.\n5. Calculate the average of each row.\n6. Calculate the total of each column.\n7. Exit\n");
   int choice=sc.nextInt();
   if(choice==1){
   for(int i=0;i<rows;i++){
   for(int j=0;j<cols;j++)
   System.out.print(a[i][j]+" ");
   System.out.println();
   }
   }
   else if(choice==2){
   System.out.println("\nEnter Row = ");
   int r=sc.nextInt();
   for(int j=0;j<cols;j++)
   System.out.print(a[r-1][j]+" ");
   }
   else if(choice==3){
   System.out.println("\nEnter Column = ");
   int c=sc.nextInt();
   for(int j=0;j<rows;j++)
   System.out.println(a[j][c-1]);
   }
   else if(choice==4){
   System.out.println("\nEnter Row and Column = ");
   int r=sc.nextInt();
   int c=sc.nextInt();
   System.out.println("\n"+a[r-1][c-1]);
   }
   else if(choice==5){
   float avg=0;
   for(int i=0;i<rows;i++){
   for(int j=0;j<cols;j++){
   System.out.print(a[i][j]+" ");
   avg+=a[i][j];
   }
   System.out.print(avg/cols+"\n");
  
   }
   }
   else if(choice==6){
   float[] avg=new float[cols];
   for(int i=0;i<rows;i++){
   for(int j=0;j<cols;j++){
   System.out.print(a[i][j]+" ");
   avg[j]+=a[i][j];
   }
   System.out.println();
   }
   for(int j=0;j<cols;j++)   
   System.out.print(avg[j]+" ");
   }
   else if(choice==7)
   System.exit(0);
   }
   }
}


Related Solutions

JAVA Write a program that prompts the user to enter a matrix number of rows and...
JAVA Write a program that prompts the user to enter a matrix number of rows and number of columns. In main method create 2D matrix based on the number of rows and columns input by the user; randomly fills the matrix with 0s and 1s and prints it. Create method sumColumns that takes only the matrix you created in main method and find the sum of each column with even index and prints it. Do not use global variables. Here...
Write a Java program to do the following: Ask the user to enter 10 first names...
Write a Java program to do the following: Ask the user to enter 10 first names (one word - no hyphen or apostrophe) using the keyboard. 1) Display the list of names one per line on the Console. 2) Display the names again, after eliminating duplicates using a HashSet (Your code MUST use HashSet).
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
Write a python program that will ask the user to enter any number of days. The...
Write a python program that will ask the user to enter any number of days. The program reads the number of days from the user, and returns how many years, months, and days are there in the given number of days. Your program should prompt the user to: Enter the number of days : 1152 The user types number of days (1152 above, for example) and hit enter key. The program then will print: 3 years 1 months 27 days...
Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
Using Java! Write a program that ask prompt the user to enter a dollar amount as...
Using Java! Write a program that ask prompt the user to enter a dollar amount as double. Then, calculate how many quarters, dimes, nickels and pennies are in the dollar amount. For example: $2.56 = 10 quarters, 1 dime, 1 nickel and 1 cent. Print all of the values. Hint: Use Modulus operator and integer division when necessary.
Write a java program that perform the following: 1. Ask a user ti enter 10 student...
Write a java program that perform the following: 1. Ask a user ti enter 10 student test score on a test (100 point test and save the score in an array 2. Iterate through the array to find the average and highest of these score, print them out 3 Count how many student score below average and print them out 4 . Instructor decide to curve the student score using square root curving method( take the square root of the...
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube of the number. The program should be called CubeNumbers. Use a value returning method with parameter, call the method myCube.
(8 marks) Write a program to ask user to enter an integer that represents the number...
Write a program to ask user to enter an integer that represents the number of elements, then generate an ArrayList containing elements which are all random integers in range [75, 144] , and finally display index and value of each element. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use ArrayList. Your program must use only printf(…) statements to adjust the alignment of your output. Your code must display the index in descending...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT