Vous êtes sur la page 1sur 15

1.Write a program to print “ Welcome to Java Programming” on the screen.

class Qno1sol

public static void main(String a[])

System.out.println("Welcome to Java Programming");

2.Write a program to find the greatest of the given three numbers.

import java.io.*;

class Qno2sol

public static void main(String args[])

try

DataInputStream ds=new DataInputStream(System.in);

System.out.println("Enter Your Three Numbers");

int a=Integer.parseInt(ds.readLine());

int b=Integer.parseInt(ds.readLine());

int c=Integer.parseInt(ds.readLine());

if(a>b )

System.out.println( a+" "+"is greatest number");


}

if(b>c )

System.out.println(b+" "+"is greatest number");

if(c>a )

System.out.println(c+" "+"is greatest number");

catch(IOException e)

System.out.println("There is problem"+e);

3.Using switch and case statements write a program to add, subtract , multiply and divide the
two numbers.

import java.io.*;

public class Qno3sol

public static void main(String a[])throws IOException

DataInputStream in=new DataInputStream(System.in);

System.out.println("Enter Switch Cases");


int i=Integer.parseInt(in.readLine());

//int i=4;

int m,n;

m=10;

n=5;

switch(i)

case 1:

System.out.println("Addition"+" "+(m+n));

break;

case 2:

System.out.println("Substration"+" "+(m-n));

break;

case 3:

System.out.println("Multiplication"+" "+(m*n));

break;

case 4:

System.out.println("Division"+" "+(m/n));

break;

default:

System.out.println("This is not a valid input parameter");

break;

}
4.Using for loop write a program to add the integers up to 50 and print the result on the
screen.

class Qno4sol

static int i,SUM;

public static void main(String a[])

for(int i=1;i<=50;i++)

System.out.println(i);

SUM=SUM+i;

System.out.println("Additional value of 50 integers"+" "+SUM);

5.Write a program to find the largest element in the given array.

class Qno5sol

Qno5sol()

int n[]={2,3,4,5,6,7,92,65,45,87};

int a,max,min,b;

max=n[0];

min=n[0];
for(int i=1;i<10;i++)

if(n[i]>max)

max=n[i];

if(n[i]<min)

min=n[i];

System.out.println("The maximum number is "+max);

System.out.println("The maximum number is "+min);

public static void main(String a[])

new Qno5sol();

6.Write a program to find the even sum and the odd sum in the given array.

class Qno6sol

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<10;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("The summesion of even number is "+sum1);

System.out.println("The summesion of odd number is "+sum2);

7.Write a program to add the two matrices.

class Qno7sol

//int c[3][3]=new int[3,3];

//int y[3][3]

int x[][]={{1,2,3},{4,5,6},{7,8,9}};

int y[][]={{11,12,13},{14,15,16},{17,18,19}};

int a[][];

int b[][];
static int c[][];

Qno7sol()

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

System.out.print( x[i][j]+" ");

System.out.println();

System.out.println("*****First Matrix*****");

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

System.out.print(y[i][j]+" ");

System.out.println();

System.out.println("*****Second Matrix*****");

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

System.out.print(x[i][j]+y[i][j]+" ");

System.out.println();

System.out.println("*****Additional Matrix*****");

}
public static void main(String args[])

new Qno7sol();

8.Write a class rectangle which consists of functions getdata() and area(), and also write a
main program to create a rectangle object and to find the area by calling the function area ()
in rectangle class and display the result on the screen.

import java.io.*;

class Qno8sol

int height,width;

public void getData()

try

DataInputStream ds =new DataInputStream(System.in);

System.out.println("Enter the rectangle height");

height=Integer.parseInt(ds.readLine());

System.out.println("Enter the rectangle width");

width=Integer.parseInt(ds.readLine());

catch(IOException ex)

{
System.out.println("There is some error"+ex);

public void area()

System.out.println("The area of given data is "+"\n"+"Area of a reactangle is "+" "+"="+"


"+"Height"+"x"+"Width"

+"\n"+"height is"+" "+height+" "+"x"+" "+"width is"+" "+width+" "+"="+" "+(height*width));

for(int i=0;i<height;i++)

System.out.println("|");

for(int i=0;i<width;i++)

System.out.print("-");

System.out.println("|");

System.out.print("-");

public static void main(String a[])

Qno8sol sol=new Qno8sol();

sol.getData();

sol.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 Qno9sol

static int width,depth,height;

Qno9sol()

width=10;

depth=5;

height=20;

void volume()

int box=width*depth*height;

System.out.println("The volume of the box is"+" "+box);

public static void main(String args[])

Qno9sol box=new Qno9sol();

box.volume();

10.Write a program to demonstrate the method overloading for add ( ) functions.


class Qno10sol

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[])

Qno10sol q=new Qno10sol();

q.add(10);

q.add(20,30);

11.Write a program to demonstrate the method overriding.

class a

String s="Nitesh";

void display()

System.out.println(s);
}

class Qno11sol extends a

void display()

System.out.println(s+" "+"Gaurav");

public static void main(String args[])

Qno11sol n=new Qno11sol();

n.display();

12.Write a program to concatenate two strings .

class Qno12sol

public static void main(String a[])

String a1="Nitesh";

String a2="Gaurav";

System.out.println(a1.concat(" "+a2));

}
13.Write a program to find whether two given strings are equal or not and print the result on
the screen.

import java.io.*;

class Qno13sol

static String s1,s2;

public static void main(String a[]) throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter your first string value");

s1=br.readLine();

System.out.println(s1);

System.out.println("Enter your first string value");

s2=br.readLine();

System.out.println(s2);

if(s1.equals(s2))

System.out.println("Strings are equal");

else

System.out.println("Strings are not equal");

14.Write a program to change the case of the given string.

import java.io.*;

class Qno14sol
{

public static void main(String ar[]) throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str;

System.out.println("Enter your lines");

System.out.println("----------******----------");

System.out.println("Enter 'exit' to quit");

System.out.println("----------******----------");

System.out.println("\n");

do

str=br.readLine();

int textLength=str.length();

for(int i=0;i<textLength;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.

class a extends Thread

static int i;

public void run()

for(i=0;i<=5;i++)

System.out.println("\t from Thread A :i="+i+"\007");

System.out.println("Exit form A ");

public class Qno15sol

public static void main(String a[])

new a().start();

Vous aimerez peut-être aussi