Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.hillel.elementary.javageeks.artem.homework2;

import java.util.Scanner;

public class Task2 {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need test for this class. And rename it to something more proper.

static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need main method anymore. Use tests for running everything.


int rows, columns, sum = 0, rowone, colone, rowtwo, coltwo;

System.out.print("Enter quantity of rows: ");
rows = scanner.nextInt();
System.out.print("Enter quantity of columns: ");
columns = scanner.nextInt();

int[][] array = new int[rows][columns];
add(array);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably fill or fillArray are better names

printArray(array);
System.out.println("Enter the coordinates of the upper left border");
System.out.print("row: ");
rowone = scanner.nextInt();
System.out.print("column: ");
colone = scanner.nextInt();

System.out.println();

System.out.println("Enter the coordinates of the lower right border");
System.out.print("row: ");
rowtwo = scanner.nextInt();
System.out.print("column: ");
coltwo = scanner.nextInt();



if(rowone<0 || colone<0 || rowtwo<0 || coltwo<0 || rowone>=columns || colone>=rows || rowtwo>=columns || coltwo>=rows){
System.out.println("Error in coordinates! Try again");
}else{
for (int i = rowone; i <= rowtwo; i++) {
for (int j = colone; j <= coltwo; j++) {
sum += array[i][j];
}
}

System.out.println("\nSum = "+sum);
}
}

public static void add(int[][] array){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = (int)(Math.random()*100);
}
}
}

public static void printArray(int[][] array){
System.out.println("\nArray:");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]+" ");
}
System.out.println();
}
System.out.println();
}

}