0

I have an assignment to be done in college, a piece asks for the instructions below to be done. Please don't give away the answer, simply leading me in the right direction would be awesome.

All the data is transmitted as four-digit integers. You program should read a four-digit integer entered by the user and encrypt it as follows: - Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then, - Swap the first digit with the third, and the second with the fourth.

i can do everything but swapping the digits at the end of the instructions.

All help greatly appreciated, thanks guys!

4
  • 1
    What have you done and what do you need help with? We can't do the entire thing for you. Commented Nov 1, 2011 at 0:34
  • 1
    thats why i said just give me hints :P... it's a huge assignment and one method of the 15 i am writing consists of this stuff... Commented Nov 1, 2011 at 0:35
  • 1
    What do you have so far? With nothing, I recommend the one and only: Java Hello World Commented Nov 1, 2011 at 0:35
  • 2
    you will want to use / and % operators. Commented Nov 1, 2011 at 0:35

3 Answers 3

2

First break down the program into logical steps:

  1. Get input from the user
  2. Check that the input is a 4-digit number
  3. Split up the number into individual digits
  4. Perform the remainder calculation on each digit
  5. Reassemble the 4-digit number, swapping digits as required
  6. Print the output.

I imagine you can do at least some of that, so let us know what you've done so far.

Sign up to request clarification or add additional context in comments.

2 Comments

i can do everything but swapping digit spots
That's great! Do it without swapping digits, show your code, and we'll be happy to help with that part.
1

First of all, you need to get every digit in the number and store in separate variables (in an array or a list).

Let's say

number = 1763

Your array would look like this:

list[0] = 3
list[1] = 6
list[2] = 7
list[3] = 1

Then for each member of the list, do this: list[i] = (list[i] + 7) % 10; Then swap the elements in the list as directed. Write a swap function so that you can reuse it.

swap(int[] array, int i, int j) { 
// Check for array boundary violation
// Swap the elements of the array identified by indexes i and j
int tmp = array[i]; array[i] = array[j]; array[j] = tmp;
}

Then construct your number out of the array. Note that array indexes will be the power of 10 when summing up the figures.

This solution will easily scale to n-digit integers.

Comments

0

The first step you will want to do is to split the 4 digit integer into its individual digits. To do this, you need to understand base 10 representation of numbers.

Once you understand how to split the number into the individual digits, the rest of the operations should be fairly easy to do.

4 Comments

i know how to do that... it would be using division and modulus operands correct? and the part i need help with is actually shifting the order of the numbers from 1 <--> 3 and 2 <--> 4
@Theo Lopez de Castilla - Then fix your question to say that. Right now you're asking for the entire assignment.
That is correct. You will need a temporary variable to do the swapping. Lets say you have i1 and i3. To swap you need to have another variable temp and do : temp = i1; i1 = i3; i3 = temp;
Brendan, read mate, i simply asked for hints and pointers... i didn't say DO THE ASSIGNMENT. and thanks for the help Nick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.