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 :...
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 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:...
Write a program that prompts the user to enter a series of strings, but with each...
Write a program that prompts the user to enter a series of strings, but with each string containing a small integer. Use a while loop and stop the loop when the user enters a zero. When the loop has finished, the program should display: the number of user inputs (not counting the final zero input). the total of the integers in the strings entered. the average of the integers accurate to one decimal place. Any help is greatly appreciated, this...
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199 Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199   Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT