I'd like to start out that I'm new and in need of assistance.
Prompt: Write a method, public static boolean Language(String word), to recognize words from the following language:
{ a^n | n is a prime number }
If the input "word" is from the language the method returns true and false.
Test Examples:
Input: aaa true
Input: aaaaaaaa false
Input: aaaaaaa true
Input: a false
Input: aba false
I appreciate the help and time taken.
EDIT:
Okay so I was extremely confused on the isPrime and what to input, but at this point I'm at isPrime(String word) And compiling gives me an error at "boolean F = isPrime(word.length())" cannot be applied to (int).
import java.io.*;
import java.util.*;
public class Homework_2F_2
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a word from language \"A\" ");
String word = keyboard.nextLine();
boolean D = isTrue(word);
if(D == false)
System.out.println("This word is not part of the language");
boolean F = isPrime(word.length());
if(F == true && D == true)
System.out.println("This word is part of the language");
}
//Check for language Method
public static boolean isTrue(String word)
{
if(word.length() == 1)
return false;
for(int i = 0; i < word.length(); i++)
{
if(word.charAt(i) != 'a')
return false;
}
return true;
}
//Method to find if word is prime
public static boolean isPrime(String n)
{
}
}