Question

//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;

}

}


Create a Java class program named XXXX_Program2.java. Please note the file name is case sensitive and it must be the same as the class name in your Java program. (method +1D array+ 2D array) Write a Java program to meet the following requirements 1. The program generates random numbers(between 20.0 to 50.0) into a 3(row)-by-2(column) matrix in double type. 2. Have below method to create the array: public static double[][] createArray() 3. Have below method to sum of each column: public static double sumColumn (double[][ m, int columnIndex) 4. Have below method to display the matrix, the sum and average of each column. All the numbers should be aligned well. Hint: Print numbers in %6.1f format. public static void printResult (double[ ][] m, double[] sum) 5. Print the max and min numbers of the matrix with their column and row positions. .Below is an example of output Output The matrix is 43.1 44.7 45.2 25.7 26.4 29.8 81.9 27.3 Sum: 133.1 Avg: 44.4 The maximum number is 45.2 at row 2, column 0 The minimum number is 25.7 at row 0, column 1 Program 2 is developed by <your Kean email ID>.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

The matrix is: 32.2 49.7 38.3 Sum: 120.2 24.3 40.2 22.8 87.3

Add a comment
Know the answer?
Add Answer to:
//please help I can’t figure out how to print and can’t get the random numbers to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Write a method that returns the sum of all the elements in a specified column...

    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...

    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

    (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...

    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...

    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...

    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...

    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...

    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...

    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...

    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:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT