1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/* If Else-If statement Example This Java Example shows how to use if else-if statement in Java program. */ public class IfElseIfElseExample { public static void main(String[] args) { /* * If Else-if statement is used to execute multiple of actions based upon * multiple conditions. * Sysntax of If Else-If statement is * * if(<condition1>) * statement1 * else if(<condition2>) * statement2 * .. * else * statement3 * * If <condition1> is true, statement1 will be executed, else if <condition2> * is true statement2 is executed and so on. If no condition is true, then else * statement will be executed. */ int i = 10; if(i > 100) System.out.println("i is grater than 100"); else if(i > 50) System.out.println("i is grater than 50"); else System.out.println("i is less than 50"); } } /* Output would be i is less than 50 */ |
Well thx for this 🙂
too many comment can’t see the program…
good Answer in continue example
[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]
class must implement the method of intfB
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.
Nice 1…………….
its very helpfully thank u thanks lottttttttttttttt
you should override the method MetfodofintB() in class a
otherwise the calss A becomes again abstract and you can not override other method also
Its 100% correct..
what a stupid way of indenting code
Where is Main method.
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..
there is no main class
apart from that inerface concept is correct
..wahh..!!! its so hard..
those comments r helpful…….
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]
Its Not Working Like This As You Showed…. i’ve tried myself too much but if else stament not work correctly…
HI
This is pretty clear to understand.
Thanks so much.
Billu…..
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.
Line 22 curly brace ({) is extra
Well … above code work with out interface IntExample. What is the importance of it in the above code then?
–Murali
thanks for this
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..????
thnx
interface A{
void fun(int k);
}
interface A{
void fun(int);
}
Which interface is correct and please give me reason….
: