Use this file to discover all available pages before exploring further.
Most real-world programs share the same skeleton: read input → display a menu → dispatch to the right operation → repeat until the user quits. Clase 9 and Clase 10 build this pattern from scratch — starting from a handful of hardcoded variables and ending with a fully refactored, exception-safe console application. Follow the evolution step by step.
The CalculadoraUTN application accepts two numbers, performs an arithmetic operation chosen from a menu, and loops until the user selects Salir (Exit). Each lesson adds one layer of functionality.
1
Define variables and the entry point (9.2)
The first version hardcodes the operands and prints a single result. There is no user interaction yet — but it establishes the class structure.
CalculadoraUTN.java — 9.2
public class CalculadoraUTN { public static void main(String[] args) { System.out.println("******* Aplicacion Calculadora *******"); var operando1 = 10; var operando2 = 20; var resultado = operando1 + operando2; System.out.println("resultado = " + resultado); }}
2
Add Scanner for live input (9.3)
Scanner wraps System.in and lets us read lines the user types. Integer.parseInt() converts the raw String to an int.
CalculadoraUTN.java — 9.3
import java.util.Scanner;public class CalculadoraUTN { public static void main(String[] args) { Scanner entrada = new Scanner(System.in); System.out.println("******* Aplicacion Calculadora *******"); System.out.print("Digite el valor para el operando1: "); var operando1 = Integer.parseInt(entrada.nextLine()); System.out.print("Digite el valor para el operando2: "); var operando2 = Integer.parseInt(entrada.nextLine()); var resultado = operando1 + operando2; System.out.println("resultado = " + resultado); }}
3
Display the menu and read the operation (9.4)
A text block ("""...""") prints the menu cleanly. The chosen operation number gates whether operands are requested.
CalculadoraUTN.java — 9.4
import java.util.Scanner;public class CalculadoraUTN { public static void main(String[] args) { Scanner entrada = new Scanner(System.in); System.out.println("******* Aplicacion Calculadora *******"); //Mostramos el menú System.out.println(""" 1. Suma 2. Resta 3. Multiplicacion 4. Division 5. Salir """); System.out.print("Operacion a realizar?"); var operacion = Integer.parseInt(entrada.nextLine()); if (operacion >= 1 && operacion <= 4) { System.out.print("Digite el valor para el operando1: "); var operando1 = Integer.parseInt(entrada.nextLine()); System.out.print("Digite el valor para el operando2: "); var operando2 = Integer.parseInt(entrada.nextLine()); } }}
4
Dispatch operations with a switch statement (9.5)
The arrow-syntax switch (case 1 -> { ... }) dispatches each menu option without fall-through. A default branch handles any invalid input. Note the switch lives inside the if block that guards valid operation numbers.
CalculadoraUTN.java — 9.5
import java.util.Scanner;public class CalculadoraUTN { public static void main(String[] args) { Scanner entrada = new Scanner(System.in); System.out.println("******* Aplicacion Calculadora *******"); //Mostramos el menú System.out.println(""" 1. Suma 2. Resta 3. Multiplicacion 4. Division 5. Salir """); System.out.print("Operacion a realizar? "); var operacion = Integer.parseInt(entrada.nextLine()); if (operacion >= 1 && operacion <= 4) { System.out.print("Digite el valor para el operando1: "); var operando1 = Integer.parseInt(entrada.nextLine()); System.out.print("Digite el valor para el operando2: "); var operando2 = Integer.parseInt(entrada.nextLine()); int resultado; switch (operacion) { case 1 -> { //Suma resultado = operando1 + operando2; System.out.println("Resultado de la suma: "+resultado); } case 2 -> { //Resta resultado = operando1 - operando2; System.out.println("Resultado de la resta: "+resultado); } case 3 -> { //Multiplicacion resultado = operando1 * operando2; System.out.println("Resultado de la multiplicacion: "+resultado); } case 4 -> { //Division resultado = operando1 / operando2; System.out.println("Resultado de la division: "+resultado); } default -> System.out.println("Opcion erronea: "+operacion); } //Fin switch } //Fin del if else if (operacion == 5) { System.out.print("Hasta pronto..."); } else { System.out.println("Opcion erronea: "+operacion); } }}
5
Wrap everything in a while loop with break (9.6)
while(true) makes the menu repeat indefinitely. When the user picks option 5, break exits the loop cleanly.
CalculadoraUTN.java — 9.6
import java.util.Scanner;public class CalculadoraUTN { public static void main(String[] args) { Scanner entrada = new Scanner(System.in); while(true){ //Ciclo infinito System.out.println("******* Aplicacion Calculadora *******"); //Mostramos el menú System.out.println(""" 1. Suma 2. Resta 3. Multiplicacion 4. Division 5. Salir """); System.out.print("Operacion a realizar? "); var operacion = Integer.parseInt(entrada.nextLine()); if (operacion >= 1 && operacion <= 4) { System.out.print("Digite el valor para el operando1: "); var operando1 = Integer.parseInt(entrada.nextLine()); System.out.print("Digite el valor para el operando2: "); var operando2 = Integer.parseInt(entrada.nextLine()); int resultado; switch (operacion) { case 1 -> { resultado = operando1 + operando2; System.out.println("Resultado de la suma: "+resultado); } case 2 -> { resultado = operando1 - operando2; System.out.println("Resultado de la resta: "+resultado); } case 3 -> { resultado = operando1 * operando2; System.out.println("Resultado de la multiplicacion: "+resultado); } case 4 -> { resultado = operando1 / operando2; System.out.println("Resultado de la division: "+resultado); } default -> System.out.println("Opcion erronea: "+operacion); } //Fin switch } //Fin del if else if (operacion == 5) { System.out.print("Hasta pronto..."); break; //Rompe el ciclo y sale } else { System.out.println("Opcion erronea: "+operacion); } //Imprimimos un salto de linea antes de repetir el menu System.out.println(); } //Fin while }}
6
Add try-catch for input validation (9.7)
If the user types a letter instead of a number, Integer.parseInt() throws a NumberFormatException. Wrapping the body of the loop in try-catch keeps the application alive.
CalculadoraUTN.java — 9.7
import java.util.Scanner;public class CalculadoraUTN { public static void main(String[] args) { Scanner entrada = new Scanner(System.in); while(true){ //Ciclo infinito System.out.println("******* Aplicacion Calculadora *******"); //Mostramos el menú System.out.println(""" 1. Suma 2. Resta 3. Multiplicacion 4. Division 5. Salir """); System.out.print("Operacion a realizar? "); try { var operacion = Integer.parseInt(entrada.nextLine()); if (operacion >= 1 && operacion <= 4) { System.out.print("Digite el valor para el operando1: "); var operando1 = Integer.parseInt(entrada.nextLine()); System.out.print("Digite el valor para el operando2: "); var operando2 = Integer.parseInt(entrada.nextLine()); int resultado; switch (operacion) { case 1 -> { resultado = operando1 + operando2; System.out.println("Resultado de la suma: " + resultado); } case 2 -> { resultado = operando1 - operando2; System.out.println("Resultado de la resta: " + resultado); } case 3 -> { resultado = operando1 * operando2; System.out.println("Resultado de la multiplicacion: " + resultado); } case 4 -> { resultado = operando1 / operando2; System.out.println("Resultado de la division: " + resultado); } default -> System.out.println("Opcion erronea: " + operacion); } //Fin switch } //Fin del if else if (operacion == 5) { System.out.print("Hasta pronto..."); break; //Rompe el ciclo y sale } else { System.out.println("Opcion erronea: " + operacion); } //Imprimimos un salto de linea antes de repetir el menu System.out.println(); } catch (Exception e) { //Fin del try, comienzo del catch System.out.println("Ocurrio un error: " + e.getMessage()); System.out.println(); } } //Fin while }}
7
Extract mostrarMenu() for clean structure (9.8)
Moving the menu-printing logic into a private static method reduces duplication and makes main easier to read.
CalculadoraUTN.java — 9.8
import java.util.Scanner;public class CalculadoraUTN { public static void main(String[] args) { Scanner entrada = new Scanner(System.in); while(true){ //Ciclo infinito System.out.println("******* Aplicacion Calculadora *******"); mostrarMenu(); try { var operacion = Integer.parseInt(entrada.nextLine()); if (operacion >= 1 && operacion <= 4) { System.out.print("Digite el valor para el operando1: "); var operando1 = Integer.parseInt(entrada.nextLine()); System.out.print("Digite el valor para el operando2: "); var operando2 = Integer.parseInt(entrada.nextLine()); int resultado; switch (operacion) { case 1 -> { resultado = operando1 + operando2; System.out.println("Resultado de la suma: " + resultado); } case 2 -> { resultado = operando1 - operando2; System.out.println("Resultado de la resta: " + resultado); } case 3 -> { resultado = operando1 * operando2; System.out.println("Resultado de la multiplicacion: " + resultado); } case 4 -> { resultado = operando1 / operando2; System.out.println("Resultado de la division: " + resultado); } default -> System.out.println("Opcion erronea: " + operacion); } //Fin switch } //Fin del if else if (operacion == 5) { System.out.print("Hasta pronto..."); break; //Rompe el ciclo y sale } else { System.out.println("Opcion erronea: " + operacion); } //Imprimimos un salto de linea antes de repetir el menu System.out.println(); } catch (Exception e) { //Fin del try, comienzo del catch System.out.println("Ocurrio un error: " + e.getMessage()); System.out.println(); } } //Fin while } private static void mostrarMenu(){ //Mostramos el menú System.out.println(""" 1. Suma 2. Resta 3. Multiplicacion 4. Division 5. Salir """); System.out.print("Operacion a realizar? "); }}
8
Extract ejecutarOperacion() — final refactored version (9.9)
The fully refactored application separates all concerns into dedicated methods. The final version also upgrades operands from int to double to support decimal results.
CalculadoraUTN.java — 9.9 (complete)
import java.util.Scanner;public class CalculadoraUTN { public static void main(String[] args) { Scanner entrada = new Scanner(System.in); while(true){ //Ciclo infinito System.out.println("******* Aplicacion Calculadora *******"); mostrarMenu(); try { var operacion = Integer.parseInt(entrada.nextLine()); if (operacion >= 1 && operacion <= 4) { ejecutarOperacion(operacion, entrada); } //Fin del if else if (operacion == 5) { System.out.print("Hasta pronto..."); break; //Rompe el ciclo y sale } else { System.out.println("Opcion erronea: " + operacion); } //Imprimimos un salto de linea antes de repetir el menu System.out.println(); } catch (Exception e) { //Fin del try, comienzo del catch System.out.println("Ocurrio un error: " + e.getMessage()); System.out.println(); } } //Fin while } private static void mostrarMenu(){ //Mostramos el menú System.out.println(""" 1. Suma 2. Resta 3. Multiplicacion 4. Division 5. Salir """); System.out.print("Operacion a realizar? "); } //Fin metodo mostrarMenu private static void ejecutarOperacion(int operacion, Scanner entrada){ System.out.print("Digite el valor para el operando1: "); var operando1 = Double.parseDouble(entrada.nextLine()); System.out.print("Digite el valor para el operando2: "); var operando2 = Double.parseDouble(entrada.nextLine()); double resultado; switch (operacion) { case 1 -> { //Suma resultado = operando1 + operando2; System.out.println("Resultado de la suma: " + resultado); } case 2 -> { //Resta resultado = operando1 - operando2; System.out.println("Resultado de la resta: " + resultado); } case 3 -> { //Multiplicacion resultado = operando1 * operando2; System.out.println("Resultado de la multiplicacion: " + resultado); } case 4 -> { //Division resultado = operando1 / operando2; System.out.println("Resultado de la division: " + resultado); } default -> System.out.println("Opcion erronea: " + operacion); } //Fin switch } //Fin metodo ejecutar operacion}
Separating menu display (mostrarMenu) from business logic (ejecutarOperacion) is not just style — it’s the foundation of clean architecture. Each method does exactly one thing, which makes unit testing, debugging, and future changes much simpler. If you later need to add a “Modulo” option, you touch only mostrarMenu() and ejecutarOperacion(), not main.
Clase 10 applies the same console-app pattern to a more realistic scenario: managing a list of Persona objects in memory. It introduces ArrayList, constructor overloading, and a boolean flag instead of break for loop control.
The Persona class is built up across three incremental source files (10.2 parts 1–3). The final version includes a static counter that auto-assigns an id to every new instance.
Persona.java
public class Persona { private int id; private String nombre; private String tel; private String email; private static int numeroPersonas = 0; //Constructor vacio public Persona(){ this.id = ++Persona.numeroPersonas; } //Constructor con parámetros (sobrecarga de constructores) public Persona(String nombre, String tel, String email){ this(); // calls no-arg constructor to set the id this.nombre = nombre; this.tel = tel; this.email = email; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNombre() { return nombre; } public void setNombre(String nombre) { this.nombre = nombre; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Persona{" + "id=" + id + ", nombre='" + nombre + '\'' + ", tel='" + tel + '\'' + ", email='" + email + '\'' + '}'; }}
import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class ListadoPersona { public static void main(String[] args) { Scanner entrada = new Scanner(System.in); //Definimos la lista fuera del ciclo while List<Persona> personas = new ArrayList<>(); //Empezamos con el menú var salir = false; while(!salir){ mostrarMenu(); }//Fin del ciclo While }//Fin método main private static void mostrarMenu() { //mostramos las opciones System.out.print(""" ******* Listado de Personas ******* 1. Agregar 2. Listar 3. Salir """); System.out.print("Digite una de las opciones: "); }}
The personas.forEach(System.out::println) call on the list uses a method reference — a shorthand for personas.forEach((p) -> System.out.println(p)). It calls toString() on each Persona automatically, which is why overriding toString() in the bean matters.