How to add 14 based numbers in instance method? - java

So I'm having trouble with creating an instance method to add two 14-based number and I was wondering if anyone could help? I'm a bit new to java and still sort of confused on the whole thing. So far I have the code to convert the 14-based numbers to base 10 then I need to add them and convert them back to base-14. I want to put them all in once instance class, but I feel like it's too much to put into one instance class.
This is the kind of input I was for the client code to be like this:
PokerNum sum = num1.add(num2);
import java.util.Scanner;
public class PokerNum{
String alienNum;
int num1, num2;
public PokerNum(String alienNum) throws IllegalArgumentException{
this.alienNum = alienNum;
String toUpper = alienNum.toUpperCase();
for (int i = 0; i < toUpper.length(); i++){
char c = toUpper.charAt(i);
if (!(Character.isDigit(c) || c == 'A' || c == 'J' || c == 'Q' || c == 'K')){
throw new IllegalArgumentException("invalid input");
}
}
}
// initialized at zero
public PokerNum() {
this.num1=0;
this.num2=0;
}
#Override public String toString(){
return PokerNum()+ " ";
}
//add pokernums
public PokerNum add(PokerNum another){
PokerNum num = new PokerNum();
this.num1 = another.num1;
this.num2 = another.num2;
public PokerNum convert(PokerNum pn){
char[] firstNum = num1.toCharArray();
int amountValue = 0;
int length = characters.length;
for (int index = 0; index < length; index++){
int symbolValue;
switch (characters[index]) {
case 'A':
symbolValue = 10;
break;
case 'J':
symbolValue = 11;
break;
case 'Q':
symbolValue = 12;
break;
case 'K':
symbolValue= 13;
break;
default:
symbolValue = characters[index] - 48;
}
amountValue += symbolValue*Math.pow(14, length - index - 1);
}
StringBuilder result1 = new StringBuilder();
while(amountValue > 0){
int digit = amountValue%14;
switch (digit) {
case 10:
result.insert(0, 'A');
break;
case 11:
result.insert(0, 'J');
break;
case 12:
result.insert(0, 'Q');
break;
case 13:
result.insert(0, 'K');
break;
default:
result.insert(0, digit);
}
amountValue-=digit;
amountValue/=14;
String firstValue = result1.toString();
}
}
char[] secNum = num2.toCharArray();
int amountValue2= 0;
int length = secNum.length;
for (int index = 0; index < length; index++){
int symbolValue;
switch (characters[index]) {
case 'A':
symbolValue = 10;
break;
case 'J':
symbolValue = 11;
break;
case 'Q':
symbolValue = 12;
break;
case 'K':
symbolValue= 13;
break;
default:
symbolValue = characters[index] - 48;
}
amountValue2+= symbolValue*Math.pow(14, length - index - 1);
}
StringBuilder result = new StringBuilder();
while(amountValue2> 0){
int digit = amountValue%14;
switch (digit) {
case 10:
result.insert(0, 'A');
break;
case 11:
result.insert(0, 'J');
break;
case 12:
result.insert(0, 'Q');
break;
case 13:
result.insert(0, 'K');
break;
default:
result.insert(0, digit);
}
amountValue2-=digit;
amountValue2/=14;
PokerNum secondValue = result.toString();
}
return firstValue + secondValue;
/*PokerNum sum = num1 +
PokerNum another.num2 =
return sum;*/
}
Thanks to anyone who helps in advance :)

I believe you can keep it in one instance. One suggestion is to use the conversion using the following functions:
Integer.parseInt(String s, int radix)
Integer.toString(int i, int radix)
The javadoc says that the string produced will use:
0123456789abcdefghijklmnopqrstuvwxyz
So, if we choose radix = 14, it will have 0123456789abcd.
The logic idea is to keep the actual number as int, and to convert at creation and at printout from and to String. We can just keep member variable num1 and remove the member variable alienNum and num2.
Constructor
Your constructor with String argument seems to do a good error checking. What we need here is just to convert from string to integer and store it in the member variable. First we need to convert all valid string from AJQK to ABCD. The example here is inefficient, but gets the idea across:
//alienNum = alienNum.replace('A', 'A'); // no need
alienNum = alienNum.replace('J', 'B');
alienNum = alienNum.replace('Q', 'C');
alienNum = alienNum.replace('K', 'D');
Then we can call the parsing method:
num1 = Integer.parseInt(alienNum , 14);
Your empty arg constructor is already fine by initializing the value of num1 to 0.
Adding
The method inside addition is not right because it is setting the current value to the addition. There are three objects working here: num, this, and another. You want to add this to another into num and return num.
num.num1 = this.num1 + another.num1;
Output
I'm assuming the output is going to be from toString(). In this case, you want to convert from integer to string, then convert it to the right character
String out = Integer.toString(num1, 14).toUpper();
//alienNum = alienNum.replace('A', 'A'); // no need
alienNum = alienNum.replace('B', 'J');
alienNum = alienNum.replace('C', 'Q');
alienNum = alienNum.replace('D', 'k');
You probably won't need a convert method now.

Related

How do I convert number to a letter in Java? [duplicate]

This question already has answers here:
Switch statement with string wrong output
(2 answers)
Closed 2 years ago.
I want to make a program to convert number to letter, from 0-9 to ABCDEFGHIK.
For example:
with n = 10 the output would be BA as 0 is A is 0 and B is 1.
Here is my code:
String convertNumber(long n) {
String result="";
String strN = Long.toString(n);
for (int i=0; i < strN.length();i++){
char ch = strN.charAt(i);
switch(ch){
case '0':
result = "A";
case '1':
result = "B";
case '2':
result = "C";
case '3':
result = "D";
case '4':
result = "E";
case '5':
result = "F";
case '6':
result = "G";
case '7':
result = "H";
case '8':
result = "I";
case '9':
result = "K";
}
}
return result;
}
However, the results returns only K. Where did I do wrong? Thank you!
There are three mistakes in your program:
Not using break with the case and therefore every case will fall to the last case.
Using = instead of +=
Using the loop in reverse order than the required order. It should be for (int i = strN.length() - 1; i >= 0; i--) instead of for (int i=0; i < strN.length();i++)
Given below is your corrected program:
public class Main {
public static void main(String[] args) {
System.out.println(convertNumber(10));
}
static String convertNumber(long n) {
String result = "";
String strN = Long.toString(n);
for (int i = strN.length() - 1; i >= 0; i--) {
char ch = strN.charAt(i);
switch (ch) {
case '0':
result += "A";
break;
case '1':
result += "B";
break;
case '2':
result += "C";
break;
case '3':
result += "D";
break;
case '4':
result += "E";
break;
case '5':
result += "F";
break;
case '6':
result += "G";
break;
case '7':
result += "H";
break;
case '8':
result = "I";
break;
case '9':
result = "K";
}
}
return result;
}
}
Output:
AB
You can use this one:
static String convertNumber(int n) {
int reminder;
char[] arr = "ABCDEFGHIK".toCharArray();
int len = arr.length;
StringBuilder builder = new StringBuilder();
while (n != 0) {
reminder = (int) n % 10;
n /= 10;
builder.append(arr[(reminder % len)]);
}
return builder.toString();
}
, main
static public void main(String[] args) {
System.out.println(convertNumber(65));
System.out.println(convertNumber(78));
System.out.println(convertNumber(99));
System.out.println(convertNumber(901));
}
, output
FG
IH
KK
BAK
You forgot the break. add break; in every case, like this:
case '0':
result = "A";
break;
case '1':
result = "B";
break;

How can I convert a hexadecimal number into binary without using parse or automatic conversion

Below I have a method named 'hextoBinary' that returns a hexadecimal to binary conversion through type void.
In order for me to continue with my program I need a conversion from hex to binary method that returns and int so I can convert that binary int into a decimal with my 'hextoDecimal' method.
Can anybody help me or guide me on what approach to take, i've been stuck on this for a while now. i am limited to doing this manually instead of using parse or java automatic conversions.
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class Main
{
static void hexToBinary(char hexdec[])
{
for (char c: hexdec)
{
switch (c)
{
case '0':
System.out.print("0000");
break;
case '1':
System.out.print("0001");
break;
case '2':
System.out.print("0010");
break;
case '3':
System.out.print("0011");
break;
case '4':
System.out.print("0100");
break;
case '5':
System.out.print("0101");
break;
case '6':
System.out.print("0110");
break;
case '7':
System.out.print("0111");
break;
case '8':
System.out.print("1000");
break;
case '9':
System.out.print("1001");
break;
case 'A':
System.out.print("1010");
break;
case 'B':
System.out.print("1011");
break;
case 'C':
System.out.print("1100");
break;
case 'D':
System.out.print("1101");
break;
case 'E':
System.out.print("1110");
break;
case 'F':
System.out.print("1111");
break;
default:
System.out.print("\nInvalid hexadecimal digit " + hexdec[c]);
}
}
}
public static int hextoDecimal(int n)
{
int decimal = 0, p = 0;
while(n != 0)
{
decimal += ((n % 10) * Math.pow(2,p));
n = n / 10;
p++;
}
return decimal;
}
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(new File("RAMerrors8x4c"));
ArrayList<String> hexValues = new ArrayList<>();
while(sc.hasNext())
{
hexValues.add(sc.nextLine());
}
hexToBinary(hexValues.get(0).toCharArray());
}
}
This code is based on some that came from here but that link no longer seems to be active. Anyway, from a hex string you can get an int like this:
int hexToDecimal(String s){
int result = 0;
int digit = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= '0' && c <= '9')
digit = c - '0';
else
if (c >= 'A' && c <= 'F')
digit = 10 + c - 'A';
else
inputError(s);
result = 16 * result + digit;
}
return result
}
I modified your code a little.
a. In your code only the first hex was printed.
Change:
call hexToBinary for every hex String.
b. the binary value was discarded after printing, so it couldn't be reused.
Change:
Changed returntype of hexToBinary from void to String and returned the binary value calculated.
To be able to return a String I add the peaces(nibbles) of the hex/binary to a String in every switch(case) clause.(a Stringbuilder might be better than a String - you can additionally improve that)
in the main: additionally collect all the returned binary values in a arraylist called "binaryValues" in order to have them for the next step.
With the above (little) changes I now have all the binary values that had already been calculated.
So I am able to simply use them in a binaryToDecimal method which just sums up the binary values weighted by their position.
Why not do it again? Because youd need to convert the A-F to numbers what your hexToBinary already did. So storing the values saves you doing that step again. I have a feeling that is what your teacher had in mind when he/she combined the tasks like this.
The resulting code is:
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class Main
{
static String hexToBinary(char hexdec[]) {
String hex = "";
for (char c : hexdec) {
switch (c) {
case '0':
System.out.print("0000");
hex += "0000";
break;
case '1':
System.out.print("0001");
hex += "0001";
break;
case '2':
System.out.print("0010");
hex += "0010";
break;
case '3':
System.out.print("0011");
hex += "0011";
break;
case '4':
System.out.print("0100");
hex += "0100";
break;
case '5':
System.out.print("0101");
hex += "0101";
break;
case '6':
System.out.print("0110");
hex += "0110";
break;
case '7':
System.out.print("0111");
hex += "0111";
break;
case '8':
System.out.print("1000");
hex += "1000";
break;
case '9':
System.out.print("1001");
hex += "1001";
break;
case 'A':
System.out.print("1010");
hex += "1110";
break;
case 'B':
System.out.print("1011");
hex += "1111";
break;
case 'C':
System.out.print("1100");
hex += "1100";
break;
case 'D':
System.out.print("1101");
hex += "1110";
break;
case 'E':
System.out.print("1110");
hex += "1110";
break;
case 'F':
hex += "1111";
System.out.print("1111");
break;
default:
System.out.print("\nInvalid hexadecimal digit " + hexdec[c]);
}
}
System.out.println();
return hex;
}
public static int binaryToDecimal(String binary) {
int decimal = 0;
for (int i = 1; i < binary.length()-1; i++) {
decimal += Math.pow(2, i-1) * (binary.charAt(binary.length()-i) - '0');
}
return decimal;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("RAMerrors8x4c"));
ArrayList<String> hexValues = new ArrayList<>();
ArrayList<String> binaryValues = new ArrayList<>();
while (sc.hasNext()) {
hexValues.add(sc.nextLine());
}
for (String hex : hexValues) {
String binary = hexToBinary(hex.toCharArray());
binaryValues.add(binary);
System.out.println(binary);
}
for (String binary : binaryValues) {
int decimal = binaryToDecimal(binary);
System.out.println(decimal);
}
}
}
}
Besides using a Stringbuilder another idea could be to do all the printing of the binary values in the main. The hexToBinary returns the String - so you can print it in the loop - if you want.

Ways to reverse print a string?

I'm trying to code a program that would convert an int to binaries. So far I have it print out the remainders, but it has to be printed in reverse for it to be a proper binary. I am not allowed to use any methods.
Here's my code.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter A Postive Number:");
int i = sc.nextInt();
int j = i; //backup
int k = 0; //remainder
while (j != 0) {
k = j % 2;
j /= 2;
String reversedStr = "";
switch (k) {
case 0:
reversedStr += "0";
break;
case 1:
reversedStr += "1";
break;
case 2:
reversedStr += "2";
break;
case 3:
reversedStr += "3";
break;
case 4:
reversedStr += "4";
break;
case 5:
reversedStr += "5";
break;
case 6:
reversedStr += "6";
break;
case 7:
reversedStr += "7";
break;
case 8:
reversedStr += "8";
break;
case 9:
reversedStr += "9";
break;
case 10:
reversedStr += "A";
break;
case 11:
reversedStr += "B";
break;
case 12:
reversedStr += "C";
break;
case 13:
reversedStr += "D";
break;
case 14:
reversedStr += "E";
break;
case 15:
reversedStr += "F";
break;
}
for (int l = reversedStr.length() - 1; l >= 0; l--) {
reversedStr.charAt(i);
System.out.print(reversedStr.charAt(l));
}
}
System.out.println("done");
}
}
this code gives me
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 178
at java.lang.String.charAt(String.java:658)
at radixHandmade.Handmade.main(Handmade.java:73)
The for loop at the bottom is the reversing part, but im not sure how to use charAt to complete the code. To be honest im very confused.
Any help would be greatly appreciated.
This statement causes the problem:
reversedStr.charAt(i);
Aside from causing an exception (because you're trying to get the (originally entered value)-th character in the string, which is by outside the bounds of the string, unless that value happens to be 0), it has no other side effect.
Just remove this line.
import java.util.Scanner;
public class Reverse {
public static void main(String[] args){
userInput();
}
public static void userInput(){
Scanner console = new Scanner(System.in);
String reverseString = console.nextLine(); //This is the word to be written backwards.
int s = reverseString.length()-1; //length of the word.
for (int i = s ; i>=0; i--){
System.out.print(reverseString.charAt(i));
}
}
}
Hope this helps.
Good luck.
removing the line reversedStr.charAt(i) will resolve the issue, but the program will still not convert to binary correctly. Your string still needs to be reversed. Might I suggest using the StringBuilder class:
import java.util.Scanner;
import java.lang.StringBuilder;
public class App {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder out = new StringBuilder();
System.out.println("Enter A Postive Number:");
int i = sc.nextInt();
int r = 0;
int b = 2; // now you can just change this for what base (binary, hex, ...)
while (i != 0) {
r = i % b;
i /= b;
switch (r) {
case 0:
out.append("0");
break;
case 1:
out.append("1");
break;
}
}
System.out.println(out.reverse());
}
}
Your code would work fine if you move the reversing for loop out of the while loop:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String revStr = "";
System.out.println("Enter A Postive Number:");
int i = sc.nextInt();
int r = 0;
int b = 2; // now you can just change this for what base (binary, hex, ...)
while (i != 0) {
r = i % b;
i /= b;
switch (r) {
case 0:
revStr += "0";
break;
case 1:
revStr += "1";
break;
// ... for hex
}
}
for (int j = revStr.length() - 1; j >= 0; j--) {
System.out.print(revStr.charAt(j));
}
System.out.print("\n");
}
But what I really don't understand is why you even have to reverse the string if you can just do:
reversedStr = "0" + reversedStr; // of course it will no longer be reversed
// ... for all the cases
Even better if you stick with just binary, you could skip the whole switch and do
public static void main(String[] args) {
String str = "";
System.out.println("Enter A Postive Number:");
for(int i = new Scanner(System.in).nextInt(); i != 0; i /= 2)
str = String.valueOf(i % 2) + str;
System.out.println(str);
}

Java: Lighter code for encryption

I'm trying to get a program to encrypt a message using Cesar's Cypher, which involves replacing every character of a string with the letter, whose code is the code of the replaced letter plus 3. For e.g., if the letter is A, then it has to be replaced with D, because the code of D is the code of A plus 3. The letters are case-sensitive. The code I thought of uses a really heavy switch construct, I was wondering, if you could help me to make it more straight forward.
Here's the code I use for the encryption method:
public class Util
{
public static String Encript(String stringa)
{
char[] stringaArray=stringa.toCharArray();
for (int i =0; i<stringaArray.length; i++)
{
switch (stringaArray[i])
{
case 'a':
stringaArray[i]=('D');
break;
case 'b':
stringaArray[i]='E';
break;
case 'c':
stringaArray[i]='F';
case 'd':
stringaArray[i]='G';
break;
case 'e':
stringaArray[i]='H';
break;
case 'f':
stringaArray[i]='I';
break;
case 'g':
stringaArray[i]='J';
break;
case 'h':
stringaArray[i]='K';
break;
case 'i':
stringaArray[i]='L';
break;
case 'j':
stringaArray[i]='M';
break;
case 'k':
stringaArray[i]='N';
break;
case 'l':
stringaArray[i]='O';
break;
case 'm':
stringaArray[i]='P';
break;
case 'n':
stringaArray[i]='Q';
break;
case 'o':
stringaArray[i]='R';
break;
case 'p':
stringaArray[i]='S';
break;
case 'q':
stringaArray[i]='T';
break;
case 'r':
stringaArray[i]='U';
break;
case 's':
stringaArray[i]='V';
break;
case 't':
stringaArray[i]='W';
break;
case 'u':
stringaArray[i]='X';
break;
case 'v':
stringaArray[i]='Y';
break;
case 'w':
stringaArray[i]='Z';
break;
case 'x':
stringaArray[i]='A';
break;
case 'y':
stringaArray[i]='B';
break;
case 'z':
stringaArray[i]='C';
break;
}
}
String encripted= new String(stringaArray);
return encripted;
}
}
Then I use this method in the graphical interface class so that it acts when a button is pressed like this:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
String stringa=messageTxt.getText();
encriptedTxt.setText(Util.Encript(stringa, encriptedTxt));
}
Here is an example of test-case:
Test case:
aBxyE //Input
dEabH //Output
Thank you in advance!
Khoor/#Zruog
You could iterate the characters in the String, and I would also pass in the key offset. Create a StringBuilder and append each character after performing integer addition and casting. Something like,
public static String encrypt(String msg, int key) {
StringBuilder sb = new StringBuilder();
for (char ch : msg.toCharArray()) {
char c = (char) (ch + key);
sb.append(c);
}
return sb.toString();
}
Decryption is trivial with a Caesar cipher; you can call encrypt with the negative value of the key
public static String decrypt(String msg, int key) {
return encrypt(msg, -key);
}
And I tested my example with
public static void main(String[] args) {
String msg = encrypt("Hello, World", 3);
System.out.println(msg);
System.out.println(decrypt(msg, 3));
}
Finally, as others have noted, the Caesar cipher is terribly insecure (because letter frequency is not perturbed, it's trivial in a modern sense).
This code will sawp a char with another one three chars apart, as defined in ALPHABET :
public class Util
{
private final static String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public static String encript(String stringa)
{
char[] stringaArray = stringa.toCharArray();
for (int i =0; i<stringaArray.length; i++) {
char c = stringaArray[i];
int index = ALPHABET.indexOf(c);
if(index <0)
{
continue ; //if c does not appear in ALPHABET
}
// for example c is *, leave it unchanged
if((index +3) >= ALPHABET.length() ) {
index = index - ALPHABET.length();
}
stringaArray[i] = ALPHABET.charAt(index+3);
}
String encripted= new String(stringaArray);
return encripted;
}
}
If it is not clear, do not hesitate to ask.
Here is my Short and Efficient code for your program:
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//N is length of string
int N = Integer.parseInt(br.readLine());
//Reading string Input
String str = br.readLine();
//K is the key to rotate characters in the string
//In your case K = 3
int K = Integer.parseInt(br.readLine());
K %= 26; //Incase K is greater than 25
//Main [tag:algorithm]
for(int i = 0; i < N; i++){
char c = str.charAt(i);
if(c >= 65 && c <= 90){
c += K;
if(c > 90){
c = (char)(c - 90 + 64);
}
}
if(c >= 97 && c <= 122){
c += K;
if(c > 122){
c = (char)(c - 122 + 96);
}
}
System.out.print(c);
}
}
}
I am doing k = k % 26 because if the k = 26 then it will print the same letter, if k = 27 the character will rotate only 1 time and so on.

Converting int to hex string using recursion

I'm doing an assignment where I have to convert an integer number to a hexadecimal string using recursion. I have to do this using a method that returns a string value. I tried to turn the method I'm using on paper into code and it works just fine:
public class ItH {
private static String intToHex(int n) {
int temp = n % 16;
String digit = "";
if (temp < 10) digit += temp;
if (temp > 9) {
switch (temp) {
case 10: digit="A"; break;
case 11: digit="B"; break;
case 12: digit="C"; break;
case 13: digit="D"; break;
case 14: digit="E"; break;
case 15: digit="F"; break;
}
}
if (n > 0) return intToHex((n-temp)/16) + digit;
return "";
}
public static void main(String[] args) {
System.out.println(intToHex(1234));
System.out.println(intToHex(257));
System.out.println(intToHex(0));
}
}
It works just fine for the first two examples used in the main method, but I can't get it to display zero. Is there a way to get it to display zero without modifying too much? I've been going over it for some time now and I can't figure out a way to do that without adding at least one zero before every other number.
Any help would be appreciated.
How about this:
private static String intToHex(int n) {
int temp = n % 16;
String digit = "";
if (temp < 10) digit += temp;
if (temp > 9) {
switch (temp) {
case 10: digit="A"; break;
case 11: digit="B"; break;
case 12: digit="C"; break;
case 13: digit="D"; break;
case 14: digit="E"; break;
case 15: digit="F"; break;
}
}
if ( (n-temp)/16 == 0 ) return digit;
if (n > 0) return intToHex((n-temp)/16) + digit;
return "";
}
public static void main(String[] args) {
System.out.println(intToHex(1234));
System.out.println(intToHex(257));
System.out.println(intToHex(0));
}
Just add in that extra if statement,
if ( (n-temp)/16 == 0 ) return digit;

Categories

Resources