Question

In: Computer Science

Write a program that prompts the user to enter two characters and display the corresponding major...

Write a program that prompts the user to enter two characters and display the corresponding major and year status. The first character indicates the major. And the second character is a number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. We consider only the following majors: B (or b): Biology C (or c): Computer Science I (or i): Information Technology and Systems M (or m): Marketing H (or h): Healthcare Management A (or a): Accounting Note that your program needs to let the user know if the major or year is invalid. Also, your program should be case-insensitive: your program should tell ''Marketing'' either user type 'm' or 'M'. Here are three sample runs: Sample 1: Enter two characters: i3 Information Technology and Systems Junior Sample 2: Enter two characters: B5 Biology Invalid year status Sample 3: Enter two characters: t2 Invalid major Sophomore 1 ITSS3311 Introduction to Programming Project 2 What to deliver? Your .java file including: 1. Five sample runs with the following five input: (20 points, 4 points each) (1) h1 (2) T3 (3) A2 (4) I0 (5) c4 Note that you need to run your program 5 times. Each time you run it, copy and paste the program output to the top of your program. (5 points for pasting the code in the beginning) 2. Your code with other appropriate comments. (Code: 50 points, Comments:25points)

Solutions

Expert Solution

/*
* The java program prompts the user to enter two characters. Then find the major and course name of the student.
* Print the result of the two characters on console. The program is tested for 5 different input values.

* Print the results on the console output.
* */

//MajorYearStatus.java
import java.util.Scanner;
public class MajorYearStatus{
   public static void main(String[] args)
   {
       //Create an instance of Scanner class to read input from keyboard
       Scanner console = new Scanner(System.in);

       //Prompt to enter two characters
       System.out.print("Enter two characters: ");
       String studentStatus = console.nextLine();
      
       //checking if lenght of the user input is less than 2
       //Then print the input is invalid
       if(studentStatus.length()<2)
           System.out.println("Invalid input");
       else
       {
           //Convert the first character in studentStatus string to upper case
           //and assign the result to the major character
           char major = Character.toUpperCase(studentStatus.charAt(0));
           //Get the second character and assign the result to the year character
           char year = studentStatus.charAt(1);
          
           //calling getMajor method
           String courseName = getMajor(major);
           //calling getYear method
           String yearName = getYear(year);
           //Print course name and year name values on cosnole
           System.out.printf("%s %s%n", courseName, yearName);
       }
   }
   /*The method getMajor that takes a major as character as input argument
   * and then matches the corresponding charactor in switch case .Then return the course name
   * of student*/
   public static String getMajor(char major)
   {
       String courseName="";
       switch(major)
       {
       case 'A':
           courseName="Accounting";
           break;
       case 'B':
           courseName="Biology";
           break;
       case 'C':
           courseName = "Computer Science";
           break;
       case 'H':
           courseName="Healthcare Management";
           break;  
       case 'I':
           courseName = "Information Technology and Systems";
           break;
       case 'M':
           courseName = "Mathematics";
           break;
       default:
           courseName ="Invalid major";
       }
       return courseName;
   } //end of the method , getMajor

   /*The method getYear that takes a character year as input argument
   * and then matches the corresponding year .Then return the year name
   * of student*/
   public static String getYear(char year)
   {
       String yearName;
       switch(year)
       {
       case '1':
           yearName = "Freshman";
           break;
       case '2':
           yearName = "Sophmore";
           break;
       case '3':
           yearName = "Junior";
           break;
       case '4':
           yearName = "Senior";
           break;
       default:
           yearName ="Invalid year";
       }
       return yearName;
   } //end of the method,getYear
} //end of the class

-------------------------------------

Sample Output:

Sample Run1:
Enter two characters: h1
Healthcare Management Freshman

Sample Run2:
Enter two characters: T3
Invalid major Junior

Sample Run3:
Enter two characters: A2
Accounting Sophmore

Sample Run4:
Enter two characters: I0
Information Technology and Systems Invalid year

Sample Run5:
Enter two characters: c4
Computer Science Senior


Related Solutions

write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a program that prompts the user for their first and lastname. Display the first...
Write a program that prompts the user for their first and last name. Display the first initial of their first name and their last name to the user.Ask the user to input a phone number.The program checks which part of Colorado a phone number is from using the values below.If the second digit of the phone number is one of the below digits, print the phone number and which part of Colorado it is from. If none of the digits...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
Write a program that asks the user to enter an array of 8 characters then you...
Write a program that asks the user to enter an array of 8 characters then you have to check if characters ‘b’ or ‘a’ appears within the characters and replace them with ‘B’ or ‘A’. For example you enter “a m a l a a b d” The output should be “A m A l A A B d”
Write a program that prompts user to enter integers one at a time and then calculates...
Write a program that prompts user to enter integers one at a time and then calculates and displays the average of numbers entered. Use a while loop and tell user that they can enter a non-zero number to continue or zero to terminate the loop. (Switch statement) Write a program that prompts user to enter two numbers x and y, and then prompts a short menu with following 4 arithmetic operations: Chose 1 for addition Chose 2 for subtraction Chose...
Write a test program that prompts the user to enter a sequence of numbers ending with...
Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach. in java please
Problem 1 Write a program that prompts the user to enter an integer It then tells...
Problem 1 Write a program that prompts the user to enter an integer It then tells the user if the integers is a multiple of 2, 3, 5, 7 or none of the above. Program language is C Ex. Enter an integer 12 You entered 12 The number you entered is a multiple of 2 ----------------------------------------------- Enter an integer 11 You entered 11 The number you entered is not a multiple of 2, 3, 5, or 7
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT