54.8k views
3 votes
In this assessment, you will design and code a Java console application that validates the data entry of a course code (like IT4782) and report back if the course code is valid or not valid. The application uses the Java char and String data types to implement the validation. You can use either the Toolwire environment or your local Java development environment to complete this assignment. The requirements of this application are as follows: The application is to read a course code entered by the user from the keyboard. The course code is made of 5 characters and should follow these rules: • First character is always an upper case I or a lower case i • Second character is always an upper case T or a lower case t • Third, fourth, fifth, and sixth characters are always digits (0-9) The application then validates the course code against above the rules and prints a message if the course code is valid or not. If the course code is not valid, the application should print a message explaining why the course code is not valid. Use these course codes to test your application: IT4782 iT4782 OT4782 it&782 Successful completion of this assignment will show a valid message or an invalid message for the entered course code. In addition, if the course code is invalid, the application should identify the reason for the invalidation. Your program output should look like this sample output:

User Lesmana
by
8.5k points

1 Answer

6 votes

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

User Brooklynsweb
by
7.0k points