Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

As you have seen in the last homework and lab, it is impossible to implement a s

ID: 3795327 • Letter: A

Question

As you have seen in the last homework and lab, it is impossible to implement a swap method that swaps two arguments of any type by using the common solution involving three assignments. But for NaturalNumber it is possible to implement swap by employing either NaturalNumber's copyFrom or Standard's transferFrom.

Given this contract for the method swapNN:

1

2

3

4

5

6

7

8

9

10

11

12

/**

* Swaps the two given {@code NaturalNumber}s.

*

* @param n1

*            the first {@code NaturalNumber}

* @param n2

*            the second {@code NaturalNumber}

* @updates n1

* @updates n2

* @ensures n1 = #n2 and n2 = #n1

*/

private static void swapNN(NaturalNumber n1, NaturalNumber n2) {...}

Implement the swapNN method using copyFrom (and not transferFrom).

Provide a second implementation for the swapNN method specified above using transferFrom (and not copyFrom).

(This question is not related to swapping, but it is a good one nonetheless.) Using any of the methods of the NaturalNumber component family, implement the method specified as follows:

1

2

3

4

5

6

7

8

9

/**

* Squares a given {@code NaturalNumber}.

*

* @param n

*            the number to square

* @updates n

* @ensures n = #n * #n

*/

private static void square(NaturalNumber n) {...}

import components.naturalnumber.NaturalNumber;

import components.naturalnumber.NaturalNumber2;

http://web.cse.ohio-state.edu/software/common/doc/components/naturalnumber/NaturalNumber.html

1

2

3

4

5

6

7

8

9

10

11

12

/**

* Swaps the two given {@code NaturalNumber}s.

*

* @param n1

*            the first {@code NaturalNumber}

* @param n2

*            the second {@code NaturalNumber}

* @updates n1

* @updates n2

* @ensures n1 = #n2 and n2 = #n1

*/

private static void swapNN(NaturalNumber n1, NaturalNumber n2) {...}

Explanation / Answer

import components.naturalnumber.NaturalNumber;

public class Main {
/**
* Swaps the two given {@code NaturalNumber}s.
*
* @param n1
* the first {@code NaturalNumber}
* @param n2
* the second {@code NaturalNumber}
* @updates n1
* @updates n2
* @ensures n1 = #n2 and n2 = #n1
*/
private static void swapNN(NaturalNumber n1, NaturalNumber n2) {
NaturalNumber temp = n1;
n2.copyFrom(n1);
n1.copyFrom(temp);
}

/**
* Swaps the two given {@code NaturalNumber}s.
*
* @param n1
* the first {@code NaturalNumber}
* @param n2
* the second {@code NaturalNumber}
* @updates n1
* @updates n2
* @ensures n1 = #n2 and n2 = #n1
*/
private static void swapNN2(NaturalNumber n1, NaturalNumber n2) {
NaturalNumber temp = n1;
n2.transferFrom(n1);
n1.transferFrom(temp);
}

/**
* Squares a given {@code NaturalNumber}.
*
* @param n
* the number to square
* @updates n
* @ensures n = #n * #n
*/
private static void square(NaturalNumber n) {
n.multiply(n);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote