Q.1 To find reverse using while loop.
import [Link].*;
class Q1
{
public static void main (String args[])
{
int num,rev,rem;
Scanner sc=new Scanner([Link]);
[Link]("Enter an number:");
num=[Link]();
rev=0;
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
[Link]("Reversed number is :"+rev);
}
}
Q.2 WAP using conditional operator to compare three numbers.
import [Link].*;
class Q2
{
public static void main (String args[])
{
int n1,n2,n3,greatest;
Scanner sc=new Scanner([Link]);
[Link]("Enter three number:");
n1=[Link]();
n2=[Link]();
n3=[Link]();
greatest=n1>n2 && n1>n3 ? n1 : n2>n1 && n2>n3 ?n2 : n3;
[Link]("Greatest number is :"+greatest);
}
}
Q.4. Constructor overloading.
import [Link].*;
class Area
{
int l,b,r;
Area(int x)
{
r=x;
}
Area(int x,int y)
{
l=x;
b=y;
}
int rectArea()
{
return(l*b);
}
double circleArea()
{
return(3.14*r*r);
}
public static void main(String args[])
{
int len,breadth,radius;
Scanner sc=new Scanner([Link]);
[Link]("Enter length and breadth of the rectangle");
len=[Link]();
breadth=[Link]();
[Link]("Enter radius of the circle");
radius=[Link]();
Area rect = new Area(len,breadth);
Area circle= new Area(radius);
[Link]("Area of rectangle is "+[Link]());
[Link]("Area of circle is "+[Link]());
}}
Q.5. WAP using command line arguments to accept integer double string and
Boolean.
import [Link].*;
class Q5
{
public static void main (String args[])
{
int num1=[Link](args[0]);
double num2=[Link](args[1]);
String s=args[2];
boolean b=[Link](args[3]);
[Link]("Entered integer is: " +num1);
[Link]("Entered double is: " +num2);
[Link]("Entered string is: " +s);
[Link]("Entered integer is: " +b);
}
}
Q.6 Program to find factorial of number accepted using buffered reader.
import [Link].*;
class Q6
{
public static void main(String[] args) throws IOException
{
int fact,i;
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter a number: ");
int number = [Link]([Link]());
fact=1;
for(i=1;i<=number;i++)
{
fact=fact*i;
}
[Link]("Factorial of " +number+ " is " +fact);
}
}
Q.7. Method overloading.
import [Link].*;
class Q7
{
double calArea(int x, int y)
{
return(x*y*0.5);
}
int calArea(int x)
{
return(x*x);
}
public static void main(String args[])
{
int base,h,side;
Scanner sc=new Scanner([Link]);
[Link]("Enter base and height of the triangle");
base=[Link]();
h=[Link]();
[Link]("Enter side of the square");
side=[Link]();
Q7 a = new Q7();
[Link]("Area of tringle is "+[Link](base,h));
[Link]("Area of square is "+[Link](side));
}
}
Q.8 Sorting of array.
import [Link].*;
class Q8
{
public static void main (String args[])
{
int arr[]={55,20,87,90,12,8,16,76,33,45};
int i,j,temp;
[Link]("Original array:");
for(i=0;i<10;i++)
{
[Link](arr[i]+",");
}
for(i=0;i<10;i++)
{
for(j=9;j>i;j--)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
} } }
[Link]("\nSorted array:");
for(i=0;i<10;i++)
{
[Link](arr[i]+",");
} }}
Q.9. ADDITION OF MATRICES
import [Link].*;
class Q9
{
public static void main(String args[])
{
int i,j;
Scanner sc=new Scanner([Link]);
//MATRIX1
int a[][]=new int[3][3];
[Link]("matrix A(even numbers)");
[Link]("Enter elements:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
a[i][j]=[Link]();
if(a[i][j]%2!=0)
{
[Link]("Please enter an even number:");
a[i][j]=[Link]();
}
}
}
//MATRIX2
int b[][]=new int[3][3];
[Link]("Matrix B(Odd Numbers");
[Link]("Enter element:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
b[i][j]=[Link]();
if(b[i][j]%2==0)
{
[Link]("Please enter an odd number:");
b[i][j]=[Link]();
}
}
}
//DISPLAY MATRICES
[Link]("1st matrix:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
[Link](a[i][j]+" ");
}
[Link]();
}
[Link]("2nd matrix:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
[Link](b[i][j]+" ");
}
[Link]();
}
//ADDITION
int sum[][]=new int[3][3];
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
sum[i][j]=a[i][j]+b[i][j];
}
}
//DISPLAY ADDITION
[Link]("SUM:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
[Link](sum[i][j] +" ");
}
[Link]();
}
}
}
Q.10. SWITCH STATEMENT.
import [Link].*;
class Q10
{
public static void main(String args[])
{
int ch;
Scanner sc=new Scanner([Link]);
[Link]("Enter the number of the month:");
ch=[Link]();
switch(ch)
{
case 1:
[Link]("JAN");
break;
case 2:
[Link]("FEB");
break;
case 3:
[Link]("MARCH");
break;
default:
[Link]("Month number should be between 1 to 12");
break;
}
}
}
Q.11. SINGLE LEVEL INHERITANCE.
import [Link].*;
class Typist
{
protected String name;
protected int code;
protected String address;
protected float speed;
protected float salary;
public void display()
{
[Link]("Teacher's
Details:\nName:"+name+"\nCode:"+code+"\nAddress:"+address+"\nSpeed:"+speed+"\nSalary:"+sal
ary);
}}
class Regular extends Typist
{
public void read()
{
Scanner sc= new Scanner([Link]);
[Link]("Enter name, code, address, speed and Salary of the regular typist:");
name=[Link]();
code=[Link]();
address=[Link]();
speed=[Link]();
salary=[Link]();
}}
class Q11
{
public static void main(String args[])
{
Regular r=new Regular();
[Link]();
[Link]();
}}
Q.12. PACKAGE
package multiplication;
public class Table
{
int i;
public void calTable(int n)
{
for(i=1;i<=10;i++)
{
[Link](n*i);
}
}
}
MAIN PROGRAM:
import [Link].*;
import [Link];
class Table2
{
public static void main(String args[])
{
int num;
Scanner sc=new Scanner([Link]);
[Link]("Enter any number");
num=[Link]();
Table t1=new Table();
[Link](num);
}
}
Q.13 AREA OF CIRCLE N RECTANGLE USING INTERFACE
import [Link].*;
interface Area
{
double area();
}
class Circle implements Area
{
double radius;
Circle(double radius)
{
[Link] = radius;
}
@Override
public double area()
{
return radius*radius*3.14;
}}
class Rectangle implements Area
{
double width, height;
Rectangle(double width, double height)
{
[Link] = width;
[Link] = height;
}
@Override
public double area()
{
return width*height;
}}
class Q13
{
public static void main(String args[])
{
double r,x,y;
Scanner sc=new Scanner([Link]);
[Link]("Enter radius of the circle:");
r=[Link]();
Circle circle=new Circle(r);
[Link]("Area of the circle :" +[Link]());
[Link]("Enter width and height of the rectangle:");
x=[Link]();
y=[Link]();
Rectangle rectangle = new Rectangle(x,y);
[Link]("Area of the rectangle :" +[Link]());
}}
Q14. MULTITHREADING – PRINTING THREE DIFFERNET VALUES OF I J K
class Thread1 extends Thread
{
public void run()
{
int i=1;
while(i<4)
{
[Link]("Value of i = "+i);
i++;
}}}
class Thread2 extends Thread
{
public void run()
{
int j=4;
while(j<7)
{
[Link]("Value of j = "+j);
j++;
}}}
class Thread3 extends Thread
{
public void run()
{
int k=7;
while(k<10)
{
[Link]("Value of k = "+k);
k++;
}}}
public class Q14
{
public static void main(String[] args)
{
Thread1 t1=new Thread1();
Thread2 t2=new Thread2();
Thread3 t3=new Thread3();
[Link]();
[Link]();
[Link]();
}
}
Q.15. APPLET – PRINT WELCOME AT CENTER
import [Link];
import [Link].*;
/*
<applet code="[Link]" width="600" height="600">
</applet>
*/
public class Welcome extends Applet {
public void paint(Graphics g) {
// Get the dimensions of the applet window
int width = getWidth();
int height = getHeight();
// Define the message to display
String message = "Welcome";
// Calculate the width and height of the string in pixels
int stringWidth = [Link]().stringWidth(message);
int stringHeight = [Link]().getHeight();
// Draw the message at the calculated center coordinates
[Link](message, (width - stringWidth) / 2, (height + stringHeight) / 2);
} }
Q.17 (3) – CLASSES AND OBJECTS
import [Link].*;
class Bank
{
String name;
String acc_type;
int acc_no;
double balance=0;
Scanner sc=new Scanner([Link]);
void getData()
{
[Link]("Enter your name:");
name=[Link]();
[Link]("Enter your account number:");
acc_no=[Link]();
[Link]();
[Link]("Enter your account type:");
acc_type=[Link]();
}
void deposit()
{
double amount;
[Link]("Enter the amount to be deposited:");
amount=[Link]();
balance=balance+amount;
[Link]("Amount deposited successfully!");
[Link]("Current Balance: "+balance);
}
void withdrawl()
{
double amount;
[Link]("Enter the amount to be withdrawn:");
amount=[Link]();
if(amount>balance)
{
[Link]("You have insufficient balance");
}
else
{
balance=balance-amount;
[Link]("Transaction Successful!");
[Link]("Balance before transaction: "+(balance+amount));
[Link]("Balance after transaction: "+balance);
}
}
}
class BankAccount
{
public static void main (String args[])
{
int ch;
Scanner sc=new Scanner([Link]);
Bank b1=new Bank();
[Link]();
do
{
[Link]("Enter your choice : [Link] [Link]");
ch=[Link]();
switch(ch)
{
case 1:
[Link]();
break;
case 2:
[Link]();
break;
default:
[Link]("Invalid input");
}
}while(ch!=0);
}
}
Q18. WAP TO SWAP TWO STRINGS.
import [Link].*;
class Q18
{
public static void main (String args[])
{
String s1,s2,temp;
Scanner sc=new Scanner([Link]);
[Link]("Enter first string:");
s1=[Link]();
[Link]("Enter second string:");
s2=[Link]();
temp=s1;
s1=s2;
s2=temp;
[Link]("After swapping, strings are: First string = "+s1+" Second string = "+s2);
}
}
Q.19. PATTERN
import [Link].*;
class Q19
{
public static void main (String
args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
[Link](i);
}
[Link]();
}
}
}
Q.20. TRUTH TABLE FOR LOGICAL OPERATORS.
import [Link].*;
class Q20
{
public static void main (String args[])
{
Boolean r,s;
[Link](" R \t S \t AND \t OR \t XOR \t NOT");
r= true;
s=true;
[Link]( r + "\t" + s + "\t" + (r&&s) + "\t" +(r||s) + "\t"+ (r^s) + "\t" + (!r));
r=true;
s=false;
[Link]( r + "\t" + s + "\t" + (r&&s) + "\t" +(r||s) + "\t"+ (r^s) + "\t" + (!r));
r=false;
s=true;
[Link]( r + "\t" + s + "\t" + (r&&s) + "\t" +(r||s) + "\t"+ (r^s) + "\t" + (!r));
r=false;
s=false;
[Link]( r + "\t" + s + "\t" + (r&&s) + "\t" +(r||s) + "\t"+ (r^s) + "\t" + (!r));
}}
Q22. WAP TO CALCULATE SUM AND REVERSE
import [Link].*;
class Q22
{
public static void main (String args[])
{
int num,rev,rem,sum,num1;
Scanner sc=new Scanner([Link]);
[Link]("Enter an number:");
num=[Link]();
num1=num;
rev=0;
do
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}while(num!=0);
[Link]("Reversed number is :"+rev);
sum=0;
do
{
rem=num1%10;
sum=sum+rem;
num1=num1/10;
}while(num1!=0);
[Link]("Sum is :"+sum);
} }
Q25. APPLETS – HORIZONTAL LINE
import [Link].*;
import [Link].*;
/*
<applet code="[Link]" width="600"
height="200">
</applet>
*/
public class Q25 extends Applet
{
public void paint(Graphics g)
{
[Link]([Link]);
[Link](10,20,200,20);
}}
Q.26 – STAR PATTERN
import [Link].*;
class Q26
{
public static void main (String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
[Link]("*");
}
[Link]();
}
}
}
Q.27. STAR PATTERN USING APPLET
import [Link];
import [Link].*;
/*
<applet code="[Link]" width="600"
height="200">
</applet>
*/
public class Q27 extends Applet {
public void paint(Graphics g) {
int x= 80;
int y= 80;
[Link]("/", x, y);
for (int i = 1; i < 30; i++) {
[Link]("*", x + (i * 10), y); // Draw each star with same spacing
}
[Link]("/", x + (300), y);
}
}
Q.24 – MULTITHREADING TO ASSIGN PRIORITY TO THREADS.
class Thread1 extends Thread
{
public void run()
{
[Link]([Link]().getName() +"Thread with MINIMUM priority");
}
}
class Thread2 extends Thread
{
public void run()
{
[Link]([Link]().getName() +"Thread with MAXIMUM priority");
}
}
class Thread3 extends Thread
{
public void run()
{
[Link]([Link]().getName() +"Thread with NORMAL priority");
}
}
public class Q24
{
public static void main(String[] args)
{
Thread1 t1=new Thread1();
Thread2 t2=new Thread2();
Thread3 t3=new Thread3();
[Link]("Thread-1");
[Link]("Thread-2");
[Link]("Thread-3");
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MAX_PRIORITY);
[Link](Thread.NORM_PRIORITY);
[Link]();
[Link]();
[Link]();
}
}