The Student Room Group

Eclipse (JUnit4) Kata programming help

Hey guys can someone please help with this question. If possible can someone code and briefly explain what you have done. I have previously done anagram katas but never this one:frown:. I have attempted a few lines of coding (in bold) but still nothing. Thanks a lot.

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
class _KataAssessment {

* Verify whether a given list of Integers contains only square numbers.
*
* For example, if the argument is the following:
*
* [ 1, 4, 9, 16, 25 ]
*
* the method should return <code>true</code>. If the argument is the
* following:
*
* [ 2, 3, 4, 6, 8, 19 ]
*
* the method should return <code>false</code>.
*
*
* @param numbers
* a list of possible square numbers
*
* @return <code>true</code> if all the lists in the argument contain
* anagrams and <code>false</code> otherwise
*/
public static boolean verifySquares(List<Integer> numbers) {
// COMPLETE THIS METHOD
Object verifySquares;
if(verifySquares == numbers );
return true;

return false;
}
}
public class KataAssessment {
private static final List<Integer> EMPTY = new LinkedList<Integer>();
private static final List<Integer> SQUARES = Arrays.asList(1, 4, 9, 16, 25,
36, 64, 81);
private static final List<Integer> NOT_SQUARES = Arrays.asList(1, 2, 3, 5,
6, 7, 8, 10);
private static final List<Integer> MIXED = Arrays.asList(1, 2, 3, 4, 5, 6,
7, 8, 9);
private static final List<Integer> NEGATIVE = Arrays.asList(0, *1, *2, *3,
*4, *5, *6, *7, *8, *9);
@Test
public void testSquares() {
Assert.assertTrue(_KataAssessment.verifySquares(SQUARES));
}
@Test
public void testEmpty() {
Assert.assertFalse(_KataAssessment.verifySquares(EMPTY));
}
@Test
public void testNonSquares() {
Assert.assertFalse(_KataAssessment.verifySquares(NOT_SQUARES));
}
@Test
public void testMixed() {
Assert.assertFalse(_KataAssessment.verifySquares(MIXED));
}
@Test
public void testNegatives() {
Assert.assertFalse(_KataAssessment.verifySquares(NEGATIVE));
}

}
Reply 1
Create a while loop that checks whether p, the number in the array, satisfies (p % x) == x, where x is the square root of p. If this is true then p is a square number, and you should create an exit flag, such as setting exit = true, with while condition being exit == false.

EDIT: thought you were looking for arrays without squares. Exit the loop if (p % x) != x.

That's the program logic down for you. I would post in full Java. but I'm a bit rusty.
(edited 9 years ago)
Reply 2
Original post by VannR
Create a while loop that checks whether p, the number in the array, satisfies (p % x) == x, where x is the square root of p. If this is true then p is a square number, and you should create an exit flag, such as setting exit = true, with while condition being exit == false.

EDIT: thought you were looking for arrays without squares. Exit the loop if (p % x) != x.

That's the program logic down for you. I would post in full Java. but I'm a bit rusty.


Well thanks for the explanation anyway :smile:

Quick Reply

Latest

Trending

Trending