Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions Switch control Statement

Have Programming Question? Ask it here!
 
 
 


Rajan Thapa
Switch control Statement
2 Answer(s)      3 years and a month ago
Posted in : Java Interview Questions


  1. Write a Java Program with file name SectionB.java using Switch control Statement

The program will use the JOptionPane and ask the user to enter any number between 1 to 12.

Based on the input, then the program will show how many days in that month. For example if the user enters 1, the program will show ‚??The Month you entered is January and it has 31 days‚??. The output will be shown in the JOptionpane.

  1. Now modify the program; if the user enters ‚??2‚?? the program will ask the user if it is a leap year. If the answer from the user is ‚??Yes‚?? it will show the message ‚??February has 29 days‚??, if the response from the user is ‚??No‚?? it will show ‚??February has 28 days‚??. Again, the output should be shown in JOptionPane.

NOTE:

[In this program use try and catch block to handle any unacceptable and invalid input from the user]


Advertisement
View Answers

March 21, 2011 at 11:44 AM


  1. Write a Java Program with file name SectionB.java using Switch control Statement

The program will use the JOptionPane and ask the user to enter any number between 1 to 12.

Based on the input, then the program will show how many days in that month. For example if the user enters 1, the program will show ‚??The Month you entered is January and it has 31 days‚??. The output will be shown in the JOptionpane.

  1. Now modify the program; if the user enters ‚??2‚?? the program will ask the user if it is a leap year. If the answer from the user is ‚??Yes‚?? it will show the message ‚??February has 29 days‚??, if the response from the user is ‚??No‚?? it will show ‚??February has 28 days‚??. Again, the output should be shown in JOptionPane.

NOTE:

[In this program use try and catch block to handle any unacceptable and invalid input from the user]



March 25, 2011 at 4:23 PM


import javax.swing.*;
import java.util.*;
import java.text.*;

class SectionB{
    static String getMonthName(int m) {
    String month = "invalid";
    DateFormatSymbols dfs = new DateFormatSymbols();
    String[] months = dfs.getMonths();
    if (m >= 0 && m <= 11 ) {
        month = months[m];
    }
    return month;
}

    public static void main(String[] args){
            int days=0;
        String st=JOptionPane.showInputDialog(null,"Enter number between 1 and 12: ");
        int num=Integer.parseInt(st);
        switch(num){
            case 1:
            days=31;
            break;

            case 2:
            String str=JOptionPane.showInputDialog(null,"Is this a leap year?");
            if(str.equalsIgnoreCase("yes")){
            days=29;
            }
            else{
            days=28;
            }
            break;

            case 3: 
            days=31;
            break;

            case 4:
            days=30;
            break;

            case 5: 
            days=31;
            break;

            case 6:
            days=30;
            break;

            case 7: 
            days=31;
            break;

            case 8: 
            days=31;
            break;

            case 9: 
            days=30;
            break;

            case 10:
            days=31;
            break;

            case 11: 
            days=30;
            break;

            case 12: 
            days=31;
            break;

            default:
            System.out.println("Invalid!");
            System.exit(0);
        }
        JOptionPane.showMessageDialog(null,"The Month you entered is "+getMonthName(num-1)+" and it has "+days+" days");
    }
}


Related Tutorials/Questions & Answers:
switch statement
switch statement   i want to write a java program that computes Fibonacci,factorial,string reversal and ackerman using switch case to run as a single program
The Switch statement
. To avoid this we can use Switch statements in Java. The switch statement is used... of a variable or expression. The switch statement in Java is the best way to test.... Here is the general form of switch statement: switch (expression){ case 1
Advertisements
Switch Statement
switch case statement. The program displays the name of the days according to user...Switch Statement  How we can use switch case in java program ?   Note:-Switch case provides built-in multiway decision statement.It
Java Switch Statement
Java Switch Statement       In java, switch is one of the control statement which turns the normal flow control of the program as per conditions. It's like if-else statement but it can
How to use switch statement in jsp code
How to use switch statement in jsp code       switch is a type of control statement used to control the flow of program execution. Its body is known as a switch block
Switch Statement with Strings in Java
Switch Statement with Strings in Java  Switch Statement with Strings in Java
Java switch statement
Java switch statement  What restrictions are placed on the values of each case of a switch statement
Switch statement in PHP
Switch statement in PHP  HII, Explain about switch statement in PHP?   hello, Switch statement is executed line by line. PHP executes the statement only when the case statement matches the value of the switch
Switch case in java
, and java, switch case is used . switch  is a selection control mechanism used... if statementswitch allow the user to select a option from multiple options. switch statement have multiple number of possible execution path. It work
What is the difference between an if statement and a switch statement?
What is the difference between an if statement and a switch statement?   Hi, What is the difference between an if statement and a switch statement... values). Whereas the Switch statement offers you multiple choice of option from
Switch Statement example in Java
Switch Statement example in Java    ... for implementing the switch statement. Here, you will learn how to use the switch statement... more about the switch statement in Java. The following java program tells you
C Break with Switch statement
C Break with Switch statement   ... with Switch-Case in C. The switch case statements allows to control complex... the user to enter any number. The expression defined in the switch statement finds
Java - The switch construct in Java
;    Switch is the control statement in java which... flow of control quits from the Switch block whenever break statement occurs.... In this Program you will see that how to use the switch statement
Java Control Statement
Java Control Statement Control flow statement is a statement that is used to break the sequential execution of statement. control flow statement, controls... statement :  Java supports two selection statements, if and switch
PHP Switch Case
Switch Case Control Structure: Almost every modern language supports switch case control structure. It is similar to if statements. Switch case is useful... the statements until it found a break statement or the end of the switch case block
switch Statement - Overview
Java Notes switch Statement - Overview... on a boolean value (only two possible values). The switch statement allows... enclosed in braces.. The switch statement executes the case corresponding
Switch Statement in java 7
Switch Statement in java 7 This tutorial describes the if statement in java 7. This is one kind of decision making statement. Switch Statements : The switch statement is used when you want to test many statements based on some
i need to replace this if statement code with switch case code
i need to replace this if statement code with switch case code   i need to replace this code with switch case for(int i=0;i<100;i++){ if((i...){ switch(i/j){ case 3: System.out.println ("java
Find Capital using Switch-Case statement
Find Capital using Switch-Case statement In this Java Tutorial section, we are going to find the capital of our states using Switch-Case statement. For this, we have created two arrays i.e. array of states and array of capitals. Then we
Strings in Switch statement
Strings in Switch statement In this section, you will learn about strings..., you can pass string as expression in switch statement. The switch statement... have matched string. The comparison of string in switch statement is case
PHP Control Statement
PHP Control Statement The PHP Conditional statements helps us to perform... and switch case. Example: php-control-stmt.html <!DOCTYPE html PUBLIC "... to control the flow of the program according to the requirement of the program
Summary - Control Flow
Java: Summary - Control Flow Each control statement is one logical... .. else c) if .. elseif .. elsif .. elsif .. else The switch statement can.... Another example of switch statement also shows how to use the switch
Java Control Statements
in C++. The if-then statement is the most simpler form of control flow statement... is executed if "if" statement is false.    Switch... statement is used to force program control back to the top of a loop. 
Switch
statements. To avoid this we can use Switch statements in Java. The switch statement... of a variable or expression. The switch statement in Java is the best way to test.... Here is the general form of switch statement: switch (expression){ case 1
Control Statments
in C++. The if-then statement is the most simpler form of control flow statement... Control Statments      ... top to bottom. We will learn how the different kinds of statement have
Java Control Statements
Statements   The control statement are used to controll the flow of execution... Statements  Selection statements: If Statement: This is a control...;); } Switch Statement: This is an easier implementation to the if-else
Switch Case in Java
if-then and if-then-else statements. As switch statement allows in-numerous possible execution paths... with type byte, short, char, or int. The execution of the switch statement takes... will be executed. A switch block is the body of a switch statement
switch case
switch case  program to input 2 numbers and a choice.Using switch case we need to perform all the mathematical operation.Print a suitable error message if the choice is wrong
switch case
switch case  program to input 2 numbers and a choice.Using switch case we need to perform all the mathematical operation.Print a suitable error message if the choice is wrong
Switch databases
Switch databases  How do you inter-change between relational databases without code changes
for statement
for statement  for(int i=0;i<5;i++); { system.out.println("The value of i is :"+i); } if i end for statement what will be the output   got the answer.. it displays only the last iteration that is "The value of i
programes on switch
=input.nextInt(); switch(choice){ case 1
Break Statement in JSP
are using the switch statement under which we will use break statement. The for loop... Statement in jsp</H1> <% for(int i =0; i <= 5; i++) { switch(i...Break Statement in JSP      
Break statement in java
control flow of  compound statement like while, do-while , for. Break... is also used in switch case. Break statement allow a force termination of  loop. Within a loop if a break statement is encountered then control resume
switch Java Keyword
:  -- The switch statement in java language enables the user to select... of each case block to exit from the switch statement. -- If the break statement... switch Java Keyword      
switch case instead of if else
switch case instead of if else  How to write code in switch case instead of if else in Java
using switch,break and for loop
using switch,break and for loop  generate a 10 digit number and display the length of longest increasing series
using switch and break
using switch and break  generate a 10 digit number and display the length of longest increasing series
Java Review: Control Flow
, a switch statement is better. switch Statement switch (expr... Java Review: Control Flow Java uses the dominant imperative control flow.... Structured Programming control flow primitives within a method: Sequence
Switch case + condition??
Switch case + condition??  I try to use a switch case with condition but it doesn't display anything have look: ......public void commandAction...(); } } else { List down = (List)display.getCurrent(); switch
using switch case
using switch case  Define a class MENU to perform the following operation depending upon the users choice using switch case 1)print square root... choice: "); int choice=input.nextInt(); switch(choice
Switch case in Jsp page
Switch case in Jsp page  <table> <tr><td>Enter Distance :</td> <td><input type="text" name="dis"/></td>... using switch case in jsp page
switch functions - Java Beginners
switch functions  I am writing a script for use in a computer based... your month of hire:") switch(n) { case(n="January"): document.write("Last... friend, This is running code, switch option using in javaSctipt
calender control
calender control  plz tell me about calender type control in java
Session control
Session control  Could we make session control in JSP language without having any help of other framework
Switch Case - Java Interview Questions
Switch Case  int x=100000; switch(x) { case 100000: printf("Lacks"); } output= Lacks I want reason why x matches switch case although range for int is in between something -32678 to +32676. Plz give me ans quickly
Control Flow Statements in java 7
- If statement switch statement conditional operator statement...Control Flow Statements in java 7 In this section we will discuss Control Flow Statements in java 7. This is one type of Language Fundamentals. Control
using if and switch stmt - Java Beginners
be assumed. Write a program using "switch and if statement" to compute net amount...using if and switch stmt  A cloth showroom has announced...(); switch(menu) { case 1: System.out.print("Quantity: "); int q1 = scan.nextInt
PHP Conditional Statement
supports if , else, switch case and conditional operator for this purpose. PHP Conditional Statement Example 1: <?php $a=12; if($a>10)  ... Example 4: <?php $a=1; switch($a) { case 0:   
 

 

 

DMCA.com