Java - Convolucion validando excepciones

 
Vista:
sin imagen de perfil

Convolucion validando excepciones

Publicado por Daniela (2 intervenciones) el 20/10/2016 21:47:51
Hola amigos disculpen alguien me podría ayudar con mi convolucion por que no puedo terminarla necesito validar las excepciones para aprobar ya que es mi examen y no se como. También lo hice orientado a objetos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.util.Scanner;
 
public class Convolucion
{
    private int M;
    private int N;
    private int P;
    private int Q;
    private int Matriz1[][] = new int[N][M];
    private int Matriz2[][] = new int[Q][P];
    private int Matriz3[][] = new int[Q][P];
 
 
        Scanner entrada = new Scanner(System.in);
 
    public void RecibeDatos()
 
    {
      System.out.print("Dar el numero de columnas de la primera matriz: ");
      N=entrada.nextInt();
 
      System.out.print("Dar el numero de filas de la primera matriz: ");
      M= entrada.nextInt();
      Matriz1= new int[N][M];
 
      System.out.print("\nUsted indico una primera matriz  " );
      System.out.printf("de  %d  filas y %d columnas", M, N);
    for ( int i = 0; i < N; i++ )
    for ( int j = 0; j < M; j++ )
    {
    System.out.printf("\nIntroduzca el valor de la entrada en la fila %d  y la columna %d: ", i + 1,  j + 1 );
    Matriz1[i][j] = entrada.nextInt();
        }
    }
    public void RecibeDatos2( )
 
    {
 
      System.out.print("Dar el numero de columnas de la segunda matriz: ");
      Q=entrada.nextInt();
 
      System.out.print("Dar el numero de filas de la segunda matriz: ");
      P= entrada.nextInt();
 
      Matriz2= new int[Q][P];
    System.out.print("\nUsted indico una segunda matriz  " );
    System.out.printf("de  %d  filas y %d columnas", P, Q);
 
    for ( int i = 0; i < Q; i++ )
      for ( int j = 0; j < P; j++ )
    {
    System.out.printf("\nIntroduzca el valor en la fila %d y la columna %d: ",  i + 1,  j + 1 );
        Matriz2[i][j] = entrada.nextInt();
            }
 
    }
    public void Convolucionar( )
 
   {
    int i, j, k, l;
      Matriz3 = new int[N][P];
 
    for ( i= 0; i< Q; i++)
    {
       for ( j = 0; j < P; j++)
      {
        for (  k = 0; k < N; k++ )
		{
		for ( l=0; l<M; l++)
		{
         Matriz3[ i ][j ] += Matriz1[k][l]*Matriz2[i][j];
 
      }
     }
	 }
	 }
	 }
 
 
 
    public void Imprimir( )
  {
  int i, j, k;
 
  System.out.printf("\n\nMatrices: \n\n");
  for ( i = 0; i < N; i++ )
   {
 
    for ( k = 0; k < M; k++)
    {
  System.out.printf("%3d", Matriz1[i][k]);
  }
 
  System.out.printf("\t\t");
   for ( j = 0; j < P; j++)
   {
     if ( i <= (N - 1))
 
  System.out.printf("%3d", Matriz2[i][j]);
 
  else
  System.out.printf(" ");
    }
 
  System.out.printf ("\t\t");
   for ( j = 0; j < P; j++ )
  {
  System.out.printf("%3d", Matriz3[i][j]);
  }
 
  System.out.printf("\n");
 
  }
 
  if ( M > N)
  {
  int l = N;
 
  while ( l < M )
  {
  for ( i = 0; i < M; i++)
  System.out.printf(" ");
  System.out.printf("\t\t\t");
  for ( j = 0; j < P; j++ )
  System.out.printf("%3d", Matriz2[l][j]);
  System.out.printf("\n" );
  l++;
  }
  }
  }
 
}
 
 
 
 
 
public class MatricesConvolucion
{
 
   public static void main(String args[])
   {
 
   Convolucion obj = new Convolucion();
 
 
    obj.RecibeDatos();
 
 
    obj.RecibeDatos2();
 
 
    obj.Convolucionar();
 
 
    obj.Imprimir();
 
   }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Convolucion validando excepciones

Publicado por YYanes (144 intervenciones) el 21/10/2016 15:05:49
Hola:

puedes emplear Try-Catch para validar tus entradas, por ejemplo:

1
2
3
4
5
6
7
8
9
10
Scanner entrada = new Scanner(System.in);
try{
      System.out.print("Entre un número: ");
      N=entrada.nextInt();
      System.out.print("El valor entrado es un número válido.");
}
 
catch(Exception e){
  System.out.print("Error: el valor entrado NO es un número válido.");
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar