hi chaps, I would like to do this exercise taken from the deitel and deitel book, chap 4 exercise 4.38, in brief: "Write an application that reads a 4 digit int and encrypt it replacing each digit with the result of addin 7 to it and getting the reminder after dividing the new value by 10. Then swap the first digit with the 3rd and swap the second with the fourt, then print the encrypted digit".
Now bearing in mind that I am only allowed to use a while statement (no for loops, no array, no switch and assuming the user enters numbers and not letters) becaue I haven't done them as yet, here's my pseudocode, and I would appreciate some feedback on it, if I am doing it right or wrong:

declare variables: digit, counter
input 1 digit
increment counter
print the inserted digit (just for testing purposes)
while counter > 0 and <=4
    encrypt the digit
    insert another number
    print the inserted digit (just for testing purposes)
    increment counter
    Print each digit of the password

But I have a few questions:
1)The user will enter a digit one by one rather than a 4 digits int, is that ok?
2)I am thinking to have 2 files, one with a main method and another one with the class, variables and methods, but I wonder, when writing pseudocode, do you write it for both files? Take the example above, I don't say in there how the conversion is done, that a method will be called, so is it worth doing 2 separate pseudocode files or not? SOrry I am bit new to this so I am not entirely sure what's the best way to write pseudocode.
cheers

Recommended Answers

All 12 Replies

not sure if you wanted to leave it out but that pseudocode does not swap digits 1&3 and 2&4.

I would think the user has to put in the 4 digit int as 1 input personally.

as for the pseudocode questions, im not the best to answer this but when i was in college id split my methods into "pseudocode Modules" , which is just basicly giving a name to a paragraph of pseudocode and then reffering to that name in other pseudocode. Im not sure if this is common practice or if theres a better way to write it, but i always got good grades with it ^^ (oh and i wouldnt bother mentioning what pseudocode goes into what file, its a bit early at that stage to think about what goes in which file imo. just separate it when you start actual coding")

hope this helps, let us know how you end up doing it :)

that's right it doesn't swap the digits as yet because that will go into the file that has the class and methods which is not "mapped" in the pseudocode. If you input a 4 digits int, how do you then change each digit? You will have to split the number correct?

theres many ways you could do that.

use toString and charAt then parse back is one.

you could work something with modulos...

like isolate the smallest digit with % 10 , the second with % 100 and stuff.

Or perhaps use bit masks to isolate the digits with & and ^.

it sounds more complicated than inputing 1 digit at a time but i just feel like 4 inputs is annoying to the user, in a console settings that is. In a gui setting then you could have a little num pad taking in input and saving the digits as they come would be transparent to the user.

thanks. ABout the first way with toString, that I suppose means to turn the int into a string correct? But then if I do that and pick up each digit with charAt() I will have to change it back to an int when I divide/add the digits and then back again to string to apply the swap...?

You can convert a char to a number using Character.digit(char ch, int radix). Considering the restrictions you are given, that seems to be what they want you to do, but don't use toString. It would be awkward to read the int in as a String, convert it to an int, then convert it back to a String, then convert each char of the String into an int; don't do that. If you are going to use charAt then do it directly on the input String.

For your output you can just use a StringBuilder with setLength(4) and setCharAt for each digit.

ok so sorry bit confused now, to summarize: I will read a 4 digits int, then what? COnvert each digit to char do all the calculations and back to in usingCharacter.digit(char ch, int radix)
thanks

It all depends on how you read your number: if you read it in as int, use the modulo 10 divide by 10 approach. On the other hand if you read your number as a String you can either loop through all characters in the String and convert each character to a digit, or you parse the number into an int and use the modulo 10 divide by 10 approach on the parsed result.

Suppose you read it in as an int, then here's an example on how to extract the digits.
All divisions are integer divisions.

Input: 12345

12345 % 10 = 5
12345 / 10 = 1234
1234 % 10 = 4
1234 / 10 = 123
123 % 10 = 3
123 / 10 = 12
12 % 10 = 2
12 / 10 = 1
1 % 10 = 1
1 / 10 = 0 <-- Here's where we stop

commented: this is also what i would choose to do for this +7

ok this is really interesting, thanks. The only thing though with this approach is that I have to save each digit in a different int variable so that I can then process each single digit

I recommend against making a variable for each digit like d1, d2, d3, d4. That can't be what they intend you to do because that is the sort of thing that people who don't understand arrays do. It's like an array but much more awkward, and since they don't even want you to use an array, they can't want that.

It would be better to have a method that extracts the digit you want from the number, like this:

private static int extractDigit(int index, int number) {
    while(index > 0) {
        number /= 10;
        index--;
    }
    return number % 10;
}

It counts digits from right-to-left instead of left-to-right, so the 4th digit would be digit-0 and the first digit would be digit-3, which is unfortunate, and it uses up your one while loop, but even so that would be my choice. I would prefer to use my one while loop this way rather than iterating over the digits, and it is much better to use extractDigit(3,number) than d1. If they wanted you to iterate over the digits of the number they would have allowed you to use a for-loop and they wouldn't have given you special rules for each digit.

I could have started with something like index = 4 - index so that extractDigit(1,number) would extract the first digit, but doing that would make extractDigit useless for longer numbers and even though the problem doesn't ask for any longer numbers, maybe someday longer numbers might be needed. A really ideal extractDigit would somehow use a while loop to index into the number from left-to-right starting from the true first digit. Normally I would just use a second while loop to find the length of the number in advance, but since I'm only allowed one it becomes a tricky problem. Perhaps figuring out a way to do that is the true challenge.

With extractDigit you can do something like

answer.setCharAt(1,Character.forDigit(encode(extractDigit(0,number)),10));

That is setting the second digit of the answer by the 4th digit of the input number, assuming there is also an encode method that converts an input digit to an answer digit.

Of course, that is assuming that your input is an int and your output is a String. If your input is a String then you can just use charAt instead of extractDigit and not have any loops at all. If your output is an int, then I would build it up from left-to-right like this:

answer = answer * 10 + encode(extractDigit(1,number)); // First digit from third digit
answer = answer * 10 + encode(extractDigit(0,number)); // Second digit from fourth digit
// etc.

If I were allowed to use an array and a for-loop I would make an array like this:

private static final int[] swaps = {2,3,0,1};

Then I could know where digit i swaps to as easily as swaps[i]. It would make it easier to change how the digits get swapped and allow me to avoid having to treat each digit specially.

commented: Solid advice :) +13

thanks, yesterday I have developed the function in a slight different way, what do you think of this?

    private void encryptNumber(){
        //System.out.printf("Digit inserted in previous method %d\n", number);
        int numberLenght = number;  

        while(numberLenght > 0){            
            number %= 10;           //4 123 u get 3
            numberLenght = number / 10;                     
            }

With this function I need a counter that tells me how many digits are left in the number so that the while loop can run numberLenght
what do you reckon?

Actually no, that's wrong. I have to have something like this

private void encryptNumber(){
        //System.out.printf("Digit inserted in previous method %d\n", number);
        int numberLenght = number;  

        while(numberLenght > 0){
            //int numberOfDigits += 1;
            digit = number % 10;            //4 123 u get 3
            numberLenght = number / 10;

with a digit variable that can hold the digit and keep the number as it is, plus a variable which is equal to the number to keep the loop running

ok, got somewhere, but a little stuck now. So here's what I came up with:
first file Encrypt.java

    public class EncryptionTest{

    public static void main( String[] args){

        Encryption myNumber = new Encryption();
        myNumber.insertNumber();

    }   

    }

second file Encryption.java

//Encryption.java
import java.util.Scanner;

public class Encryption{
    private int number;//digit inserted by the user
    private int numberLenght;//to calculate the length
    //private int numberLeft;
    private int digit;

    public void insertNumber(){
        //System.out.print("digit is " + number + "\n");
        System.out.print("Insert your 4 digit int to convert and press enter: ");
        Scanner input = new Scanner(System.in);
        number = input.nextInt();
        System.out.printf("You have inserted %d\n", number);
        extractNumber();
        }
    private void extractNumber(){
        //System.out.printf("Digit inserted in previous method %d\n", number);
        int numberLenght = number;  

        while(number > 0){
            //int numberOfDigits += 1;
            digit = number % 10;            //4 123 u get 3
            encryptNumber(digit);           
            number /= 10;                       
            }
        } 

        private void encryptNumber(int toEncrypt){
            toEncrypt = ((toEncrypt + 7) % 10);
            String theNumber = String.valueOf(toEncrypt);
            System.out.print(theNumber );
            //return theNumber;
            }
    }

So the problem I have is that I need to change the order of the digits in the number that I have converted into string. The thing is the conversion happens in the encryptNumber()method, so is it better to return the string somewhere and then swap the digits or how? ANy advice please?
thannks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.