-1

First off all sorry for my English.

I have to do practice for class with Java. I'm completely beginner level at Java.

I'm trying to do Caesar Cipher. My Caesar cipher must override an abstract Cipher class.

This is my abstract Cipher class.

abstract class Cipher {  
    abstract String encode(String input, String keyword );
    abstract String decode(String input, String keyword );
}

And this is my Caesar cipher.

public class Ceaser extends Cipher {

    String encode(String input, String keyword) {
        String encodedMessage = "";
        for (char e:input.toCharArray()) {
            if (e+keyword > 122) {
                e = (char) ('A' + (e+keyword-123));
            } else {
                e+=keyword;
            }
            encodedMessage+=e;
        }
        return encodedMessage;
    }

    String decode(String input, String keyword) {
        String decodedMessage = "";
        for (char e:input.toCharArray()) {
            if (e-keyword<65) {
                int a = e-keyword;
                int b = 65-a;
                e = (char) ('z' - b+1);
            } else {
                e-=keyword;
            }
            decodedMessage+=e;
        }
        return decodedMessage;
    }
}

In the Caesar cipher, the keyword variable must be int. But I have to override the abstract class which is String. I'm thinking about using length of string as a integer. But I couldn't do it. How can I do?

Abra
  • 19,142
  • 7
  • 29
  • 41

1 Answers1

0

You can pass number as a string in the method like this,

decode("text", "200")

And inside the method you can convert this string into an integer like this,

int i =  Integer.parseInt("200")

It will return 200 as an integer.