Question

In: Computer Science

Write a java program that prompts user to enter his name and KSU ID using format...

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.

Solutions

Expert Solution

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.


Related Solutions

Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
jgrasp environment, java write a complete program that prompts the user to enter their first name,...
jgrasp environment, java write a complete program that prompts the user to enter their first name, middle name, and last name (separately). print out thier name and initials, exactly as shown: your name is: John Paul Chavez your initials are: J. P. C. use string method chartAt() to extract the first (zero-th) character from a name(the name is a string type): username.charAt(0). thank you.
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers using a sentinel while loop. The program should accept integer inputs until the user enters the value -1 (negative one is the sentinel value that stops the while loop). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output. The program should ignore any other negative...
JAVA Write a program that will compare two names. The program prompts the user to enter...
JAVA Write a program that will compare two names. The program prompts the user to enter two names for a comparison. If the names are same, the program states that. If the names are different, the program converts both names to UPPERCASE, and compares then again. If they are equal, the programs displays a message stating that names are equal if CASE is ignored. Otherwise, the program prints names with a message that names are not equal.  Check also if there...
Write a program that prompts the user to enter a file name, then opens the file...
Write a program that prompts the user to enter a file name, then opens the file in text mode and reads names. The file contains one name on each line. The program then compares each name with the name that is at the end of the file in a symmetrical position. For example if the file contains 10 names, the name #1 is compared with name #10, name #2 is compared with name #9, and so on. If you find...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
JAVA Write a test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
JAVA Write a program that prompts the user to enter a matrix number of rows and...
JAVA Write a program that prompts the user to enter a matrix number of rows and number of columns. In main method create 2D matrix based on the number of rows and columns input by the user; randomly fills the matrix with 0s and 1s and prints it. Create method sumColumns that takes only the matrix you created in main method and find the sum of each column with even index and prints it. Do not use global variables. Here...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT