Home > Blockchain >  Combining two methods in one
Combining two methods in one

Time:11-06

I currently have 2 methods. The first method says what is the row with the highest average and the second one says what is the column with the highest average. I want to merge these two methods into one but I don't know how. the parameter is an array of doubles and the return type should be a string that says for the rows (R3 for example) and for the columns (C3)

What I tried to do is create 4 for-loops (2 for loops for the rows and 2 for loops for the columns) but when I try to return the highest average of the column it will say "unreachable statement" because I can't use return twice. Any suggestions?

the code below is the two methods I have right now. what the methods are doing is getting the averages of each row/column and then compare them.

public static double findHighestRow(double[][] grid) {
        int i, j;
        double sum = 0;
        double average = 0;
        double averageGreater = 0;
        
        for (i = 0; i < grid.length; i  ) {
            for (j = 0; j < grid[i].length; j  ) {
                sum = sum   grid[i][j];
            }
            average=sum/grid[i].length;
            sum=0;
            averageGreater = Math.max(average, sum / grid[i].length);
            System.out.println("Average of row "   (i 1)   " = "   average);
        }

        System.out.println(" row with highest average is "   (i)   " = "   averageGreater);
        return averageGreater;
    }
    
    public static double findHighestColumn(double grid[][]) {
        int k, l;
        double sum2 = 0;
        double average2 = 0;
        double averageGreater2 = 0;
        
        for (k = 0; k < grid.length; k  ) {
            for (l = 0; l < grid[k].length; l  ) {
                sum2=sum2 grid[k][l];
            }
            average2=sum2/grid[k].length;
            sum2 = 0;
            averageGreater2 = Math.max(average2, sum2 / grid[k].length);
            System.out.println("Average of column "   (k 1)   " = "   average2);
        }
        
        System.out.println(" column with highest average is "   (k)   " = "   averageGreater2);
        return averageGreater2;
    }

This is the code that is failing. Another problem is that I do not know how to convert the result into a string.

public static double findHighestRow(double[][] grid) {
        int i, j;
        double sum = 0;
        double average = 0;
        double averageGreater = 0;
        
        for (i = 0; i < grid.length; i  ) {
            for (j = 0; j < grid[i].length; j  ) {
                sum = sum   grid[i][j];
            }
            average=sum/grid[i].length;
            sum=0;
            averageGreater = Math.max(average, sum / grid[i].length);
            System.out.println("Average of row "   (i 1)   " = "   average);
        }
        
        System.out.println(" row with highest average is "   (i)   " = "   averageGreater);
        return averageGreater;
        
        int k, l;
        double sum2 = 0;
        double average2 = 0;
        double averageGreater2 = 0;
        
        for (k = 0; k < grid.length; k  ) {
            for (l = 0; l < grid[k].length; l  ) {
                sum2=sum2 grid[k][l];
            }
            average2=sum2/grid[k].length;
            sum2 = 0;
            averageGreater2 = Math.max(average2, sum2 / grid[k].length);
            System.out.println("Average of column "   (k 1)   " = "   average2);
        }
        
        System.out.println(" column with highest average is "   (k)   " = "   averageGreater2);
        return averageGreater2;
    }

CodePudding user response:

You cannot combine the contents of two methods that return values the way you did. Not properly scoping your "return" statements will result in a compilation error.

public static double findHighestRow(double[][] grid) {
        int i, j;
        double sum = 0;
        double average = 0;
        double averageGreater = 0;
        
        for (i = 0; i < grid.length; i  ) {
            for (j = 0; j < grid[i].length; j  ) {
                sum = sum   grid[i][j];
            }
            average=sum/grid[i].length;
            sum=0;
            averageGreater = Math.max(average, sum / grid[i].length);
            System.out.println("Average of row "   (i 1)   " = "   average);
        }
        
        System.out.println(" row with highest average is "   (i)   " = "   averageGreater);
        return averageGreater;
        
        int k, l; // this is unreachable code because of the return statement that preceded it.

You will need to properly scope those return statements. One possible way is by enclosing it in an if statement.

The important thing is to clearly outline your logic so you will know when to return a value, and how to return it. Pseudocode often helps beginner developers to figure this out. You can have something like this (pseudocode)

WHEN HIGHEST ROW FOUND
THEN FIND HIGHEST COLUMN
LASTLY RETURN ROW AND COL

Which implies that the logic will calculate one portion first, and then when that process is done, will calculate the other. Lastly, will return the value needed.

Also, if you are combining these two methods, the implication is that your method cannot return a single value anymore. Instead, it need to return a tuple ([row, col]). Since Java does not have Tuple as a data type, you will have to create your own. You can use an array, or you can use a class that represent a tuple. An easy way to do this is by using a Java record.

public record Tuple(int row, int col){}

If using this approach, your code will have to be modified something like this:

public static Tuple findHighestRow(double[][] grid) {
        int i, j;
        double sum = 0;
        double average = 0;
        double averageGreater = 0;
        
        for (i = 0; i < grid.length; i  ) {
            for (j = 0; j < grid[i].length; j  ) {
                sum = sum   grid[i][j];
            }
            average=sum/grid[i].length;
            sum=0;
            averageGreater = Math.max(average, sum / grid[i].length);
            System.out.println("Average of row "   (i 1)   " = "   average);
        }
        
        System.out.println(" row with highest average is "   (i)   " = "   averageGreater);
        
        int k, l;
        double sum2 = 0;
        double average2 = 0;
        double averageGreater2 = 0;
        
        for (k = 0; k < grid.length; k  ) {
            for (l = 0; l < grid[k].length; l  ) {
                sum2=sum2 grid[k][l];
            }
            average2=sum2/grid[k].length;
            sum2 = 0;
            averageGreater2 = Math.max(average2, sum2 / grid[k].length);
            System.out.println("Average of column "   (k 1)   " = "   average2);
        }
        
        System.out.println(" column with highest average is "   (k)   " = "   averageGreater2);
        return new Tuple(i, k);
    }

I'm also convinced there is a better algorithm to find these rows and columns. This looks like a brute force solution to me. That said, I just wanted to focus on your immediate issue. I am no algorithm expert, but I think if you create a POJO that captures a tuple of [row, col] and the value in a list or array, you can then much easier return the max value in your list rather than iterate again through the same 2D array twice. For example:

3, 2, 1
4, 5, 9
7, 8, 6 

Given a 2D array like the one above, you can capture the following data after one pass in a list of MyPojo objects

MyPojo: [0,0], 3
MyPojo: [1,2], 9
MyPojo: [2,1], 8

Then all you have to do is provide a method in MyPojo that returns the tuple of the highest value. For this, you will need to create a Comparator. You can use a lambda expression for this.

MyPojo maxValue = myList
      .stream()
      .max(Comparator.comparing(MyPojo::value))

The getter method name value is correct, if using a Java Record. Records don't append get or set to the getters and setters. If using a plain class, it would be whatever the method name of that getter function. Once you have the object, you can either return the [row, col] or the value.

CodePudding user response:

After reviewing the question, I suspect the O/P wants the result of two methods combined into one. But, first, the unreachable statement:

Part 1: Unreachable Statement

Consider the first part of your code, which has this in the middle:

 ... 
 System.out.println(" row with highest average is "   (i) 
                    " = "   averageGreater);
 return averageGreater;
 ...

Execution of that return statement is unavoidable. It is not subject to an if or other conditional flow control.

Execution of a return statement terminates execution of the method that contains it. So, reaching that return terminates execution of the findHighestRow(double[][] grid) method

Because reaching that return is unavoidable, the code below it cannot be executed. That causes the "unreachable statement" error.

Part 2: How to convert the result into a string.

You say you "do not know how to convert the result into a string."

You actually do know how to do that. You've done it in your code.

Consider, for example, this statement, from your code:

 System.out.println(" row with highest average is "   (i) 
                          " = "   averageGreater);

System.out is an instance of a PrintStream. The overloaded println method you are using is taking a String argument. That's one String, not four.

So, how does that work? Although String is a class, the Java compiler provides special support for it, which includes overloading to act as a String concatenation operator. When used on a primitive, the is overloaded to convert the primitive to a String. For Objects, it acts as if the toString method was called.

 public static void main(String[] args) {
    String foo = "foo";
    String bar = "bar";       
    int bin = 3;
    String foobar = foo   bar   bin;
    System.out.println (foobar);        
}        

Output: foobar3

So, a potential solution to your problem might be this:

Change

 public static double findHighestRow(double[][] grid) {

to

 public static String findHighestRow (double[][] grid) {

which ends thus:

    return (" row with highest average is "   (i)   " = "   averageGreater);
 }

Make similar changes to your public static double findHighestColumn(double grid[][]) .

If desired, both methods and the results they return can be combined in one statement, similar to this:

 String result = findHighestRow (theGrid)   findHighestColumn (theGrid);
  • Related