1. Write a program to print “Welcome to Java Programming” on the screen.
class welcome
{
public static void main(String a[])
{
System.out.println("Welcome to Java Progamming");
}
}
//2. Write a program to check whether the given two numbers are equal. If not then find the greater of the given two numbers.
import java.lang.*;
class Comparision
{
public static void main(String args[])
{
int x,y;
x=67;
y=97;
if(x==y)
{
System.out.println("Given two number are equal");
}
else if(x>y)
{
System.out.println(x+ " is greater than " +y);
}
else
{
System.out.println(y+ " is greater than " +x);
}
}
}
//3. Using switch and case statements write a program to add, subtract, multiply and divide the two numbers.
import java.io.*;
public class BasicCalc
{
public static void main(String a[])throws IOException
{
DataInputStream in = new DataInputStream(System.in);
System.out.println("What do you want to do? \n 1. Add, \n 2. Substract, \n 3. Multiply, \n 4. Divide");
int i = Integer.parseInt(in.readLine());
//int i=4;
int m, n;
m = 10;
n = 5;
switch (i)
{
case 1:
System.out.println("Result after Adding" + " " + (m + n));
break;
case 2:
System.out.println("Result after Substracting" + " " + (m - n));
break;
case 3:
System.out.println("Result after Multiplying" + " " + (m * n));
break;
case 4:
System.out.println("Result after Dividing" + " " + (m / n));
break;
default:
System.out.println("This is not a valid input parameter");
break;
}
}
}
//4. Using for loop write a program to print the table of a given number on the screen.
import java.util.Scanner;
public class Table
{
public static void main(String args[])
{
int num,i,res;
Scanner s = new Scanner(System.in);
System.out.print("Enter a number : ");
num =s.nextInt();
for(i=1; i<=10; i++)
{
res=i*num;
System.out.println(res);
}
}
}
//5. Write a program to find the even sum and the odd sum in the given array.
class EvenOddSum
{
static int sum1, sum2;
static int n[] ={ 2, 3, 4, 5, 6, 7, 92, 65, 45, 87 };
public static void main(String a[])
{
for (int i = 0; i <>
{
if (n[i] % 2 == 0)
{
System.out.println(n[i] + " " + "*" + " " + "Even");
sum1 = sum1 + n[i];
}
else
{
System.out.println(n[i] + " " + "*" + " " + "Odd");
sum2 = sum2 + n[i];
}
}
System.out.println("sum of even no. is " + sum1);
System.out.println("sum of odd no. is " + sum2);
}
}
//6. Write a program to print all the prime numbers between n and m.
import java.io.*;
class PrimeNumber
{
public static void main(String[] args) throws Exception
{
int i;
BufferedReader bf = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter 1st number: ");
int num = Integer.parseInt(bf.readLine());
System.out.println("Enter 2nd number: ");
int num2 = Integer.parseInt(bf.readLine());
System.out.println("Prime numbers are: ");
for (i = num; i <num2; i++)
{
int j;
for (j=2; j<i; j++)
{
int n = i % j;
if (n == 0)
{
break;
}
}
if (i == j)
{
System.out.print(" " + i);
}
}
}
}
//7. Write a program to add the two matrices.
class MatrixSum
{
public static void main(String[] args)
{
int array[][]= {{4,5,6},{6,8,9}};
int array1[][]= {{5,4,6},{5,6,7}};
System.out.println("Number of Row = " + array.length);
System.out.println("Number of Column = " + array[1].length);
int l= array.length;
System.out.println("Matrix 1 : ");
for(int i = 0; i < l; i++)
{
for(int j = 0; j <= l; j++)
{
System.out.print(" "+ array[i][j]);
}
System.out.println();
}
int m= array1.length;
System.out.println("Matrix 2 : ");
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= m; j++)
{
System.out.print(" "+array1[i][j]);
}
System.out.println();
}
System.out.println("Addition of both matrix : ");
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= m; j++)
{
System.out.print(" "+(array[i][j]+array1[i][j]));
}
System.out.println();
}
}
}
{
public static void main(String a[])
{
System.out.println("Welcome to Java Progamming");
}
}
//2. Write a program to check whether the given two numbers are equal. If not then find the greater of the given two numbers.
import java.lang.*;
class Comparision
{
public static void main(String args[])
{
int x,y;
x=67;
y=97;
if(x==y)
{
System.out.println("Given two number are equal");
}
else if(x>y)
{
System.out.println(x+ " is greater than " +y);
}
else
{
System.out.println(y+ " is greater than " +x);
}
}
}
//3. Using switch and case statements write a program to add, subtract, multiply and divide the two numbers.
import java.io.*;
public class BasicCalc
{
public static void main(String a[])throws IOException
{
DataInputStream in = new DataInputStream(System.in);
System.out.println("What do you want to do? \n 1. Add, \n 2. Substract, \n 3. Multiply, \n 4. Divide");
int i = Integer.parseInt(in.readLine());
//int i=4;
int m, n;
m = 10;
n = 5;
switch (i)
{
case 1:
System.out.println("Result after Adding" + " " + (m + n));
break;
case 2:
System.out.println("Result after Substracting" + " " + (m - n));
break;
case 3:
System.out.println("Result after Multiplying" + " " + (m * n));
break;
case 4:
System.out.println("Result after Dividing" + " " + (m / n));
break;
default:
System.out.println("This is not a valid input parameter");
break;
}
}
}
//4. Using for loop write a program to print the table of a given number on the screen.
import java.util.Scanner;
public class Table
{
public static void main(String args[])
{
int num,i,res;
Scanner s = new Scanner(System.in);
System.out.print("Enter a number : ");
num =s.nextInt();
for(i=1; i<=10; i++)
{
res=i*num;
System.out.println(res);
}
}
}
//5. Write a program to find the even sum and the odd sum in the given array.
class EvenOddSum
{
static int sum1, sum2;
static int n[] ={ 2, 3, 4, 5, 6, 7, 92, 65, 45, 87 };
public static void main(String a[])
{
for (int i = 0; i <>
{
if (n[i] % 2 == 0)
{
System.out.println(n[i] + " " + "*" + " " + "Even");
sum1 = sum1 + n[i];
}
else
{
System.out.println(n[i] + " " + "*" + " " + "Odd");
sum2 = sum2 + n[i];
}
}
System.out.println("sum of even no. is " + sum1);
System.out.println("sum of odd no. is " + sum2);
}
}
//6. Write a program to print all the prime numbers between n and m.
import java.io.*;
class PrimeNumber
{
public static void main(String[] args) throws Exception
{
int i;
BufferedReader bf = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter 1st number: ");
int num = Integer.parseInt(bf.readLine());
System.out.println("Enter 2nd number: ");
int num2 = Integer.parseInt(bf.readLine());
System.out.println("Prime numbers are: ");
for (i = num; i <num2; i++)
{
int j;
for (j=2; j<i; j++)
{
int n = i % j;
if (n == 0)
{
break;
}
}
if (i == j)
{
System.out.print(" " + i);
}
}
}
}
//7. Write a program to add the two matrices.
class MatrixSum
{
public static void main(String[] args)
{
int array[][]= {{4,5,6},{6,8,9}};
int array1[][]= {{5,4,6},{5,6,7}};
System.out.println("Number of Row = " + array.length);
System.out.println("Number of Column = " + array[1].length);
int l= array.length;
System.out.println("Matrix 1 : ");
for(int i = 0; i < l; i++)
{
for(int j = 0; j <= l; j++)
{
System.out.print(" "+ array[i][j]);
}
System.out.println();
}
int m= array1.length;
System.out.println("Matrix 2 : ");
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= m; j++)
{
System.out.print(" "+array1[i][j]);
}
System.out.println();
}
System.out.println("Addition of both matrix : ");
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= m; j++)
{
System.out.print(" "+(array[i][j]+array1[i][j]));
}
System.out.println();
}
}
}
//8. Write a class circle which consists of functions getdata() and area(), and also write a main program to create a circle object and to find the area by calling the function area () in circle class and display the result on the screen.
import java.lang.*;
import java.io.*;
class Circle
{
double r,a;
static final double p=3.14;
public void getData()
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the radius of the circle :");
r=Double.parseDouble(br.readLine());
}
catch(Exception e)
{
}
}
public void area()
{
a=p*(r*r);
System.out.print("The area of the circle is :"+a);
}
public static void main(String args[])
{
Circle c=new Circle();
c.getData();
c.area();
}
}
//9. Write a class Box with the variable width, depth and height and constructor to assigning the values for these variables and a method to find the volume of the box and also write the main program, which creates the box object, and find the volume of the box. Print the result on the screen.
class classBox
{
static int width, depth, height;
dimention()
{
width = 10;
depth = 5;
height = 20;
}
void volume()
{
int box = width * depth * height;
System.out.println("volume of box is :" + " " + box);
}
public static void main(String args[])
{
dimention box = new dimention();
box.volume();
}
}
//10. Write a program to demonstrate the method overloading for sum ( ) function.
class demo
{
void add(int a)
{
System.out.println(a);
}
void add(int x, int y)
{
System.out.println(x + y);
}
public static void main(String args[])
{
demo q = new demo();
q.add(10);
q.add(20, 30);
}
}
//11. Write a program to demonstrate the overriding of constructor methods for the program no. 9.
class A
{
String s = "Ravish";
void display()
{
System.out.println(s);
}
}
class B extends A
{
void display()
{
System.out.println(s + " " + "Kumar");
}
public static void main(String args[])
{
B over = new B();
over.display();
}
}
//12. Write a program to concatenate two strings
class Con
{
public static void main(String a[])
{
String a1 = "Vikash";
String a2 = "Sharma";
System.out.println(a1.concat(" " + a2));
}
}
//13. Write a program to find whether a given string is a palindrome or not.
class Palin
{
public static void main(String []s)
{
int i=0,j;
j=s[0].length()-1;
while(i<j)
{
if(s[0].charAt(i) != s[0].charAt(j))
break;
i++;
j--;
}
if(i<j)
System.out.println("Not palin");
else
System.out.println("Palin");
}
}
//14. Write a program to change the case of the given string.
import java.io.*;
class case
{
public static void main(String ar[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter any String");
System.out.println("and to quit write “ Exit“ ");
System.out.println("\n");
do
{
str = br.readLine();
int textLength = str.length();
for (int i = 0; i <>
{
char ch = Character.toUpperCase(str.charAt(i));
System.out.print(ch);
}
} while (!str.equals("exit"));
}
}
//15. Write a program to create a thread by extending the thread class.
import java.lang.*;
class Child extends Thread
{
Child()
{
super("Thread Demo");
System.out.println("Child Thread :" +this);
start();
}
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ExtendThread
{
public static void main(String args[])
{
new Child();
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1500);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
No comments:
Post a Comment