Commit 0d9e639c authored by Ash's avatar Ash

New Version

parent c899adb3
Pipeline #569 failed with stages
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class AssessmentPartFour { public class AssessmentPartFour {
...@@ -7,18 +10,82 @@ public class AssessmentPartFour { ...@@ -7,18 +10,82 @@ public class AssessmentPartFour {
public int loadMorseFromFile(String filename) public int loadMorseFromFile(String filename)
{ {
//This section creates a new instance of File called file and reads from filename
File file = new File(filename);
//This section of the code clears the arraylist morsecode of all elements within effectively setting it to equal nothing
morseCode.clear();
//This section of the code sets up a scanner called Read
Scanner Read;
//This section of the code tries to run if an error throws up it instead runs the catch code instead
try {
//This section states that Read is equal to a new instance of Scanner which reads from the File file
Read = new Scanner(file);
//This Section Creates a new string called Holder
String Holder;
//The section states that while the scanner Read has another line from file to read it will continue
while (Read.hasNextLine()) {
//This section of the code sets Holder to be equal to Read's next line of input
Holder = (Read.nextLine());
//This section of the code adds the value of holder to the Arraylist morsecode
morseCode.add(Holder);
return 0; }
//This code runs if the code above throws an error
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Once all the lines of code have been added it returns the size of the arraylist morsecode
return morseCode.size();
} }
public String translateMorse() public String translateMorse()
{ {
return ""; //This section of the code creates a new instance of StringBuilder called decoded
StringBuilder decoded = new StringBuilder();
//This section of code creates a new string called Words
//And for each string In morseCode it will run this code below using Words to hold each string
for(String Words:morseCode) {
//This section creates a new string called Result and sets it to be equal to the result of the Method MorseToEnglish
//This section of code takes in the string Words in order to run the method MorseToEnglish using this value
String Result = MorseToEnglish(Words);
//This section adds the String Result to the stringBuilder decoded
decoded.append(Result);
}
// //This section of the code returns the value of the stringBuilder decoded in a string
return decoded.toString();
}
//This is a new method that I created that takes in the string Morse
public String MorseToEnglish(String Morse) {
//This is where I create two strings one called alpha containing the english alphabet and the other called morse which contains the morse equivalents
String[] alpha = {"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", " "};
String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-","...-" ,".--" ,"-..-", "-.--", "--..", "/"};
//This section of code creates the int i and sets it to 0. it then states that for each time i is less than morse's length increment it by one and then run the code below
for(int i = 0; i < morse.length; ++i){
//This section of code compares the string Morse to the String morse based on the value of i
if(Morse.equals(morse[i])) {
//This section of code returns the alpha value of morse if they both match
return alpha[i];
}
}
return null;
} }
} }
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