Commit d8539125 authored by Chris's avatar Chris

uploaded

parents
Pipeline #589 failed with stages
public class AssessmentPartTwo {
public int scrabbleScore(String aWord)
{
// 03 -Scrabble Score
// Complete this method so that it returns the scrabble score for the word in aWord
// In scrabble each letter is worth a number of points and each word is worth the
// sum of the scores of the letters in it. For this assignment we will ignore
// double/treble letter/word bonuses.
// The English language points per letter can be found at
// https://en.wikipedia.org/wiki/Scrabble_letter_distributions
// You will need to come up with a way of connecting each letter to its score and
// a way of identifying each letter in the word.
//keeps track of the total number of points
int totalPoints = 0;
//an array consisting of all characters that are worth one point
char[] onePoint = { 'e', 'a', 'i', 'o', 'n', 'r', 't', 'l', 's', 'u'};
//an array consisting of all characters that are worth two points
char[] twoPoint = { 'd', 'g'};
//an array consisting of all characters that are worth three points
char[] threePoint = { 'b', 'c', 'm', 'p'};
//an array consisting of all characters that are worth four points
char[] fourPoint = { 'f', 'h', 'v', 'w', 'y'};
//a variable that represents the character worth five points
char fivePoint = 'k';
//an array consisting of all characters that are worth eight points
char[] eightPoint = {'j', 'x'};
//an array consisting of all characters that are worth ten points
char[] tenPoint = {'q', 'z'};
//a for loop that will iterate for each letter that is in the word that is being checked.
for (int i=0; i <aWord.length(); i++)
{
//a variable to check the current character against the point values.
char letter = aWord.charAt(i);
//a loop that will iterate for each character within the onePoint value array
for (int j=0; j <onePoint.length; j++)
{
//an if statement that checks whether the character in the word is equal to the character in the array.
if (letter == onePoint[j])
{
//if both characters are the same then one point is added to the total points.
totalPoints = totalPoints + 1;
}
}
//a loop that will iterate for each character within the twoPoint value array
for (int j=0; j <twoPoint.length; j++)
{
//an if statement that checks whether the character in the word is equal to the character in the array.
if (letter == twoPoint[j])
{
//if both characters are the same then two points are added to the total points.
totalPoints = totalPoints + 2;
}
}
//a loop that will iterate for each character within the threePoint value array
for (int j=0; j <threePoint.length; j++)
{
//an if statement that checks whether the character in the word is equal to the character in the array.
if (letter == threePoint[j])
{
//if both characters are the same then three points are added to the total points.
totalPoints = totalPoints + 3;
}
}
//a loop that will iterate for each character within the fourPoint value array
for (int j=0; j <fourPoint.length; j++)
{
//an if statement that checks whether the character in the word is equal to the character in the array.
if (letter == fourPoint[j])
{
//if both characters are the same then four points are added to the total points.
totalPoints = totalPoints + 4;
}
}
//a loop that will iterate for each character within the eightPoint value array
for (int j=0; j <eightPoint.length; j++)
{
//an if statement that checks whether the character in the word is equal to the character in the array.
if (letter == eightPoint[j])
{
//if both characters are the same then eight points are added to the total points.
totalPoints = totalPoints + 8;
}
}
//a loop that will iterate for each character within the tenPoint value array
for (int j=0; j <tenPoint.length; j++)
{
//an if statement that checks whether the character in the word is equal to the character in the array.
if (letter == tenPoint[j])
{
//if both characters are the same then ten points are added to the total points.
totalPoints = totalPoints + 10;
}
}
//checks if the character in the word is equal to 'k'
if (letter == fivePoint)
{
//if the two characters match then 5 points are added to the total points.
totalPoints = totalPoints + 5;
}
}
//this returns the total number of points gained
return totalPoints;
}
public Boolean passwordValidator(String password)
{
//creates a variable to check if there's a number
boolean numberCheck = false;
//creates a variable to check if there is an uppercase letter
boolean upperCase = password.equals(password.toLowerCase());
//creates a variable to check if there is a lower case letter
boolean lowerCase = password.equals(password.toUpperCase());
//creates an array to house the two prohibited words
String[] prohibitedWords = {"password", "passwd"};
//splits the password into an array of characters
char[] chars = password.toCharArray();
//creates a for loop that repeats for the number of characters in the array
for (char j: chars)
{
//checks if there is a number in the password
if(Character.isDigit(j))
{
//if there is a number then the number check is set to true
numberCheck = true;
}
}
//checks if the number check is true
if (numberCheck == false)
{
//if the check fails then the method returns false
return false;
}
//checks if the length of the password meets the requirements
if (password.length() < 8 || password.length() > 16)
{
//if the password length is not valid the method returns false
return false;
}
else
{
//checks if the entire password is in one case rather than two
if (upperCase || lowerCase)
{
///if it is only in one case then the method returns false
return false;
}
//creates a for loop that repeats for the number of strings in the prohibited words array
for (int i = 0; i < prohibitedWords.length; i++)
{
//checks if the password matches up with the words in the prohibited words array
if ( password.toLowerCase().indexOf(prohibitedWords[i].toLowerCase()) != -1 ) {
//if the password does match up then the method returns false
return false;
}
}
}
//the method returns true if the password meets the required criteria
return true;
// 04 - Password Validator
// Complete this method to validate that the String password
// is a valid password
// A password is valid if it is
// - between 8 and 16 characters long (inclusive)
// - made up of letters (upper or lower), numbers, and the following characters !£$%
// - has at least one lower case letter, one upper case letter and a number
// - does not contain the phrases 'password' or 'passwd'
}
}
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class AssessmentPartTwoTest {
public static AssessmentPartTwo test;
@BeforeAll
static void setUpBeforeClass() throws Exception {
test = new AssessmentPartTwo();
}
@ParameterizedTest
@DisplayName("Testing scrabbleScore")
@CsvSource({
"rabbit,10",
"speaker,13",
"exactly,19",
"xylophone,24",
"application,17"
})
void testScrabbleScore(String theWord, int expectedScore) {
assertEquals(expectedScore, test.scrabbleScore(theWord));
}
@ParameterizedTest
@DisplayName("Testing passwordValidator")
@CsvSource({
"qw11Ij87,true",
"a834j,false",
"a88drTcdmn45tdgjhj,false",
"pmmfj6793,false",
"PASSWORD,false",
"lo98passwdiI,false",
"oi!Fcv98ij,true"
})
void testPasswordValidator(String thePassword, Boolean expectedResult) {
assertEquals(expectedResult, test.passwordValidator(thePassword));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment