//please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks
import java.util.*;
public class Test {
/** Main method */
public static void main(String[] args) {
double[][] matrix = getMatrix();
// Display the sum of each column
for (int col = 0; col < matrix[0].length; col++) {
System.out.println(
"Sum " + col +
" is " + sumColumn(matrix, col));
}
}
/** getMatrix initializes an array with user input values */
public static double[][] getMatrix() {
final int ROWS = 3;
final int COLUMNS = 2;
double[][] m = new double[ROWS][COLUMNS];
// Prompt the user to enter a 3-by-4 matrix
for (int row = 0; row < m.length; row++)
for (int col = 0; col < m[row].length; col++)
System.out.println(20+(int) (Math.random() *50));
return m;
}
/** sum returns the sum of the elements in columnIndex */
public static double sumColumn(double[][] m, int columnIndex) {
double sum = 0;
for (int row = 0; row < m.length; row++) {
sum += m[row][columnIndex];
}
return sum;
}
}
I have done the first 4 parts of the problem. The last part is trivial and can be added easily in the code
import java.util.*;
public class Test {
/** Main method */
public static void main(String[] args) {
double[][] matrix = createArray();
double[] sum = new double[matrix[0].length];
for (int i = 0; i < matrix[0].length; i++) {
sum[i] = sumColumn(matrix, i);
}
printResult(matrix, sum);
}
/** getMatrix initializes an array with user input values
*/
public static double[][] createArray() {
final int ROWS = 3;
final int COLUMNS = 2;
double[][] m = new double[ROWS][COLUMNS];
// Create a random 3-by-2 matrix
for (int row = 0; row < ROWS; row++)
for (int col = 0; col < COLUMNS; col++)
// For every element of the 3x2 matrix
m[row][col] = 20 + (Math.random() * 30); // Genereate a random
double between 20 and 50 and insert it into the matrix
return m;
}
/** sum returns the sum of the elements in columnIndex */
public static double sumColumn(double[][] m, int columnIndex)
{
double sum = 0;
for (int row = 0; row < m.length; row++) {
sum += m[row][columnIndex];
}
return sum;
}
/* print the matrix */
public static void printResult(double[][] m, double [] sum) {
System.out.println("The matrix is: ");
for(int row = 0; row < m.length; row++) {
System.out.printf(" ");
for(int col = 0; col < m[0].length; col++) {
System.out.printf("%6.1f ", m[row][col]);
}
System.out.print("\n");
}
System.out.printf("Sum: ");
for (int i = 0; i < sum.length; i++) {
System.out.printf("%6.1f ", sum[i]);
}
}
}
Output:
//please help I can’t figure out how to print and can’t get the random numbers to...
JAVA Write a method that returns the sum of all the elements in a specified column in a matrix using the following header: public static double sumColumn(double [][] m, int columnIndex) Write another method that returns the sum of all the elements in a specified row in a matrix using the following header: public static double sumRow( double [][] m, int rowIndex) Write a test program that reads a 3-by-4 matrix and displays the sum of each column and sum...
Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right corner to the bottom left corner and i dont know how to do it please help me thank you dd another method to the bottom of the "TwoDimArraysMethods.java" file called "printDiagonalRL()" public static void printDiagonalRL(int[][] matrix) 4. Call this method in the main file ("TwoDimArraysAsParam.java") passing it "board." e.g. TwoDimArraysMethods.printDiagonal(board); 5. Your new method should print any numbers along the diagonal from...
(Sum elements column by column) Write a method that returns the sum of all the elements in a specified column in a matrix using the following header: public staticdouble sumColumn( double[] [] m, int columnIndex) Write a test program that reads a 3- by- 4 matrix and displays the sum of each column.
please use eclipse the sample output is incorrect CPS 2231 Chapter 8 Lab 1 Spring 2019 1. Write a class, SumOrRows. The main method will do the following methods: a. Call a method, createAndFillArray which: defines a 2 dim array of 3 rows and 4 columns, prompts the user for values and stores the values in the array. b. Calls a method, printArray, which: prints the Array as shown below c. Cails a method, calcSums, which: returns à 1 dimensional...
Please help, Array does not print properly. I need to print out the 8 class numbers entered by the user ! Java only using J option pane, you cannot use ulti or anything else only J option pane Program compiles but does not print the array correctly! import javax.swing.JOptionPane; public class programtrial { public static void main(String[] args) { int newclass = 0; int countclass = 0; final int class_Max = 8; int[] classarray =...
Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...
This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...
need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...
Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...
In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...