Home Answers Viewqa Java-Interview-Questions Java Control Statement

 
 


Rajan Thapa
Java Control Statement
1 Answer(s)      2 years and 2 months 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]

View Answers

March 25, 2011 at 4:20 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 Pages:
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
Summary - Control Flow
Java: Summary - Control Flow Each control statement is one logical statement, which often encloses a block of statements in curly braces {}. The if else statement can be used in following ways: a) if ..else b) if .. elseif
Java Review: Control Flow
Java Review: Control Flow Java uses the dominant imperative control flow.... Structured Programming control flow primitives within a method: Sequence...). The dreaded goto statement is not popular. if Statement
What is the difference between a break statement and a continue statement?
statement? Thanks,   Hello, In Java programming language supports 3... and return statement etc. Other-then Java programming language the break...What is the difference between a break statement and a continue statement
Control Statments
The if statement: To start with controlling statements in Java, lets have a recap over... in C++. The if-then statement is the most simpler form of control flow statement... executed if and only if the test evaluates to true. That is the if statement in Java
Break statement in java
Break statement in java Break statement in java is used to change the normal control flow of  compound statement like while, do-while , for. Break...; loop. Within a loop if a break statement is encountered then control resume
Java Control Statements
Java Control Statements      ... if The if statement: To start with controlling statements in Java, lets have a recap over... in C++. The if-then statement is the most simpler form of control flow statement
Continue and break statement
statement in java program?   The continue statement restart the current loop whereas the break statement causes the control outside the loop. Here... then break statement will call and control will jump outside the current loop
Java Control Statements
Java Control Statements     ... Statements   The control statement are used to controll the flow of execution... and the conditional logic. Java contains the following types of control statements
Control Flow Statements in java 7
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... but you can also control flow of your program with the help of control flow
Loop Control flow enhancement discussion - Java Tutorial
Loop Control flow enhancement discussion 2001-04-28 The Java Specialists... product for practicing for the SUN Certified Java Programmer Examination... for really "nasty" code. Perhaps you can put a summary statement on your
java if statement
java if statement  If statement in Java
Java - Continue statement in Java
Java - Continue statement in Java       Continue: The continue statement is used in many programming... and continue statement that the break statement exit control from the loop
Continue Statement
the control to the enclosing labeled statement. A continue statement statement must... about Continue Statement click on the link http:/www.roseindia.net/java/beginners... Continue Statement      
calender control
calender control  plz tell me about calender type control in java
PHP Control Statement
PHP Control Statement The PHP Conditional statements helps us to perform... to control the flow of the program according to the requirement of the program. In the current tutorial - we'll study two control statements: If..else
The continue statement The continue statement is used in many programming languages such as C
. There is the difference between break and continue statement that the break statement exit control...The continue statement The continue statement is used in many programming languages such as C. The continue statement The continue statement is used
piracy control - Java Beginners
piracy control  how can i make a program that can control piracy in java? piracy control program means that in which we can only run any text file, mp3 file but it cant be copy from there. can u please help me in making that its
loop, control structures
loop, control structures  a java program to print
loop, control statements
loop, control statements  a java program to print
Create Listbox in JSP using AJAX control
listbox to select single elemnt using AJAX control . But i don't know anything... language="java" import ="java.sql.*" %> <% String buffer="<select id...:3306/test","root","root"); Statement stmt = con.createStatement
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
if else statement in java
if else statement in java  if else statement in java explain with example
JAVA statement - Java Beginners
JAVA statement  The import statement is always the first noncomment statement in a Java program file.Is it true?  Hi Friend, No,it is not true.If your class belongs to a package, the package statement should
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
if else statement in java
if else statement in java  explain about simple if else statement and complex if else statement in java with an example
Java error unreachable statement
Java error unreachable statement   ... exits the control of the expression from the loop, but continue statement put... a code that help you in understanding a unreachable statement. For this we
Switch Statement
Switch Statement  How we can use switch case in java program ?  ... switches to statement by testing the value. import java.io.BufferedReader; import... switch case statement. The program displays the name of the days according to user
Java callable statement
Java callable statement  What is callable statement? Tell me the way to get the callable statement
ToggleButtonBar Control in Flex4
ToggleButtonBar control in Flex4: The ButtonBar control and the ToggleButtonBar control both are same. The ToggleButtonBar control is a MX component.... This control defines only single event itemClick event. In ToggleButtonBar
loops , control structures
loops , control structures  a simple java program to print 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1
conditional statement in java
conditional statement in java  Write a conditional statement in Java   Logical and comparison statements in OOPs are also know as conditional statements. You can learn "How to write conditional statement in Java " from
Java switch statement
Java switch statement  What restrictions are placed on the values of each case of a switch statement
Java if else
Java if else       The if-else statement is one of the widely used control flow statements while...:/www.roseindia.net/java/beginners/IfElse.shtml  
Java - The switch construct in Java
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
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
GOTO Statement in java
GOTO Statement in java  I have tried many time to use java goto statement but it never works i search from youtube google but e the example on net are also give compile error. if possible please give me some code with example
java program using control statements...
java program using control statements...  public class ControlStatements { public static void main(String args[]){ int x=10; if(x==0){ System.out.println("It is zero"); else if(x>0) System.out.println
'if' Statement - if inside if
Java Notes'if' Statement - if inside if if inside if You can put an if statement inside another if statement. Example -- series... the condition in an if statement, Java thinks it's finished with the body
ELSE STATEMENT!! - Java Beginners
ELSE STATEMENT!!  Hi! I just want to know why doesn't my else statement works? Else statement is there in the code below which is: else JOptionPane.showMessageDialog(null, n+ " is not in the array!");//doesn't work
if Statement - Overview
Java Notesif Statement - Overview Purpose The purpose of the if statement is to make decisions, and execute different parts of your program depending... with if. [The other 1% of the decisions use the switch/case statement
if Statement - Overview
Java Notesif Statement - Overview Purpose The purpose of the if statement... is a statement? A statement is a part of a Java program. We have already... directly after the condition in an if statement, Java thinks it's finished
Java IF statement problem
Java IF statement problem  Dear Sir/Madam i am using the following code. expected to not have any output but it still showing "welcome".. please help me why it showing "welcome". class c1 { int a=5; while(a>1
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? Thanks,   Hi, The if statement gives options to select one option
Branching Statements in java 7
Branching Statements in java 7. This is one type of control flow statement. Branching... statement provided by the java 7. Its functionality to stop the current... statement is to exits the current method and it returns the flow of your control
If statement in java 7
If statement in java 7 This tutorial describes the if statement in java 7. This is one kind of decision making statement. There are various way to use if statement with else - If Statement :  If statement contains one boolean
Control Tags-If / Else If / Else
Control Tags-If / Else If / Else       In this section we are going to discuss the various control tags ( The Control Tags are used for flow control such as if, else and iterate.)  'If' tag could
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.