If Else Statement

If Else-If statement Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

Add Comment

  • [code]
    class A implements IntfA
    {public
    int a;
    A()
    {this(10);
    }

    A(int x)
    {a=x;
    }

    public String MethodofIntfA()
    {String s=”mehul”;
    return s;
    }
    public void methodofA()
    {System.out.println(” Method Of A ” +a);
    }
    }

    interface IntfA extends IntfB
    {
    //constant can be declare;;;;;;;;;;;;;;;;;;;; how use it’s
    public String MethodofIntfA();
    //public String MethodofIntfA1();
    }

    public interface IntfB
    {//constant can be declare;;;;;;;;;;;;;;;;;;;; how use it’s
    public String MethodofIntfB();
    //public String MethodofIntfA1();
    }
    [/code]

      • it cannot be run. because interface can not be implement . because it contain the partial function. that’s why

    • [code]
      class A implements IntfA,IntfB
      {
      public int a;
      A()
      {this(10);
      }

      A(int x)
      {a=x;
      }

      public String MethodofIntfA()
      {
      String s=”Method-A”;
      // System.out.println(s);
      return s;
      }
      public void methodofA()
      {
      System.out.println(” Method Of A ” +a);
      }
      public String MethodofIntfB()
      {
      String s2=”Method-B”;
      return s2;
      }

      public static void main(String args[])
      {
      A a1=new A();

      String s1=new String();
      String s2=new String();
      s1=a1.MethodofIntfA();
      s2=a1.MethodofIntfB();
      a1.methodofA();
      System.out.println(“”);
      System.out.println(“Method -IntfA”+s1);
      System.out.println(“”);
      System.out.println(“Method -IntfB”+s2);

      }
      }

      interface IntfA extends IntfB
      {
      //constant can be declare;;;;;;;;;;;;;;;;;;;; how use it’s
      public String MethodofIntfA();
      //public String MethodofIntfA1();
      }

      interface IntfB
      {//constant can be declare;;;;;;;;;;;;;;;;;;;; how use it’s
      public String MethodofIntfB();
      //public String MethodofIntfA1();
      }

      /*****************/
      You will have to implement IntfB interface in the Class A
      and i will give you some changes. i hope your query is solved.
      /*************************/
      output
      /**************************/

      Method Of A 10

      Method -IntfAMethod-A

      Method -IntfBMethod-B

      /**************************/
      [/code]

      • slight modification in ur answer

        class A no need to implement the interface IntfB because it is already extended by the interface IntfA. So,Class A can get all methods of interface IntfA and IntfB by implementing IntfA itself.

        isn’t it?

        • Submitted by Anonymous on Thu, 02/11/2010 – 13:24.

          The answer is yes as interface IntfB is extended by IntfA.
          Class A must implement IntfB’s method, as otherwise the contract to implement the methods of interface by a Class implementing it is not fulfilled.

        • Yes you are correct………..class A no need to implement the interface IntfB because it is already extended by the interface IntfA. So,Class A can get all methods of interface IntfA and IntfB by implementing IntfA itself.

    • you should override the method MetfodofintB() in class a
      otherwise the calss A becomes again abstract and you can not override other method also

    • this(10)
      this line shows what???
      we didn’t give any command to print this(System.out.println). Here it hasn’t been given know……so it becomes an error according to me..

  • Get a number as input from the user, and output the equivalent of the number in words. The number inputted should range from 1-10. If the user inputs a number that is not in the range, output, “Invalid number”.

    Use an if-else statement to solve this problem

    Use a switch statement to solve this problem

    ty

    • [code]
      import java.io.Console;
      class IfElse
      {
      public static void main(String[] args)
      {

      Console c = System.console();
      String s=c.readLine(“%s”, “Enter a number between 1 to 10: “);
      int i=Integer.parseInt(s);

      if (i==1)
      {
      System.out.println(“Number entered is One”);
      }
      else if(i==2)
      {
      System.out.println(“Number entered is Two” );
      }
      else if(i==3)
      {
      System.out.println(“Number entered is Three” );
      }
      else if(i==4)
      {
      System.out.println(“Number entered is Four” );
      }
      else if(i==5)
      {
      System.out.println(“Number entered is Five” );
      }
      else if(i==6)
      {
      System.out.println(“Number entered is Six” );
      }
      else if(i==7)
      {
      System.out.println(“Number entered is Seven” );
      }
      else if(i==8)
      {
      System.out.println(“Number entered is Eight” );
      }
      else if(i==9)
      {
      System.out.println(“Number entered is Nine” );
      }
      else if(i==10)
      {
      System.out.println(“Number entered is Ten” );
      }
      else
      System.out.println(“Invalid number”);
      }
      }
      [/code]

    • class number
      {
      public void main(int n)
      {switch(n)
      {case 1:
      System.out.println(“One”);
      break;
      case 2:
      System.out.println(“Two”);
      break;
      .
      .
      .
      .
      .
      case 10:
      System.out.println(“Ten”);
      break;
      default:
      System.out.println(“Invalid Input”);
      }
      }
      }

  • who can help me write a code in java putting in nodes end edges that can be deleted and resized then later find the shortest distance.

  • in ths first example if the value of i is above than hundred means the if and elseif both cases will be true .. but i got the result where only for the first case ….. would able to explain..????

  • interface A{
    void fun(int k);
    }

    interface A{
    void fun(int);
    }

    Which interface is correct and please give me reason….
    :

Sponsors

Facebook Fans