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

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 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...
(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...
Write, save, and run a Python program Ask the user to enter a number of grams....
Write, save, and run a Python program Ask the user to enter a number of grams. Your program converts that to whole tones, then whole kilograms and whatever is left is in grams. It prints the entered grams , tones , kilograms and the remaining grams.      4 marks for i in range(0,10): print(i) 2    what is wrong in the above python code? display your explanation using print statement. mark 3    insert the following comments into your program of part...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they type DONE to quit. The program should DOUBLE each of the digits in the number and display the original entry AND the SUM of the doubled digits. Hint: Use the // and % operators to accomplish this. Print the COUNT of entries that were invalid Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46. User Entry Output...
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT