Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
ChristopherFosterAssessmentPart2
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
christopher.foster
ChristopherFosterAssessmentPart2
Commits
d8539125
Commit
d8539125
authored
Jan 05, 2019
by
Chris
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
uploaded
parents
Pipeline
#589
failed with stages
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
230 additions
and
0 deletions
+230
-0
AssessmentPartTwo.java
AssessmentPartTwo.java
+183
-0
AssessmentPartTwoTest.java
AssessmentPartTwoTest.java
+47
-0
No files found.
AssessmentPartTwo.java
0 → 100644
View file @
d8539125
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'
}
}
AssessmentPartTwoTest.java
0 → 100644
View file @
d8539125
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
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment