In: Computer Science
Write a java program that prompts user to enter his name and KSU ID using format name-ID as one value, his gender as char, and his GPA out of 5. Then your program should do the following:
Print whether the ID is valid or not, where a valid ID should be
of length = 9, and should start with 4.
Calculate the GPA out of 100 which is calculated as follows GPA/5 *
100.
Update the name after converting the first letter to
uppercase.
Print “Mr.” before name if the gender is ‘M’, and print “Mrs.”
before name if the gender is ‘F’. Otherwise print message “invalid
gender” without name and GPA. Then print the new GPA.
import java.util.Scanner;
/* Java class */
public class Main{
public int isValidID(String ksu){
/* This function checks if ksu is valid as per given criteria */
/* Param: An alphanumeric string <s>*/
/* Return: 0 for invalid id, 1 for valid id */
/* declare variables */
int length, i, mode;
String name = "";
String id = "";
/* find length of ksu */
length = ksu.length();
/* check for each character */
/* fetch name and id */
i = 0;
mode = 0;
while(i < length){
/* checks for - sign */
/* Ascii of - is 45*/
if((int)ksu.charAt(i) == 45){
mode = 1;
i = i + 1;
continue;
}
/* when mode = 0 , get the character of name */
if(mode == 0)
name += ksu.charAt(i);
/* when mode = 1 , get the digits of id */
if(mode == 1){
if((int)ksu.charAt(i) >= 48 && (int)ksu.charAt(i) <= 57)
id += ksu.charAt(i);
}
i = i + 1;
}
/* if the length of id part is not equals to 9 */
/* or id does not starts with 4, it is invalid*/
/* check the criteria */
if(id.length() != 9 || (int)id.charAt(0) != 52)
return 0;
/* ksu id is valid */
return 1;
}
public static void main(String[] args){
/* declare objects */
Main obj = new Main();
Scanner sc = new Scanner(System.in);
/* declare variables */
String name ="", temp ="", ksu;
char gender, c;
double gpa;
int status;
/* take inputs of name ksu id, gender and gpa */
System.out.print("Enter your name: ");
name = sc.nextLine();
System.out.print("\nEnter KSU id in <name>-<id> format: ");
ksu = sc.nextLine();
System.out.print("\nEnter the gender (M/F): ");
gender = sc.next().charAt(0);
System.out.print("\nEnter the GPA out of 5: ");
gpa = sc.nextDouble();
/* call function to check id validity */
status = obj.isValidID(ksu);
/* display validity test result */
if(status == 1)
System.out.print("KSU ID is valid!\n");
else
System.out.print("KSU ID is not valid!\n");
/* calculate gpa out of 100 */
gpa = (gpa * 100) / 5;
/* update first later of name to uppercase */
/* store converted name in temp string */
c = Character.toUpperCase(name.charAt(0));
temp += c;
for(int i = 1; i < name.length(); i++)
temp += name.charAt(i);
/* print name based on gender */
/* else print invalid gender */
if(gender == 'M')
System.out.print("\nMr. "+temp);
else if(gender == 'F')
System.out.print("\nMrs. "+temp);
else{
System.out.print("\nInvalid gender!");
}
/* print new gpa */
System.out.print("\nNew GPA: "+gpa);
}
}
___________________________________________________________________
___________________________________________________________________
Enter your name: john Cena
Enter KSU id in <name>-<id> format: john-456789012
Enter the gender (M/F): M
Enter the GPA out of 5: 3.6
KSU ID is valid!
Mr. John Cena
New GPA: 72.0
___________________________________________________________________
Note: If you have
queries or confusion regarding this question, please leave a
comment. I would be happy to help you. If you find it to be useful,
please upvote.