Commit 2afc8a53 authored by Ricardo Frank Sendes's avatar Ricardo Frank Sendes
Browse files

initial commit

parents
public class CaesarDecoder {
String encodedText;
String[] decodedTexts;
boolean isSolved;
public CaesarDecoder(String encodedText){
this.encodedText = encodedText;
decodedTexts = new String[26];
isSolved = false;
}
public String[] getDecodedTexts(){
if(!isSolved){
this.solveAllKeys();
}
return decodedTexts;
}
private String solve(int key){
StringBuilder resultText = new StringBuilder(encodedText.length());
for(int digit = 0; digit < encodedText.length(); digit++){
int letter = encodedText.codePointAt(digit) - 65; // reset ascii value, so A=0, B=1 etc.
if(letter >= 0 && letter <= 57){ // letter is either A-Z or a-z
if(letter <= 25){ // upper case letter
letter = ((letter + key) % 26); // add key circulative
}
else if(letter >= 32){ // lower case letter
letter = letter - 32; // reset value so a=0, b=1, ...
letter = ((letter + key) % 26) +32; // add key circulative
}
}
letter = letter + 65; // add ascii offset back
resultText.append((char) letter);
}
return resultText.toString();
}
private void solveAllKeys(){
for(int i=0; i<26; i++){
decodedTexts[i] = solve(i);
}
this.isSolved = true;
}
}
import java.util.Date;
public class Clock {
Date currDate;
public Clock(){
currDate = new java.util.Date();
}
private void updateDate(){
currDate = new java.util.Date();
}
public Date getCurrDate(){
this.updateDate();
return currDate;
}
public String getDateAsString(){
return "Jetzt: " + this.getCurrDate().toString();
}
}
import java.util.ArrayList;
public class Primecalculator {
int max;
boolean isCalculated;
ArrayList<Integer> primes;
public Primecalculator(int max){
if(max <2){
throw new ArithmeticException("Cannot calculate primes less than two");
}
this.max = max;
this.isCalculated = false;
primes = new ArrayList<>();
for(int i=2; i<=max; i++){
primes.add(i);
}
}
public int getMax(){
return this.max;
}
public ArrayList<Integer> getPrimes(){
if(!isCalculated){
this.calculate();
}
return primes;
}
private void calculate(){
for(int i=2; i<= max/2;i++){
for(int j=i+i; j<=max;j=j+i){
if(primes.contains(j))
primes.remove((Object) j);
}
}
this.isCalculated = true;
}
}
import java.util.ArrayList;
public class app {
public static void main(String[] args){
//executeClock();
//executePrimecalculator(500);
executeCaesarDecoder("Ebowifzebk\tDirbzhtrkpze!\tPfb\texybk\tafb\tSbopzeirbppbirkd\tdbhkxzhq\trka\tybhljjbk\tyxia\t\n" +
"bfk\tGly-Xkdbylq\tslj\txjbofhxkfpzebk\tDbebfjafbkpq.");
}
private static void executeClock(){
Clock clock = new Clock();
String result = clock.getDateAsString();
System.out.println(result);
}
private static void executePrimecalculator(int max){
Primecalculator calc = new Primecalculator(max);
ArrayList<Integer> primes = calc.getPrimes();
for (Object prime : primes) {
System.out.print(prime + " ");
}
System.out.println("\nThere are " + primes.size() + " prime Numbers from 1 to " + calc.getMax());
}
private static void executeCaesarDecoder(String enc){
CaesarDecoder decoder = new CaesarDecoder(enc);
String[] result = decoder.getDecodedTexts();
for (String text : result) {
System.out.println(text);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment