Question

please help with the TODO's in bold below! * Java application: CS140_Arrays.java * * Need to...

please help with the TODO's in bold below!

 * Java application: CS140_Arrays.java
 *
 * Need to be done!
 *
 * TODO#1: Change the name and date accordingly
 * Created by , 11/25/2019
 */

public class CS140_Arrays_Done
{
        //TODO#2: Define two private static constants: rows (set to 5) & cols (set to 3)



        //array for the values
        private static double[][] values = { { 9, 10, 8 },
                                                                                 { 3.5, 10, 8.5 },
                                                                                 { 10, 8.5, 9 },
                                                                                 { 8.5, 10, 7.5},
                                                                                 { 10, 7.5, 8 } };

        public static void main(String[] args)
        {
                //arrays for rowAvgs and colAvgs
                        //rowAvgs: average all values for each row
                        //colAvgs:  average all values for each column
                double[] rowAvgs = new double[rows];
                double[] colAvgs = new double[cols];

                calculateRowAvgs(rowAvgs);

                /* TODO#4: call method calculateColAvgs to have column average result */



    } //end main method

        //calculate the average of all values for each row
        public static void calculateRowAvgs(double[] rowAvgs)
        {
                /* TODO#3: add the nested for loop to
                 * calculate the average of all values for each row and print out
                 * use the next method as an example */


        } //end method calculateRowAvgs

        //calculate the average of all values for each column
        public static void calculateColAvgs(double[] colAvgs)
        {
                System.out.println();
                for (int i=0; i
0 0
Add a comment Improve this question Transcribed image text
Answer #1
/*Java application:CS140_Arrays.java
 *
 *Need to be done!
 *
 *TODO#1:Change the name and date accordingly
 *Created by,11/25/2019
 */

public class CS140_Arrays_Done {
    //TODO#2: Define two private static constants: rows (set to 5) & cols (set to 3)


    //array for the values
    private static double[][] values = {{9, 10, 8},
            {3.5, 10, 8.5},
            {10, 8.5, 9},
            {8.5, 10, 7.5},
            {10, 7.5, 8}};

    public static void main(String[] args) {
        //arrays for rowAvgs and colAvgs
        //rowAvgs: average all values for each row
        //colAvgs:  average all values for each column
        double[] rowAvgs = new double[values.length];
        double[] colAvgs = new double[values[0].length];

        calculateRowAvgs(rowAvgs);
        calculateColAvgs(colAvgs);

        System.out.print("Row averages are:");
        for (int i = 0; i < rowAvgs.length; i++) {
            System.out.print(" " + rowAvgs[i]);
        }
        System.out.println();

        System.out.print("Column averages are:");
        for (int i = 0; i < colAvgs.length; i++) {
            System.out.print(" " + colAvgs[i]);
        }
        System.out.println();
    } //end main method

    //calculate the average of all values for each row
    public static void calculateRowAvgs(double[] rowAvgs) {
        /* TODO#3: add the nested for loop to
         * calculate the average of all values for each row and print out
         * use the next method as an example */
        for (int i = 0; i < values.length; i++) {
            rowAvgs[i] = 0;
            for (int j = 0; j < values[i].length; j++) {
                rowAvgs[i] += values[i][j];
            }
            rowAvgs[i] /= values[i].length;
        }
    } //end method calculateRowAvgs

    //calculate the average of all values for each column
    public static void calculateColAvgs(double[] colAvgs) {
        for (int i = 0; i < values[0].length; i++) {
            colAvgs[i] = 0;
            for (int j = 0; j < values.length; j++) {
                colAvgs[i] += values[j][i];
            }
            colAvgs[i] /= values.length;
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
please help with the TODO's in bold below! * Java application: CS140_Arrays.java * * Need 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
  • 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:...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

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

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

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

  • Help pls! and kindly explain how you do this as well. Thaank you Write a program...

    Help pls! and kindly explain how you do this as well. Thaank you Write a program to test whether a square is a 3x3 magic square. A magic square is a grid with 3 rows and 3 columns, like the figure below. A magic square has the following properties: the grid contains only the numbers 1 through 9 the sum of each row, each column, and each diagonal all add up to the same number Notes: I have provided the...

  • create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array...

    create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array of doubles (call it scores) with three rows and three columns and that uses methods and loops as follows. Use a method containing a nested while loop to get the nine (3 x 3) doubles from the user at the command line. Use a method containing a nested for loop to compute the average of the doubles in each row. Use a method to...

  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

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