38.0k views
2 votes
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

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 "Biology" either user type ‘b’ or ‘B’.

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

What to deliver?

Your .java file including:

1. (Code: 5 points, Comments: 3 points) Your code with other appropriate comments.

2. (2 points) Four sample runs with the following four input:
(1) i3

(2) T2

(3) c2

(4) F0

Note that you need to run your program 4 times. Each time you run it, copy and paste the program output to the top of your program. After you finished these 4 runs, comment out the output you pasted so that you program would continue to run without any issue.

User NicoCaldo
by
5.3k points

1 Answer

3 votes

Answer:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner input = new Scanner(System.in);
  5. System.out.print("Please enter two characters: ");
  6. String inputStr = input.nextLine();
  7. if(inputStr.charAt(0) == 'B' || inputStr.charAt(0) == 'b'){
  8. System.out.println("Biology");
  9. }
  10. else if(inputStr.charAt(0) == 'C' || inputStr.charAt(0)== 'c'){
  11. System.out.println("Computer Science");
  12. }
  13. else if(inputStr.charAt(0) == 'I' || inputStr.charAt(0) == 'i')
  14. {
  15. System.out.println("Information Technology and Systems");
  16. }
  17. else{
  18. System.out.println("Invalid major");
  19. }
  20. int num = Character.getNumericValue(inputStr.charAt(1));
  21. if(num >= 1 && num <= 4){
  22. switch (num){
  23. case 1:
  24. System.out.println("freshman");
  25. break;
  26. case 2:
  27. System.out.println("sophomore");
  28. break;
  29. case 3:
  30. System.out.println("junior");
  31. break;
  32. case 4:
  33. System.out.println("senior");
  34. break;
  35. }
  36. }
  37. else{
  38. System.out.println("Invalid year status");
  39. }
  40. }
  41. }

Step-by-step explanation:

The code consists of two main parts. The part 1 is to validate the input major and print out the major according to the input character (Line 10 -22). If the input character is not matched with the target letters, a message invalid major will be displayed.

The part 2 is to validate the year status to make sure it only fall within the range of 1-4 (Line 26 -45). If within the range, the program will display the year major accordingly. If not a message invalid year status will be displayed.

User Rukshan Jayasekara
by
5.4k points