Question

In: Computer Science

8.25 Lab 6b Switching Up Months Objectives Use Boolean expressions involving String comparison Use String methods...

8.25 Lab 6b Switching Up Months

Objectives

  • Use Boolean expressions involving String comparison
  • Use String methods substring, toUpperCase, equals, and charAt
  • Use switch program control statement
  • Print out a program and turn it in for "eyes-on" grading

Introduction

In this lab you will continue using the other type of conditional statements: the switch-case.

For switch-case conditional statements, the idea is that you have a variable (the switch) that will determine the next course of action depending on its value (the case).

Example structure would be:

int someVariable = 2;
switch (someVariable){
    case 1: /*do something*/;
            break;
    case 2: /*do something else*/;
            break;

}

someVariable is your swtich and the program will decide a case according to its value.

Assignment

Write the class SwitchingUpMonths.java. This program will be completing exactly the same task as Lab3c, however you will need to use switch-case conditional statements instead of if-else.

That is, your program must:

  1. Asks a user to enter the name of a month
  2. Confirms the User's input.
  3. Prints the abbreviation and number corresponding to that month.

The sample output for input February:

Enter the month: 
You entered February
Its abbreviation is FEB
This is month number 2

Solutions

Expert Solution

SOURCE CODE:

import java.util.*;
public class Main
{
   public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("Entet the month:");
        String str=sc.nextLine(); // reading month
        str=str.toUpperCase(); //convert into uppercase
        switch(str) //switch statement
        {
            //check month and printing its abbreviation and number
            case "JANUARY":
                System.out.println("You enteted "+str );
                System.out.println("Its abbreviation is "+str.substring(0,3) );
                System.out.println("This is month number 1");
                break;
            //check month and printing its abbreviation and number
            case "FEBRUARY":
                System.out.println("You enteted "+str );
                System.out.println("Its abbreviation is "+str.substring(0,3) );
                System.out.println("This is month number 2");
                break;
            //check month and printing its abbreviation and number
            case "MARCH":
                System.out.println("You enteted "+str );
                System.out.println("Its abbreviation is "+str.substring(0,3) );
                System.out.println("This is month number 3");
                break;
            //check month and printing its abbreviation and number
            case "APRIL":
                System.out.println("You enteted "+str );
                System.out.println("Its abbreviation is "+str.substring(0,3) );
                System.out.println("This is month number 4");
                break;
            //check month and printing its abbreviation and number
            case "MAY":
                System.out.println("You enteted "+str );
                System.out.println("Its abbreviation is "+str.substring(0,3) );
                System.out.println("This is month number 5");
                break;
            //check month and printing its abbreviation and number
            case "JUNE":
                System.out.println("You enteted "+str );
                System.out.println("Its abbreviation is "+str.substring(0,3) );
                System.out.println("This is month number 6");
                break;
            //check month and printing its abbreviation and number
            case "JULY":
                System.out.println("You enteted "+str );
                System.out.println("Its abbreviation is "+str.substring(0,3) );
                System.out.println("This is month number 7");
                break;
            //check month and printing its abbreviation and number
            case "AUGUST":
                System.out.println("You enteted "+str );
                System.out.println("Its abbreviation is "+str.substring(0,3) );
                System.out.println("This is month number 8");
                break;
            //check month and printing its abbreviation and number
            case "SEPTEMBER":
                System.out.println("You enteted "+str );
                System.out.println("Its abbreviation is "+str.substring(0,3) );
                System.out.println("This is month number 9");
                break;
            //check month and printing its abbreviation and number
            case "OCTOBER":
                System.out.println("You enteted "+str );
                System.out.println("Its abbreviation is "+str.substring(0,3) );
                System.out.println("This is month number 10");
                break;
            //check month and printing its abbreviation and number
            case "NOVEMBER":
                System.out.println("You enteted "+str );
                System.out.println("Its abbreviation is "+str.substring(0,3) );
                System.out.println("This is month number 11");
                break;
            //check month and printing its abbreviation and number
            case "DECEMBER":
                System.out.println("You enteted "+str );
                System.out.println("Its abbreviation is "+str.substring(0,3) );
                System.out.println("This is month number 12");
                break;
            // if not valid month
            default:
                System.out.println("Not valid month");
        }
   }
}


CODE SCREENSHOT:


OUTPUT:
RUN-1:

RUN-2:

RUN-3:


Related Solutions

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT