SHOW:
|
|
- or go back to the newest paste.
| 1 | /*Dado un número positivo (validado), almacenar todos sus divisores en una pila y determinar si el número | |
| 2 | es perfecto haciendo uso de los valores guardados. Para la resolución del ejercicio se deben utilizar las | |
| 3 | versiones básica o genérica de Stack propuestas en teoría. | |
| 4 | ||
| 5 | Definición: Un número es perfecto cuando la suma de sus divisores excepto el mismo número es igual al | |
| 6 | número en cuestión. | |
| 7 | ||
| 8 | Modifique el programa escrito de manera que permita optar entre el ingreso de valores por consola o por | |
| 9 | un generador de valores aleatorios. | |
| 10 | ||
| 11 | Indicaciones : | |
| 12 | Este ejercicio necesita del objeto scanner para ingresar datos por la consola o teclado, se espera que el | |
| 13 | código controle los problemas que normalmente ocurren al operar con la consola o teclado. | |
| 14 | Se espera una correcta modularización entre el código que realiza el ingreso y validación de los datos | |
| 15 | respecto del código que hace lo que se solicita en el ejercicio. | |
| 16 | - | package tp2punto3; |
| 16 | + | |
| 17 | generados aleatoriamente.*/ | |
| 18 | ||
| 19 | ||
| 20 | import java.util.Random; | |
| 21 | import java.util.Scanner; | |
| 22 | - | int n ; |
| 22 | + | |
| 23 | public class Principal {
| |
| 24 | ||
| 25 | - | int resp=respuesta(); |
| 25 | + | |
| 26 | ||
| 27 | public static void main(String[] args) {
| |
| 28 | Integer resp=respuesta(); | |
| 29 | ||
| 30 | if (resp==1) {
| |
| 31 | ingresarValor(); | |
| 32 | } | |
| 33 | else {
| |
| 34 | - | public static int respuesta() {
|
| 34 | + | |
| 35 | - | int resp=0; |
| 35 | + | |
| 36 | leer.close(); | |
| 37 | } | |
| 38 | - | try{System.out.println("1. Ingresar número positivo.\n2. Generar positivo aleatorio.");
|
| 38 | + | |
| 39 | public static Integer respuesta() {
| |
| 40 | Integer resp=0; | |
| 41 | do {
| |
| 42 | do{
| |
| 43 | try{
| |
| 44 | System.out.println("1. Ingresar número positivo.\n2. Generar positivo aleatorio.");
| |
| 45 | resp= Integer.parseInt(leer.nextLine()); | |
| 46 | ||
| 47 | break; | |
| 48 | }catch(Exception exception) {
| |
| 49 | - | int numero=0; |
| 49 | + | |
| 50 | }while(true); | |
| 51 | } while ((resp!=1) & (resp!=2)); | |
| 52 | - | try{System.out.println("Ingrese un número positivo: ");
|
| 52 | + | |
| 53 | - | numero = Integer.parseInt(leer.nextLine()); |
| 53 | + | |
| 54 | - | break; |
| 54 | + | |
| 55 | public static void ingresarValor() {
| |
| 56 | Integer numero=0; | |
| 57 | do{
| |
| 58 | do{
| |
| 59 | try{
| |
| 60 | System.out.println("Ingrese un número positivo: ");
| |
| 61 | ||
| 62 | numero = Integer.parseInt(leer.nextLine()); | |
| 63 | - | }}while (numero<1); |
| 63 | + | break; |
| 64 | }catch(Exception exception) {
| |
| 65 | System.err.println("Caracter inválido, purebe otra vez.");}
| |
| 66 | }while(true); | |
| 67 | ||
| 68 | if (validarNum(numero)==false) {
| |
| 69 | - | divisores(1 + r.nextInt(500)); |
| 69 | + | |
| 70 | } | |
| 71 | else {
| |
| 72 | divisores(numero); | |
| 73 | }}while (numero<1); | |
| 74 | } | |
| 75 | ||
| 76 | public static void valorAleatorio() {
| |
| 77 | System.out.println("El valor es generado al azar.");
| |
| 78 | Random r = new Random(); | |
| 79 | divisores(1 + r.nextInt(100)); | |
| 80 | } | |
| 81 | ||
| 82 | public static boolean validarNum(int n) {
| |
| 83 | boolean positivo; | |
| 84 | if (n<1) {
| |
| 85 | positivo= false; | |
| 86 | }else {
| |
| 87 | positivo=true; | |
| 88 | } | |
| 89 | return positivo; | |
| 90 | } | |
| 91 | ||
| 92 | public static void divisores(int num) {
| |
| 93 | Pila pila = new Pila(); | |
| 94 | boolean finished=false; | |
| 95 | int divisor=1; | |
| 96 | int suma=0; | |
| 97 | while(!finished) {
| |
| 98 | if(num%divisor==0) {
| |
| 99 | - | while(!pila.estaVacia()) {
|
| 99 | + | |
| 100 | suma =suma+divisor; | |
| 101 | divisor +=1; | |
| 102 | }else {
| |
| 103 | divisor +=1; | |
| 104 | } | |
| 105 | if (divisor>num) | |
| 106 | finished=true; | |
| 107 | } | |
| 108 | System.out.println("Los divisores de " + num + " son: ");
| |
| 109 | while(!pila.empty()) {
| |
| 110 | System.out.println(pila.pop()); | |
| 111 | } | |
| 112 | numPerfecto(suma,num); | |
| 113 | } | |
| 114 | ||
| 115 | public static void numPerfecto(int s,int n) {
| |
| 116 | s=s-n; | |
| 117 | if (s==n) {
| |
| 118 | System.out.println("Además " + n + " es un número perfecto.");
| |
| 119 | } | |
| 120 | else {System.out.println("Además " + n + " no es un número perfecto.");
| |
| 121 | } | |
| 122 | } | |
| 123 | ||
| 124 | } |