#4 Dano
package du4;
public class Matice {
private double[][] matice;
public void naplnSekvenci(double od) {
for (int i = 0; i < matice.length; i++) {
for (int j = 0; j < matice[i].length; j++) {
matice[i][j] = od++;
}
}
}
public double[][] getMatice(Matice m){
return m.matice;
}
private boolean kontrola(int i, int j) {
if (i < 0) {
return true;
}
if (j < 0) {
return true;
}
if (i >= matice.length) {
return true;
}
if (j >= matice.length) {
return true;
}
return false;
}
public void naplnSachovnici() {
int a = 0;
for (int i = 0; i < matice.length; i++) {
for (int j = 0; j < matice[i].length; j++) {
if ((a % 2) == 0) {
matice[i][j] = 0;
} else {
matice[i][j] = 1;
}
a++;
if (matice[0].length % 2 == 0) {
if (matice[0].length - 1 == j) {
a++;
}
}
}
}
}
public double prvek(int i, int j) {
double a = 0;
i--;
j--;
if (i < 0) {
return 0 / a;
}
if (j < 0) {
return 0 / a;
}
if (i > matice.length) {
return 0 / a;
}
if (j > matice[0].length) {
return 0 / a;
}
return matice[i][j];
}
public int pocetRadku() {
return matice.length;
}
public int pocetSloupcu() {
return matice[0].length;
}
public double soucet() {
double s = 0;
for (int i = 0; i < matice.length; i++) {
for (int j = 0; j < matice[i].length; j++) {
s = s + matice[i][j];
}
}
return s;
}
@Override
public String toString() {
String s = "";
for (int i = 0; i < matice.length; i++) {
for (int j = 0; j < matice[i].length; j++) {
//
if (j == 0) {
s = s + String.format("%5.2f", matice[i][j]);
} else {
if(matice[i][j] >= 100){
s = s + " ";
}
s = s + String.format("%6.2f", matice[i][j]);
}
if (j == (matice[0].length - 1)) {
if (i == matice.length - 1) {
} else {
s = s + "\n";
}
}
}
}
return s;
}
public void prohodRadky(int i, int j) {
i--;
j--;
double a;
if (kontrola(i, j) == true) {
} else {
for (int k = 0; k < matice[i].length; k++) {
a = matice[i][k];
matice[i][k] = matice[j][k];
matice[j][k] = a;
}
}
}
public Matice(int radky, int sloupce) {
radky = Math.abs(radky);
sloupce = Math.abs(sloupce);
if (radky == 0) {
radky = 1;
}
if (sloupce == 0) {
sloupce = 1;
}
matice = new double[radky][sloupce];
for (int i = 0; i < radky; i++) {
for (int j = 0; j < sloupce; j++) {
matice[i][j] = 0;
}
}
}
}
package du4;
import java.util.Scanner;
public class DU4 {
public static void main(String[] args) { Scanner scan = new Scanner(System.in);
int r, s, od;
System.out.println("---Vysledky---");
System.out.print("Zadej pocet radku:");
r = scan.nextInt();
if (r <= 0) {
System.out.println("Pocet radku musi byt vetsi nez 0.");
System.exit(1);
}
System.out.print("Zadej pocet sloupcu:");
s = scan.nextInt();
if (s <= 0) {
System.out.println("Pocet sloupcu musi byt vetsi nez 0.");
System.exit(1);
}
System.out.print("Zadej pocatechni hodnotu:");
od = scan.nextInt();
Matice m = new Matice(r,s);
System.out.println(m);
m.naplnSekvenci(od);
System.out.println("---");
System.out.println(m);
m.prohodRadky(1, 2);
System.out.println("---");
System.out.println(m);
m.naplnSachovnici();
System.out.println("---");
System.out.println(m);
System.out.println("---");
System.out.printf("soucet = %.2f%n", m.soucet());
m.prohodRadky(1, 2);
System.out.println("---");
System.out.println(m);
}
}