D
D
DmitriySwan2018-06-06 22:07:53
Java
DmitriySwan, 2018-06-06 22:07:53

How to properly declare these methods?

Here is the task itself: Define the Matrix class of dimension (nxn). Declare an array of m objects. Pass objects to a method that swaps rows with the maximum and minimum elements of the kth column. Output new matrices and row numbers.
There is a solution, but you need to declare these three methods. Please don't link to tutorials, just show how to properly declare these methods, or one of them. Thanks in advance)

spoiler
public void inputSize() throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter size: ");
        boolean correctInf;
        correctInf = false;
        while (correctInf != true) {
            try {
                n = Integer.parseInt(br.readLine());
                while (n <= 0) {
                    System.out.println("Error, please enter element > 0");
                    n = Integer.parseInt(br.readLine());
                }
                correctInf = true;
            }
            catch (NumberFormatException e) {
                System.out.println("Error, please enter another element ");
            }
        }
        correctInf = false;
    }
 
    public void InputNumberStolb() throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter nomer stolbca dlya sortirovki: ");
        boolean correctInf;
        correctInf = false;
        while (correctInf != true) {
            try {
                k = Integer.parseInt(br.readLine());
                while (k < 0) {
                    System.out.println("Error, please enter element >= 0");
                    k = Integer.parseInt(br.readLine());
                }
                correctInf = true;
            }
            catch (NumberFormatException e) {
                System.out.println("Error, please enter another element ");
            }
        }
        correctInf = false;
    }
 
    public void inputElement()throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        matrix = new int[n][n];
        boolean correctInf;
        System.out.println("Enter Main element: ");
        correctInf = false;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                while (correctInf != true) {
                    try {
                        matrix[i][j] = Integer.parseInt(br.readLine());
                        correctInf = true;
                    } catch (NumberFormatException e) {
                        System.out.println("Error, please enter another element ");
                    } //Math.round((Math.random()*2*n) - n);
                }
                correctInf = false;
            }
        }
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                System.out.print(matrix[i][j] + "\t");
            }
            System.out.println();
        }
    }
    public void sort(){
        int jmax = 0;
        int max;
        int min;
        int jmin = 0;
        int z = 0;
        
        for(int i = 0; i < n; i++){
            max = matrix[i][0];
            min = matrix[i][0];
            if (matrix[i][k] > max) {
                for (int j = 0; j < n; j++) {
                    max = matrix[i][j];
                    jmax = j;
                }
            }
            if (matrix[i][k] < min){
                for (int j = 0; j < n; j++){
                    min = matrix[i][j];
                    jmin = j;
                }
            }
            matrix[i][jmin] = max;
            matrix[i][jmax] = min;
 
        }
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                System.out.print(matrix[i][j] + "\t");
            }
            System.out.println();
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kinhagen, 2018-06-06
@Kinhagen

I am also learning and at first they explained to me this way (if it’s quite simple): for example, class a is created, a description of future class objects is written in it. Then the puskA class is created (it doesn’t matter what you call it, the meaning is that the program is launched from this class), class objects are created in this puskA and the main method is launched, and they are already working with these objects.
Well, now here the experts will catch up - they will teach you: D

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question