Commit aecf333f authored by Leon's avatar Leon

Second Commit

parent e6d08c5b
......@@ -43,29 +43,24 @@ public class AssessmentPartOne {
// You should comment your code explaining what each part does
int sumOfSquares = 0;
int noSum=0;
int noSum=-1; //if the range descends cancels the sum
int sum=0;
for(int i=start+1;i<end;i++) {
if(i<0)
{
noSum =-1;
}
else
sum = sum + i;
for(int i=start+1;i<end;i++)//+1 skips the start value
{ //sum will run between start and end but not include either
sum = sum + i; //adds the numbers up
}
if (start <0) {
sumOfSquares= noSum;
sumOfSquares= noSum; //if the sum starts in the negative cancels it
}
else if(sum == 0) {
sumOfSquares= sum-1;
else if(start<end) {
sumOfSquares= sum-1; //takes the +1 of the start of the sum if the range decends
}
else {
sumOfSquares = sum;
sumOfSquares = sum; //if the sum is fine returns it
}
System.out.println(sumOfSquares);
return sumOfSquares;
......
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.
char[] word = aWord.toCharArray(); //converts the word into a character array
int score =0;//tally of the word score
for(int i =0; i < word.length;i++) {//goes through every letter of the word until there's no more letters
if (word[i] == 'e') {//if the letter is present then adds the correct number of points to it
score = score +1;//score
}
if (word[i] == 'a') {
score = score +1;
}
if (word[i] == 'o') {
score = score +1;
}
if (word[i] == 't') {
score = score +1;
}
if (word[i] == 'i') {
score = score +1;
}
if (word[i] == 'n') {
score = score +1;
}
if (word[i] == 'r') {
score = score +1;
}
if (word[i] == 's') {
score = score +1;
}
if (word[i] == 'l') {
score = score +1;
}
if (word[i] == 'u') {
score = score +1;
}
if (word[i] == 'd') {
score = score +2;
}
if (word[i] == 'g') {
score = score +2;
}
if (word[i] == 'c') {
score = score +3;
}
if (word[i] == 'm') {
score = score +3;
}
if (word[i] == 'b') {
score = score +3;
}
if (word[i] == 'p') {
score = score +3;
}
if (word[i] == 'h') {
score = score +4;
}
if (word[i] == 'f') {
score = score +4;
}
if (word[i] == 'w') {
score = score +4;
}
if (word[i] == 'y') {
score = score +4;
}
if (word[i] == 'v') {
score = score +4;
}
if (word[i] == 'k') {
score = score +5;
}
if (word[i] == 'j') {
score = score +8;
}
if (word[i] == 'x') {
score = score +8;
}
if (word[i] == 'q') {
score = score +10;
}
if (word[i] == 'z') {
score = score +10;
}
}
return score;
}
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'
boolean pass = false;
boolean hasUppercase = !password.equals(password.toLowerCase());
boolean hasLowercase = !password.equals(password.toUpperCase());
boolean noConditions = !(password.contains("password") || password.contains("passwd"));
char [] pW = password.toCharArray();
if(pW.length <8) {
pass = false;
}
else if(pW.length>16) {
pass = false;
}
else if(!hasUppercase) {
pass= false;
}
else if(!hasLowercase) {
pass= false;
}
else if(!noConditions) {
pass= false;
}
else {
pass = true;
}
return pass;
}
}
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