Commit af7750b1 authored by thomas.wilson4's avatar thomas.wilson4

Initial commit

parents
import java.util.*;
public class AssessmentPartTwo {
public int scrabbleScore(String aWord)
{
//array of scrabble letters
char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
//array of scrabble letter scores, arranged in a way such that the score for 'a' is at the same index as 'a' in the alphabet array
int[] scores = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
//integer value to keep track of the score as it is added up
int score = 0;
//nested for loop cycles through each letter of aWord and each character in the alphabet array
for(int j = 0; j<aWord.length(); j++) {
for(int i = 0; i<26;i++) {
// if statement compares the letter of aWord that j points to, with the letter of the alphabet that i points to.
if(aWord.charAt(j)==alphabet[i]) {
//if the characters are equal, then the relevant score for that character is found in the scores array using the index of the character in the alphabet array
//it is then added to the score value
score = score + scores[i];
}
}
}
//score is returned
return score;
// 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.
}
public Boolean passwordValidator(String password)
{
String validCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!?$%";
String upperCases = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lowerCases= "abcdefghijklmnopqrstuvwxyz";
String numbers = "1234567890";
if(!password.contains("password") && !password.contains("passwd") ) {
if(password.length()<17 && password.length()>7) {
boolean validChars = true;
boolean lowerCase = false;
boolean upperCase = false;
boolean number = false;
for(int i = 0; i<password.length();i++) {
if(!validCharacters.contains(password.substring(i, i+1))) {
return false;
}
if(upperCases.contains(password.substring(i, i+1))) {
upperCase = true;
}
if(lowerCases.contains(password.substring(i, i+1))) {
lowerCase = true;
}
if(numbers.contains(password.substring(i, i+1))) {
number = true;
}
}
if(upperCase==true && lowerCase ==true && number == true) {
return true;
}else {
return false;
}
}else {
return false;
}
}else {
return false;
}
// 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'
}
private void select(String password) {
// TODO Auto-generated method stub
}
}
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