class A extends Thread
{
public void run()
{
System.out.println("Thread A has started");
for (int i=0;i<=5;i++)
{
System.out.println("From thread A:i =" +i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
System.out.println("Thread B has started");
for (int j=0;j<=5;j++)
{
System.out.println("From thread B:j =" +j);
if(j==3)
stop();
}
System.out.println("Exit from B");
}
}
class C extends Thread
{
public void run()
{
System.out.println("Thread C has started");
for (int k=0;k<=5;k++)
{
System.out.println("From thread C:k =" +k);
if(k==1)
try{
sleep(100);
}catch(Exception e)
{
}
}
System.out.println("Exit from C");
}
}
class ThreadMethods
{
public static void main(String args[])
{
A ta = new A();
B tb = new B();
C tc = new C();
tc.setPriority(Thread.MAX_PRIORITY);
ta.setPriority(Thread.MIN_PRIORITY);
System.out.println("Start thread A");
ta.start();
System.out.println("Start thread B");
tb.start();
System.out.println("Start thread C");
tc.start();
System.out.println("End of main Thread");
}
}
Function Overloding In Java.
class FunctionOverloading
{
int a;
FunctionOverloading()
{
a=10;
}
void volume()
{
System.out.println("Valume = " +(a * a * a));
}
int volume(int i)
{
return(i*i*i);
}
float volume(float i)
{
return(i*i*i);
}
public static void main(String args[])
{
int intVolume;
float floatVolume;
FunctionOverloading f=new FunctionOverloading();
f.volume();
intVolume=f.volume(5);
System.out.println("Valume = " +intVolume);
floatVolume=f.volume(2.3f);
System.out.println("Valume = " +floatVolume);
}
}
{
int a;
FunctionOverloading()
{
a=10;
}
void volume()
{
System.out.println("Valume = " +(a * a * a));
}
int volume(int i)
{
return(i*i*i);
}
float volume(float i)
{
return(i*i*i);
}
public static void main(String args[])
{
int intVolume;
float floatVolume;
FunctionOverloading f=new FunctionOverloading();
f.volume();
intVolume=f.volume(5);
System.out.println("Valume = " +intVolume);
floatVolume=f.volume(2.3f);
System.out.println("Valume = " +floatVolume);
}
}
Array Examples In Java.
class ArrayExample
{
public static void main(String args[]) throws Exception
{
int arr[]=new int[5];
arr[0]=45;
arr[1]=24;
arr[2]=39;
arr[3]=25;
arr[4]=12;
System.out.println("Array elements are :");
for(int i=0;i<5;i++)
{
System.out.println("Arr["+ i +"] = " +arr[i]);
}
}
}
{
public static void main(String args[]) throws Exception
{
int arr[]=new int[5];
arr[0]=45;
arr[1]=24;
arr[2]=39;
arr[3]=25;
arr[4]=12;
System.out.println("Array elements are :");
for(int i=0;i<5;i++)
{
System.out.println("Arr["+ i +"] = " +arr[i]);
}
}
}
Program to Find Square Root of Number.
import java.lang.Math;
public class SquareRoot
{
public static void main(String args[])
{
double x=9;
double y;
y=Math.sqrt(x);
System.out.println("Y = "+y);
}
}
public class SquareRoot
{
public static void main(String args[])
{
double x=9;
double y;
y=Math.sqrt(x);
System.out.println("Y = "+y);
}
}
Program To Find Area Of Room in Java
class Room
{
float length;
float breadth;
public void getData(float a, float b)
{
length=a;
breadth=b;
}
}
public class RoomArea{
public static void main(String args[])
{
float area;
Room room1=new Room();
room1.getData(14,10);
area=room1.length * room1.breadth;
System.out.println("Area = "+area);
}
}
{
float length;
float breadth;
public void getData(float a, float b)
{
length=a;
breadth=b;
}
}
public class RoomArea{
public static void main(String args[])
{
float area;
Room room1=new Room();
room1.getData(14,10);
area=room1.length * room1.breadth;
System.out.println("Area = "+area);
}
}
Program to Reading data from Keyboard
import java.io.DataInputStream;
class Reading
{
public static void main(String args[])
{
DataInputStream in =new DataInputStream(System.in);
int intNumber=0;
float floatNumber=0.0f;
try
{
System.out.println("Enter a Integer : ");
intNumber =Integer.parseInt(in.readLine());
System.out.println("Enter a Float number : ");
floatNumber = Float.parseFloat(in.readLine());
}catch(Exception ex)
{
ex.printStackTrace();
}
System.out.println("intNumber = " +intNumber);
System.out.println("floatNumber = "+floatNumber);
}
}
class Reading
{
public static void main(String args[])
{
DataInputStream in =new DataInputStream(System.in);
int intNumber=0;
float floatNumber=0.0f;
try
{
System.out.println("Enter a Integer : ");
intNumber =Integer.parseInt(in.readLine());
System.out.println("Enter a Float number : ");
floatNumber = Float.parseFloat(in.readLine());
}catch(Exception ex)
{
ex.printStackTrace();
}
System.out.println("intNumber = " +intNumber);
System.out.println("floatNumber = "+floatNumber);
}
}
Program In Java which uses commend line arguments as input.
class ComLineTest
{
public static void main(String args[])
{
int count,i=0;
String string;
count=args.length;
System.out.println("Number of arguments = " +count);
while(i {
string=args[i];
i=i+1;
System.out.println(i+" : "+"Java is "+ string+ "!");
}
}
}
//input :- java ComLineTest Simple Object_Oriented Distributed Robust Secure Portable MultiThreaded Dynamic
{
public static void main(String args[])
{
int count,i=0;
String string;
count=args.length;
System.out.println("Number of arguments = " +count);
while(i
string=args[i];
i=i+1;
System.out.println(i+" : "+"Java is "+ string+ "!");
}
}
}
//input :- java ComLineTest Simple Object_Oriented Distributed Robust Secure Portable MultiThreaded Dynamic
Program In Java that will read a float type
/* Its Program that will read a float type value from the keyboard and print the following output.
->Small Integer not less than the number.
->Given Number.
->Largest Integer not greater than the number.
*/
class ValueFormat{
public static void main(String args[])
{
double i = 34.32; //given number
System.out.println("Small Integer not greater than the number : "+Math.ceil(i));
System.out.println("Given Number : "+i);
System.out.println("Largest Integer not greater than the number : "+Math.floor(i));
}
->Small Integer not less than the number.
->Given Number.
->Largest Integer not greater than the number.
*/
class ValueFormat{
public static void main(String args[])
{
double i = 34.32; //given number
System.out.println("Small Integer not greater than the number : "+Math.ceil(i));
System.out.println("Given Number : "+i);
System.out.println("Largest Integer not greater than the number : "+Math.floor(i));
}
Program to display a greet message according to Marks obtained by student.
class SwitchDemo
{
public static void main(String args[])
{
int marks = Integer.parseInt(args[0]); //take marks as command line argument.
switch(marks/10)
{
case 10:
case 9:
case 8:
System.out.println("Excellent");
break;
case 7:
System.out.println("Very Good");
break;
case 6:
System.out.println("Good");
break;
case 5:
System.out.println("Work Hard");
break;
case 4:
System.out.println("Poor");
break;
case 3:
case 2:
case 1:
case 0:
System.out.println("Very Poor");
break;
default:
System.out.println("Invalid value Entered");
}
}
}
{
public static void main(String args[])
{
int marks = Integer.parseInt(args[0]); //take marks as command line argument.
switch(marks/10)
{
case 10:
case 9:
case 8:
System.out.println("Excellent");
break;
case 7:
System.out.println("Very Good");
break;
case 6:
System.out.println("Good");
break;
case 5:
System.out.println("Work Hard");
break;
case 4:
System.out.println("Poor");
break;
case 3:
case 2:
case 1:
case 0:
System.out.println("Very Poor");
break;
default:
System.out.println("Invalid value Entered");
}
}
}
Program to find sum of all integers greater than 100 and less than 200 that are divisible by 7 In Java.
class SumOfDigit
{
public static void main(String args[])
{
int result=0;
for(int i=100;i<=200;i++) {
if(i%7==0)
result+=i;
}
System.out.println("Output of Program is : "+result);
}
}
{
public static void main(String args[])
{
int result=0;
for(int i=100;i<=200;i++) {
if(i%7==0)
result+=i;
}
System.out.println("Output of Program is : "+result);
}
}
program to find SUM AND PRODUCT of a given Digit In Java.
class Sum_Product_ofDigit
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]);
//taking value as command line argument.
int temp = num,result=0;
//Logic for sum of digit
while(temp>0)
{
result = result + temp;
temp--;
}
System.out.println("Sum of Digit for "+num+" is : "+result);
//Logic for product of digit
temp = num;
result = 1;
while(temp > 0)
{
result = result * temp;
temp--;
}
System.out.println("Product of Digit for "+num+" is : "+result);
}
}
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]);
//taking value as command line argument.
int temp = num,result=0;
//Logic for sum of digit
while(temp>0)
{
result = result + temp;
temp--;
}
System.out.println("Sum of Digit for "+num+" is : "+result);
//Logic for product of digit
temp = num;
result = 1;
while(temp > 0)
{
result = result * temp;
temp--;
}
System.out.println("Product of Digit for "+num+" is : "+result);
}
}
Program to Reverse a given number in Java.
class Reverse
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]);
//take argument as command line
int remainder, result=0;
while(num>0)
{
remainder = num%10;
result = result * 10 + remainder;
num = num/10;
}
System.out.println("Reverse number is : "+result);
}
}
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]);
//take argument as command line
int remainder, result=0;
while(num>0)
{
remainder = num%10;
result = result * 10 + remainder;
num = num/10;
}
System.out.println("Reverse number is : "+result);
}
}
Program to generate 5 Random nos. between 1 to 100, and it should not follow with decimal point In Java.
class RandomDemo
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
System.out.println((int)(Math.random()*100));
}
}
}
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
System.out.println((int)(Math.random()*100));
}
}
}
Program to Find Minimum of 2 nos. using conditional operator in Java
class Minof2
{
public static void main(String args[])
{
//taking value as command line argument.
//Converting String format to Integer value
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
int result = (i
System.out.println(result+" is a minimum value");
}
}
{
public static void main(String args[])
{
//taking value as command line argument.
//Converting String format to Integer value
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
int result = (i
System.out.println(result+" is a minimum value");
}
}
Program to Find Maximum of 2 numbers in Java.
class Maxof2
{
public static void main(String args[])
{
//taking value as command line argument.
//Converting String format to Integer value
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
if(i > j)
System.out.println(i+" is greater than "+j);
else
System.out.println(j+" is greater than "+i);
}
}
{
public static void main(String args[])
{
//taking value as command line argument.
//Converting String format to Integer value
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
if(i > j)
System.out.println(i+" is greater than "+j);
else
System.out.println(j+" is greater than "+i);
}
}
program to find Fibonacci series of a given number in Java.
class Fibonacci
{
public static void main(String args[])
{
int num = 5; //taking no. as command line argument.
System.out.println("*****Fibonacci Series*****");
int f1, f2=0, f3=1;
for(int i=1;i<=num;i++)
{
System.out.print(" "+f3+" ");
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
}
****************************************************
O/p :
Input - 8
Output - 1 1 2 3 5 8 13 21
{
public static void main(String args[])
{
int num = 5; //taking no. as command line argument.
System.out.println("*****Fibonacci Series*****");
int f1, f2=0, f3=1;
for(int i=1;i<=num;i++)
{
System.out.print(" "+f3+" ");
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
}
****************************************************
O/p :
Input - 8
Output - 1 1 2 3 5 8 13 21
Program to Find Factorial of Given no In JAVA
class Factorial
{
public static void main(String args[])
{
int num = 5;
int result = 1;
int result2 = 1;
//using while loop
while(num>0)
{
result = result * num;
num--;
}
//using for loop
num = 5;
for(int i=num;i>=1;i--)
{
result2=result2*i;
}
System.out.println("Factorial of Given no. is : "+result);
System.out.println("Factorial of Given no. is : "+result2);
}
}
{
public static void main(String args[])
{
int num = 5;
int result = 1;
int result2 = 1;
//using while loop
while(num>0)
{
result = result * num;
num--;
}
//using for loop
num = 5;
for(int i=num;i>=1;i--)
{
result2=result2*i;
}
System.out.println("Factorial of Given no. is : "+result);
System.out.println("Factorial of Given no. is : "+result2);
}
}
Subscribe to:
Posts (Atom)