Commit 05634b39 authored by daniel.blaho's avatar daniel.blaho

First blood. Added all files.

parent 4daf673b
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.
int scoreTotal = 0; // Integer used to store the final score, and to add to it during the loop.
for (int i = 0; i < aWord.length(); i++){ // I used a for loop to iterate through the length of the aWord string.
char calculatedLetter = aWord.charAt(i); // I created a new char to use with the switch below which iterates
// through the individual characters in the string aWord.
switch (calculatedLetter) { // I decided to use a switch so I could assign a score to the characters.
case 'e': case 'a': case 'i': // I first list the cases - the letters which will be assigned to a score.
case 'o': case 'n': case 'r':
case 't': case 'l': case 's': // Then, if the letter within this block is found within the word given,
case 'u': // scoreTotal is set to it's value plus 1.
scoreTotal +=1; break; // Break is used to end this switch case block, and the program moves on
// through the rest of the code.
case 'd': case 'g':
scoreTotal +=2; break; // In this case, the scoreTotal is set to it's value plus 2.
case 'b': case 'c': case 'm':
case 'p':
scoreTotal +=3; break; // In this case, the scoreTotal is set to it's value plus 3, and so on.
case 'f': case 'h': case 'v':
case 'w': case 'y':
scoreTotal +=4; break;
case 'k':
scoreTotal +=5; break;
case 'j': case 'x':
scoreTotal +=8; break;
case 'q': case 'z':
scoreTotal +=10; break;
default: break; //Default is ran if there is no case match with the input used.
}
}
return scoreTotal; //Returns the final score for the words.
}
public Boolean passwordValidator(String password)
{
// 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'.
int length = 0; // First I created an integer called length to be used in a while loop.
char check; // I created a char called check to be used in the while loop.
boolean upperChar = false; // I created separate booleans to check for each validation method. This one is for upper characters.
boolean lowerChar = false; // This boolean is for lower character validation.
boolean hasNumber = false; // This boolean is for number validation.
boolean excludePhrase = true; // I defaulted the excludePhrase to be true to make the validation for it easier to code.
boolean isValid = false; // This boolean is for the result, if the password is valid or not.
while (length <= password.length() -1 && password.length() >= 8 && password.length() <= 16) {
// I set-up the while loop to run between the inclusive lengths of 8-16 characters.
check = password.charAt(length); // This code checks for characters at a given index.
if(Character.isLetter(check)) { // This if statements runs if the character at the index is a letter.
if (Character.isUpperCase(check)) { // This nested if statement runs if the character is uppercase.
upperChar = true;
}
else { // Else the character is lowercase.
lowerChar = true;
}
}
else if (Character.isDigit(check)) { // Checks if the character at a given index is a digit.
hasNumber = true;
}
length++; // Iterates through the while loop.
}
if (password.contains("passwd") || password.contains("password")){ // This is the check to find if the password contains
excludePhrase = false; // the phrases which aren't allowed to be used.
}
if (upperChar && lowerChar && hasNumber == true) { // This if statement runs if the three conditions are met (are true).
if (excludePhrase == true) { // This nested if statement runs if password passed the above check for phrases.
isValid = true; // The result is set to true, so the password will pass the validation.
}
else {
isValid = false; // If any conditions aren't met, then the password didn't pass the validation.
}
}
return isValid; // Returns the result.
}
}
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