I am having trouble making a file, I think I have got the majority of it correct, but I’m having trouble getMorseCode() method. I am almost positive it begins with
String morse = "";
however, since letter is a char and morse is a string, it results in an error. Otherwise it complies but simply returns a space when I run the tester I’m not sure what to put in the quotation marks though, can anyone give me a hint please?
Thanks,
public class MorseCode
{
private char letter;
public MorseCode(char let)
{
letter = let;
}
public char getLetter()
{
return letter;
}
public String getMorseCode()
{
String morse = "";
if (morse.equalsIgnoreCase("a"))
morse = ".-";
return morse;
}
}
import java.util.Scanner;
/**
* A class to test the MorseCode class
*/
public class MorseCodeTester
{
/**
* Tests methods of the MorseCode class
* @param args not used
*/
public static void main( String args[] )
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a character: ");
char morseChar = input.nextLine().charAt(0);
MorseCode one = new MorseCode(morseChar);
System.out.println(one.getLetter() + " is " + one.getMorseCode() + " in morse!\n");
Edit : misread the question,
String morse = Character.toString(letter);is what you need. (Assuming you want to use your current code)In any case, you should just conduct a switch statement on the char instead, i.e
The problem with your current code is that morse is just being set to “” and you are testing cases against it, in which it will always evaluate false.