Commit c4aff524 authored by chris's avatar chris

Uploaded

parents
Pipeline #596 failed with stages
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.util.List;
import java.io.File;
import java.util.Scanner;
public class AssessmentPartFour {
List<String> morseCode = new ArrayList<String>();
public int loadMorseFromFile(String filename) //throws IOException
{
//this deletes all data from the array list effectively making it a blank slate.
morseCode.clear();
//this sets up a variable to represent the text file
File morse = new File(filename);
//this creates an instance of a scanner so that the file can be read from.
Scanner readLine;
//This creates a try catch method that prevents the program from breaking if the file cannot be found.
try {
//this creates a new instance of the scanner that allows it to read from the file
readLine = new Scanner(morse);
//this creates a while loop that will continue to iterate until there is no longer anything to read from the file
while (readLine.hasNextLine()) {
//this adds the line that the program has just read into the array list
morseCode.add(readLine.nextLine());
}
//This section of code runs if the file cannot be found and causes an error
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//This returns the number of lines within the array list by calculating the size
return morseCode.size();
}
public String translateMorse()
{
//this creates an instance of a string builder called message
StringBuilder message = new StringBuilder();
//this is a for loop that will continuously loop until the array morseCode is empty
//this has also created a string that is equal to each string in the array morse
for (String sentence: morseCode)
{
//this creates a string called decoded and uses the method translator to translate the string sentence
String decoded = translator(sentence);
//this uses the stringbuilder to add the decoded string to the message
message.append(decoded);
}//this returns the fully decoded message
return message.toString();
}
//this is an additional method that I created that translates the morse for me
public String translator(String morseSentence)
{
//this is an array that contains the english alphabet including a space
String[] englishAlphabet = {"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", " " };
//this is an array that contains the morse alphabet
String[] morseAlphabet = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-","...-" ,".--" ,"-..-", "-.--", "--..", "/"};
//this is a for loop that continues to loop until it reaches the end of the array morseAlphabet
for(int i = 0; i < morseAlphabet.length; ++i){
//this checks whether the word in the array is equal to the letter in the morse alphabet
if(morseSentence.equals(morseAlphabet[i]))
{
//if they are equal then this returns the translated word to the method translateMorse
return englishAlphabet[i];
}
}
//nothing is returned if the program is unable to decipher what is in the file.
return null;
}
}
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 AssessmentPartFourTest {
public static AssessmentPartFour test;
@BeforeAll
static void setUpBeforeClass() throws Exception {
test = new AssessmentPartFour();
}
@ParameterizedTest
@DisplayName("Testing loadMorseFromFile")
@CsvSource({
"test1.txt,9",
"test2.txt,18",
"test3.txt,15",
"test4.txt,13"
})
void testEnryptedCharacter(String filename, int count) {
assertEquals(count,test.loadMorseFromFile(filename));
}
@ParameterizedTest
@DisplayName("Testing translateMorse")
@CsvSource({
"test1.txt,9,java code",
"test2.txt,18,programming is fun",
"test3.txt,15,fingers crossed",
"test4.txt,13,yippee did it"
})
void testEncryptedString(String filename, int count, String message) {
int cc = test.loadMorseFromFile(filename);
if (cc==count)
{
assertEquals(message, test.translateMorse());
}
else
{
fail("File failed to load");
}
}
}
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