Commit 37b32f75 authored by Bran's avatar Bran

completed

parent c899adb3
import java.util.ArrayList;
import java.util.List;
import java.io.*;
import java.util.Scanner;
public class AssessmentPartFour {
public class AssessmentPartFour
{
List<String> morseCode = new ArrayList<String>();
List<String> morseCode = new ArrayList<String>(); //creates an arraylist called morseCode that can handle strings
public int loadMorseFromFile(String filename)
public int loadMorseFromFile(String filename)
{
File file = new File(filename); // creates a instance of a file
morseCode.clear(); // clears the contents of the array "morseCode
return 0;
Scanner sc = null; // creates a scanner called sc and assigns it a value of null
try //creates a try block and attempts to run the code inside it
{
sc = new Scanner(file); //assigns the value of file to the scanner sc
} catch (FileNotFoundException e) // if an exception is thrown from assigning the value of "file" to the scanner sc,
{
// TODO Auto-generated catch block
e.printStackTrace(); // if an exception is met, prints the stack trace of the instance ready for debugging
}
String lineFromFile; // creates a string called lineFromFile
while (sc.hasNextLine()) //creates a while loop as long as sc has another line waiting
{
lineFromFile = sc.nextLine(); // makes lineFromFile equal to the
morseCode.add(lineFromFile); //adds the value assigned to lineFromFile to the array morseCode
}
return morseCode.size(); //returns the size of the array string morseCode
}
public String translateMorse()
//////////////////////////////////////////////
public String translateMorse(String filename)
{
return "";
}
}
File file = new File(filename); //creates an instance of a file
morseCode.clear(); // clears the contents of morseCode
//creates an array string containing the alphabet in morse code
String[] morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","/"};
String[] 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"," "};
//creates an array string containing the alphabet in lower case
Scanner sc = null; //Assigns a value of null to the scanner "sc"
try //creates a try block and attempts to run the code inside it
{
sc = new Scanner(file); // creates a new scanner and assigns the value of "file" to it
}
catch (FileNotFoundException e) // if the scanner cannot assign the value of "file" , run the following
{
// TODO Auto-generated catch block
e.printStackTrace(); // if an exception is met, prints the stack trace of the instance ready for debugging
}
String lineFromFile; //creates a string called lineFromFile
while (sc.hasNextLine()) //creates a while loop aslong as as sc has another line waiitng
{
lineFromFile = sc.nextLine(); //stores the next line in the scanner sc
morseCode.add(lineFromFile); // assigns the value of lineFromfile to morsecode
}
String message=""; //creates a string called message and assigns it nothing
for (int i = 0; i < morseCode.size();i++) //creates a for loop where i = 0 and runs for the size of the array morseCode
{
for (int x = 0; x < alphabet.length; x++) //creates a nested for loop where x = 0 and runs for the the length of alphabet.length (so 26 times)
{
if (morseCode.get(i).equals(morse[x])) // if the i'th element of morsecode is equal to the x 'th value of the array morse
{
morseCode.set( i, alphabet[x] ); // sets the value of i and the x'th value of alphabet ine the array morseCode
message = message + alphabet[x]; // makes message equal to message + the x'th value of alphabet
}
}
}
return message; //returns the value of message
}
}
\ No newline at end of file
......@@ -27,6 +27,7 @@ class AssessmentPartFourTest {
})
void testEnryptedCharacter(String filename, int count) {
assertEquals(count,test.loadMorseFromFile(filename));
test.morseCode.clear();
}
@ParameterizedTest
......@@ -38,14 +39,17 @@ class AssessmentPartFourTest {
"test4.txt,13,yippee did it"
})
void testEncryptedString(String filename, int count, String message) {
test.morseCode.clear();
int cc = test.loadMorseFromFile(filename);
if (cc==count)
{
assertEquals(message, test.translateMorse());
assertEquals(message, test.translateMorse(filename));
}
else
{
fail("File failed to load");
}
test.morseCode.clear();
}
}
}
\ No newline at end of file
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