mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-09 18:03:09 +01:00
feat (ch 4): blocks 4, 5, 6
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque04;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Ejercicio01_MostrarCadenaOrdenInverso {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String s;
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Introduzca cadena: ");
|
||||||
|
s = sc.nextLine();
|
||||||
|
|
||||||
|
muestraCadenaOrdenInverso(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void muestraCadenaOrdenInverso(String str) {
|
||||||
|
for (int i = str.length() - 1; i > -1; i--) {
|
||||||
|
System.out.print(str.charAt(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque04;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Ejercicio02_ContarVocalesYConsonantes {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String s;
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Introduzca cadena: ");
|
||||||
|
s = sc.nextLine();
|
||||||
|
|
||||||
|
contarVocalesYConsonantes(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void contarVocalesYConsonantes(String str) {
|
||||||
|
int contVocales = 0, contConsonantes = 0;
|
||||||
|
|
||||||
|
for (int i = str.length() - 1; i > -1; i--) {
|
||||||
|
char c = str.charAt(i);
|
||||||
|
System.out.println(c);
|
||||||
|
|
||||||
|
if ( (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ) {
|
||||||
|
|
||||||
|
if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'
|
||||||
|
|| c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
|
||||||
|
contVocales++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
contConsonantes++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Vocales: " + contVocales + " - Consonantes: "
|
||||||
|
+ contConsonantes);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque04;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Ejercicio03_Palindromo {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String s;
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Introduzca cadena: ");
|
||||||
|
s = sc.nextLine();
|
||||||
|
|
||||||
|
if (esPalindromo(s)) System.out.println("Es un palíndromo");
|
||||||
|
else System.out.println("NO es un palíndromo");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static boolean esPalindromo(String str) {
|
||||||
|
for (int i = 0; i < (str.length() / 2); i++) {
|
||||||
|
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque04;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Ejercicio04_ContarPalabras {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String s;
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Introduzca cadena: ");
|
||||||
|
s = sc.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Palabras: " + cuentaPalabras(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static int cuentaPalabras(String str) {
|
||||||
|
int count =0;
|
||||||
|
boolean existePalabra = true;
|
||||||
|
|
||||||
|
for (int i = 0; i < str.length(); i++) {
|
||||||
|
|
||||||
|
if (str.charAt(i) >= 'A' && str.charAt(i)<= 'Z' ||
|
||||||
|
str.charAt(i) >= 'a' && str.charAt(i)<= 'z'
|
||||||
|
&& existePalabra == true ) {
|
||||||
|
|
||||||
|
count++;
|
||||||
|
existePalabra = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (str.charAt(i) == ' ') {
|
||||||
|
existePalabra = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque04;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Ejercicio05_CompararCadenas {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String s1, s2;
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Introduzca cadena s1: ");
|
||||||
|
s1 = sc.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Introduzca cadena s2: ");
|
||||||
|
s2 = sc.nextLine();
|
||||||
|
|
||||||
|
if (comparaCadenas(s1, s2) == -1) {
|
||||||
|
System.out.println(s1 + " es menor que " + s2);
|
||||||
|
}
|
||||||
|
else System.out.println(s2 + " es menor que " + s1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static int comparaCadenas(String s1, String s2) {
|
||||||
|
|
||||||
|
int menorLongitud = (s1.length() < s2.length())? s1.length() : s2.length();
|
||||||
|
|
||||||
|
for (int i = 0; i < menorLongitud; i++) {
|
||||||
|
if (s1.charAt(i) < s2.charAt(i)) return -1;
|
||||||
|
else if (s1.charAt(i) > s2.charAt(i)) return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s1.length() == s2.length()) return 0;
|
||||||
|
return (s1.length() < s2.length())? -1 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque04;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Ejercicio06_ObtenerCadenaMinusculas {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String s;
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Introduzca cadena: ");
|
||||||
|
s = sc.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Convertida a minúsculas es: " +
|
||||||
|
convierteAMinusculas(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static String convierteAMinusculas(String s) {
|
||||||
|
String res = "";
|
||||||
|
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
|
||||||
|
res += (char) (s.charAt(i) + 32);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res += s.charAt(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque05;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Ejercicio01_EliminarEspaciosEnBlancoEnCadena {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner (System.in);
|
||||||
|
|
||||||
|
System.out.println("Introduzca la cadena: ");
|
||||||
|
String cadena = sc.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Sin espacios: " + eliminaEspaciosEnBlanco(cadena));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String eliminaEspaciosEnBlanco (String str) {
|
||||||
|
StringBuffer strSinEspacios = new StringBuffer();
|
||||||
|
for (int i = 0; i < str.length(); i++) {
|
||||||
|
if (str.charAt(i) != ' ') {
|
||||||
|
// strSinEspacios += str.charAt(i);
|
||||||
|
strSinEspacios.append(str.charAt(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return strSinEspacios.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque05;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Ejercicio02_CifradoCesar {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner (System.in);
|
||||||
|
|
||||||
|
System.out.println("Introduzca la cadena: ");
|
||||||
|
// String cadena = sc.nextLine();
|
||||||
|
String cadena = "rafaxyz";
|
||||||
|
|
||||||
|
System.out.println("César: " + cifradoCesar(cadena, 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String cifradoCesar (String str, int clave) {
|
||||||
|
String abecedario = "abcdefghijklmnñopqrstuvwxyz";
|
||||||
|
|
||||||
|
StringBuffer strCesar = new StringBuffer();
|
||||||
|
|
||||||
|
for (int i = 0; i < str.length(); i++) {
|
||||||
|
int posicionCharOriginal = abecedario.indexOf(str.charAt(i));
|
||||||
|
System.out.println();
|
||||||
|
int posicionTrasCifradoCesar = (posicionCharOriginal + clave) % abecedario.length();
|
||||||
|
strCesar.append(abecedario.charAt(posicionTrasCifradoCesar));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return strCesar.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque05;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Ejercicio03_ContarOcurrencias {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
System.out.println("Contar ocurrencias: " +
|
||||||
|
contarOcurrencias("rafa está afónicaf", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int contarOcurrencias (String str, String patron) {
|
||||||
|
if (patron == "") {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cont = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i <= (str.length() - patron.length()); i++) {
|
||||||
|
if (str.charAt(i) == patron.charAt(0)) {
|
||||||
|
boolean patronCoincide = true;
|
||||||
|
for (int j = 0; j < patron.length(); j++) {
|
||||||
|
if (str.charAt(i + j) != patron.charAt(j)) {
|
||||||
|
patronCoincide = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (patronCoincide == true) {
|
||||||
|
cont++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cont;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque05;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Ejercicio04_EncontrarPalabraMasLarga {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
System.out.println("Palabra más larga: " +
|
||||||
|
palabraMasLarga("rafa está afónico hoy"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String palabraMasLarga (String str) {
|
||||||
|
String palabraCandidata = "";
|
||||||
|
String palabraMasLarga = "";
|
||||||
|
|
||||||
|
str += ' ';
|
||||||
|
|
||||||
|
for (int i = 0; i < str.length(); i++) {
|
||||||
|
if (str.charAt(i) == ' ') { // He terminado una palabra
|
||||||
|
System.out.println(palabraCandidata);
|
||||||
|
if (palabraCandidata.length() > palabraMasLarga.length()) {
|
||||||
|
palabraMasLarga = palabraCandidata;
|
||||||
|
}
|
||||||
|
palabraCandidata = "";
|
||||||
|
}
|
||||||
|
else { // Estoy visitando las letras de una palabra
|
||||||
|
palabraCandidata += str.charAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return palabraMasLarga;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque05;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Ejercicio05_InvertirOrdenLetrasEnPalabras {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
System.out.println("Contar ocurrencias: " +
|
||||||
|
palabraMasLarga("rafa está afónico hoy"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String palabraMasLarga (String str) {
|
||||||
|
String palabra = "";
|
||||||
|
String salida = "";
|
||||||
|
|
||||||
|
str += ' ';
|
||||||
|
|
||||||
|
for (int i = 0; i < str.length(); i++) {
|
||||||
|
if (str.charAt(i) == ' ') { // He terminado una palabra
|
||||||
|
System.out.println(palabra);
|
||||||
|
// Recorro la palabra al revés y la agrego al String salida
|
||||||
|
for (int j = palabra.length() - 1; j > -1; j--) {
|
||||||
|
salida += palabra.charAt(j);
|
||||||
|
}
|
||||||
|
salida += " ";
|
||||||
|
palabra = "";
|
||||||
|
}
|
||||||
|
else { // Estoy visitando las letras de una palabra
|
||||||
|
palabra += str.charAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
// Quita el último espacio
|
||||||
|
return salida.substring(0, salida.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque05;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Ejercicio06_Pangrama {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
System.out.println("Pangrama: " +
|
||||||
|
esPangrama("rafa esta afonico hoy, pero esta bien"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static boolean esPangrama (String str) {
|
||||||
|
String abecedario = "abcdefghijklmnñopqrstuvwxyz";
|
||||||
|
|
||||||
|
for (int i = 0; i < abecedario.length(); i++) {
|
||||||
|
if (str.indexOf(abecedario.charAt(i)) == -1) {
|
||||||
|
System.out.println("no encontrado en cadena " + abecedario.charAt(i));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,274 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque06;
|
||||||
|
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ej01_EjerciciosMatrices {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int m[][] = new int[][] { {1, 0, 0, 1, 0},
|
||||||
|
{0, 2, 0, 1, 0},
|
||||||
|
{0, 0, 3, 1, 0},
|
||||||
|
{0, 0, 0, 4, 0},
|
||||||
|
{0, 0, 0, 1, 5} };
|
||||||
|
|
||||||
|
// inicializaMatrizAlAzar(m);
|
||||||
|
imprimeMatriz(m);
|
||||||
|
System.out.println("Es positiva: " + esMatrizPositiva(m));
|
||||||
|
System.out.println("Es diagonal: " + esMatrizDiagonal(m));
|
||||||
|
System.out.println("Es triangular superior: " + esMatrizTriangularSuperior(m));
|
||||||
|
System.out.println("Es dispersa: " + esMatrizDispersa(m));
|
||||||
|
|
||||||
|
int array[] = arrayFromMatriz(m);
|
||||||
|
UtilsArrays.imprimeArray(array);
|
||||||
|
|
||||||
|
System.out.println("Es simétrica: " + esMatrizSimetrica(m));
|
||||||
|
|
||||||
|
int traspuesta[][] = dameMatrizTraspuesta(m);
|
||||||
|
imprimeMatriz(traspuesta);
|
||||||
|
|
||||||
|
matrizOpuesta(m);
|
||||||
|
imprimeMatriz(m);
|
||||||
|
|
||||||
|
int t[][] = eliminaFila(m, 2);
|
||||||
|
imprimeMatriz(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
*/
|
||||||
|
public static void inicializaMatrizAlAzar(int m[][]) {
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
m[i][j] = (int) Math.round(Math.random() * 100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
*/
|
||||||
|
public static void imprimeMatriz(int m[][]) {
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
System.out.print(m[i][j] + "\t");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean esMatrizPositiva(int m[][]) {
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
if (m[i][j] < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean esMatrizDiagonal (int m[][]) {
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
if ( (i != j && m[i][j] != 0) // Si un elemento fuera de la diagonal es distinto de 0
|
||||||
|
||
|
||||||
|
(i == j && m[i][j] == 0) // Si un elemento en la diagonal es igual a 0
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean esMatrizTriangularSuperior (int m[][]) {
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
if ( i > j && m[i][j] != 0 ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean esMatrizDispersa (int m[][]) {
|
||||||
|
// Con el primer for paso por todas las filas
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
boolean hayCero = false;
|
||||||
|
// Con el segundo for paso por todos los elementos de cada fila
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
if (m[i][j] == 0) {
|
||||||
|
hayCero = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// He terminado una fila, compruebo si había al menos un cero
|
||||||
|
if (hayCero == false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Con el primer for paso por todas las columnas
|
||||||
|
for (int i = 0; i < m[0].length; i++) {
|
||||||
|
boolean hayCero = false;
|
||||||
|
// Con el segundo for paso por todos los elementos de cada columna
|
||||||
|
for (int j = 0; j < m.length; j++) {
|
||||||
|
if (m[j][i] == 0) {
|
||||||
|
hayCero = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// He terminado una fila, compruebo si había al menos un cero
|
||||||
|
if (hayCero == false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si llego hasta aquí la matriz es dispersa
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int[] arrayFromMatriz (int m[][]) {
|
||||||
|
// Comienzo calculando la dimensión, en elementos, de la matriz
|
||||||
|
int dimensionArray = 0;
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
dimensionArray += m[i].length;
|
||||||
|
}
|
||||||
|
|
||||||
|
int a[] = new int[dimensionArray];
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
a[i * m[0].length + j] = m[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean esMatrizSimetrica (int m[][]) {
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
if (m[i][j] != m[j][i] ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int[][] dameMatrizTraspuesta (int m[][]) {
|
||||||
|
int t[][] = new int[m[0].length][m.length];
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
t[j][i] = m[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static void matrizOpuesta (int m[][]) {
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
m[i][j] = -m[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int[][] eliminaFila (int m[][], int filaAEliminar) {
|
||||||
|
|
||||||
|
if (filaAEliminar >= m.length) {
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sólo llego aquí si no se ha entrado en el "if"
|
||||||
|
int nueva[][] = new int[m.length - 1][m[0].length];
|
||||||
|
int k = 0;
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
if (filaAEliminar != i) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
nueva[k][j] = m[i][j];
|
||||||
|
}
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nueva;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user