Tema 2. Errores y Excepciones: modelo y gestión

Anuncio
!
!
"
"
!
#
$
"
"
!
"
&
"
'
%
"
(
(
#
*) +
!
"
!
"
(
"
'$) ),
!)
)
"
,
)
' ---)
public class TestSituacionesInesperadas {
private static Scanner teclado=new Scanner(System.in).useLocale(LocaleEDA);
/
0) 1
!
0)0$
0) 2"
3
0). 2 (
#
!
" # $
#
3
!
"
)0%
) 4
" # $
!
!
1
4
) 5
public static void main(String args[]){
GrupoDeFiguras g = new ArrayGrupoDeFiguras(); int opcion;
do {
opcion = menu(); if ( opcion != 0 ) procesar(opcion, g);
} while ( opcion != 0 );
}
private static int menu(){
int res = 0; System.out.println();
System.out.println("
*** MENÚ ***");
System.out.println("1.- Insertar una nueva Figura en el Grupo");
System.out.println("2.- Buscar una Figura en el Grupo");
System.out.println("3.- Borrar una Figura del Grupo");
System.out.println("4.- Mostrar las Figuras del Grupo");
System.out.println("5.- Calcular el área del Grupo");
System.out.println("6.- Ordenar el Grupo por área \n");
System.out.println("0 - Salir");
System.out.print("Seleccione opción: ");
res = teclado.nextInt();
return res;
}
"
&
!
) )0
) ) '
3
!
%
%
" #
" #
(
"(
6 #7
! #
8
(
8
9
-)):;<
.
private static void procesar(int opcion, ArrayGrupoDeFiguras g) {
switch (opcion) {
6# 7
%
case 1: Figura f = seleccionarFiguraALeer();
6# 7
#
f.leer(teclado);
4
<
g.insertar(f);
<
break;
case 2: Figura fB = seleccionarFiguraALeer();
6# 7
1
fB.leer(teclado);
8
(
System.out.println("Buscando la Figura "+fB);
4
<
int p = g.buscar(fB);
System.out.println(“1ªapar.de "+fB+”:”+g.recuperar(p).toString());
break;
case 3: Figura fD = seleccionarFiguraALeer();
fD.leer(teclado);
6# 7
System.out.println("Borrando "+fD);
Figura fDD = g.borrar(fD); 8
(
1
4
<
System.out.println("Borrada "+fD+" del Grupo: "+fDD.toString());
case
case
case
case
4:
5:
6:
7:
break;
System.out.println(g.toString()); break;
System.out.println("Área total :"+g.area()); break;
g.ordenar(); break;
System.out.println(“Posición de la Figura a recuperar del Grupo");
6# 7
int i = teclado.nextInt();
Figura fR = g.recuperar(i);6# 7
#
8
System.out.println("Recuperada la Figura
de la
! <"+fR+"
(
4 posición
< "+i);
}
}
private static void procesar(int opcion, ArrayGrupoDeFiguras g) {
private static Figura seleccionarFiguraALeer(){
Figura res = null;
System.out.println("Para leer Figura PULSE:");
System.out.println("1 para Círculo");
System.out.println("2 para Rectángulo");
System.out.println("3 para Cuadrado"); int op = teclado.nextInt();
if ( op == 1 ) res = new Circulo();
else if (op==2) res=new Rectangulo();
else if (op==3) res=new Cuadrado();
return res;
}
6# 7
! #
8
9
0)).;<
8
(
1
!
5
1
>
"
'
!
"
(@
,
!
!
'
!
3
!
'
"7
1
"
?
)
#
4
#
3
"
') )
" "
!
'
A
"
#
!
A
'
A
! B
) )B
!
!
(
?
A' "
#
'
"
"
1
! #
"
!
'
(
!
"
" "
"
#
!
A
=
C
! "
#$
" # $
"
"
" # $
A
" #
!
"
#
A
" # $
)
*
$%
&
&
D
+
$
Scanner
)
(
0-
" #
3
java.util
E
public double nextDouble()
Scans the next token of the input as a double.
This method will throw InputMismatchException if the next
token cannot be translated into a valid double value.
If the translation is successful, the scanner advances
past the input that matched.
...
Returns:
the double scanned from the input
Throws:
InputMismatchException - if the next token does not match the
Float regular expression, or is out of range
NoSuchElementException - if the input is exhausted
IllegalStateException - if this scanner is closed
00
!
3
3
@>
>
,
"
8
G"
$
3
GH
F
"
IF
I@
J
>
>
"
8
GI F
GI@J
J
0
'
+
.
(
#
'
-$
0
)
'
*$
+
,
#
'
#
.$
'
#
/
)
0
+
,
*$
'
)
#1
-$
'
)
#1
.$
'
)
#1
(
"#
"#
$
$
(
"#
(
)
#
.
(
/
#
'
)
)
'
2
)
#
(
&
0
'
*-
'
)
)
#
'
0.
%
0K
&
%
&
$0
A
'
*
23
$
"
" #
2
#
$0
" #
A
*
1
"
$
4
*
4
1
"
GM
N "
?3
'
&
'
#
O M
P))I@
$
"
#
)
#
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