Answer:
The program is written using jCreator IDE
Comments are used for explanatory purpose
Program starts here
import java.util.*;
public class courecode
{
public static void main(String [] args)
{
String ccode; // Declare course code as string
Scanner input = new Scanner(System.in);
System.out.print("Course code: "); // Prompt user for input
ccode = input.nextLine();
boolean flag = true; // Initialize a boolean variable to true
if(ccode.length() != 6) // Check if length of course code is 6
{
System.out.print("Input length must be 6"); //Display message if length of input string is not 6
}
else // If length is 6
{
for(int i = 0; i<6;i++) //check validity if input string using for loop
{
if(i == 0) //Test for first character
{
//The following if statement will be executed if the first character is not i or I
if(ccode.charAt(i) != 'i' && ccode.charAt(i) != 'I')
{
System.out.println("Invalid Course Code");
System.out.println("The course code must start with an i or I");
flag = false; //Set boolean variable to false
break;
}
}
else if(i == 1)
{
//The following if statement will be executed if the second character is not t or T
if(ccode.charAt(i) != 't' && ccode.charAt(i) != 'T')
{
System.out.println("Invalid Course Code");
System.out.print("The second letter of the course code must be t or T");
flag = false; //Set boolean variable to false
break;
}
}
else
{
//The following if statement will be executed if any of the last 4 characters is not an integer
if(!Character.isDigit(ccode.charAt(i)))
{
System.out.println("Invalid Course Code");
System.out.print("The last 4 characters of the course code must be an integer");
flag = false; //Set boolean variable to false
break;
}
}
}
}
//The following statment checks if boolean variable flag is still true
// If true, then the input course code is valid and the accompanying print statement will be executed
if(flag)
{
System.out.print("The input course code is valid");
}
}
}
Step-by-step explanation:
Lines marked with // are comments
Check comments (//) for explanation