Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Entregable;
- import Entregable.exception.TooHighNumberException;
- /**
- *
- * @author javie
- */
- public class cifrado {
- public static char cifrar(char letra, int des, boolean cfr) throws TooHighNumberException {
- if (des > 26 || des < 0) {
- TooHighNumberException e = new TooHighNumberException();
- throw e;
- }
- if (cfr) {
- des = des * -1;
- }
- boolean Smi = false;
- boolean Sma = false;
- int car;
- int cif;
- char result = 0;
- String juegocaracteresMinuscula = "abcdefghijklmnopqrstuvwxyz";
- String juegocaracteresMayuscula = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- if (juegocaracteresMinuscula.indexOf(letra) > -1) {
- Smi = true;
- }
- if (juegocaracteresMayuscula.indexOf(letra) > -1) {
- Sma = true;
- }
- if (Smi || Sma) {
- if (Smi) {
- car = juegocaracteresMinuscula.indexOf(letra);
- } else {
- car = juegocaracteresMayuscula.indexOf(letra);
- }
- cif = car + des;
- if (cif > 25) {
- cif = des - (26 - car);
- } else if (cif < 0) {
- des = des * -1;
- cif = 26 - (des - car);
- }
- if (Smi) {
- result = juegocaracteresMinuscula.charAt(cif);
- } else {
- result = juegocaracteresMayuscula.charAt(cif);
- }
- } else {
- result = letra;
- }
- return result;
- }
- public static boolean letra(String letra) {
- boolean ca = false;
- String juegocaracteres = "abcdefghijklmnopqrstuvwxyz";
- for (int i = 0; i < juegocaracteres.length(); i++) {
- if (juegocaracteres.contains(letra)) {
- ca = true;
- }
- }
- return ca;
- }
- public static int desplazamiento(String letraE) {
- int des;
- String juegocaracteres = "abcdefghijklmnopqrstuvwxyz";
- int posE = 4;
- int posLetraE = juegocaracteres.indexOf(letraE);
- if (posLetraE < posE) {
- des = posLetraE + 20;
- } else {
- des = posLetraE - posE;
- }
- return des;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment