0% found this document useful (0 votes)
21 views

Java Pracs

Uploaded by

hitesh.patil24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Java Pracs

Uploaded by

hitesh.patil24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Q.1 To find reverse using while loop.

import java.util.*;
class Q1
{
public static void main (String args[])
{
int num,rev,rem;
Scanner sc=new Scanner(System.in);
System.out.println("Enter an number:");
num=sc.nextInt();
rev=0;
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
System.out.println("Reversed number is :"+rev);
}
}
Q.2 WAP using conditional operator to compare three numbers.

import java.util.*;
class Q2
{
public static void main (String args[])
{
int n1,n2,n3,greatest;
Scanner sc=new Scanner(System.in);
System.out.println("Enter three number:");
n1=sc.nextInt();
n2=sc.nextInt();
n3=sc.nextInt();
greatest=n1>n2 && n1>n3 ? n1 : n2>n1 && n2>n3 ?n2 : n3;

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


}
}
Q.4. Constructor overloading.

import java.util.*;
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(System.in);
System.out.println("Enter length and breadth of the rectangle");
len=sc.nextInt();
breadth=sc.nextInt();
System.out.println("Enter radius of the circle");
radius=sc.nextInt();
Area rect = new Area(len,breadth);
Area circle= new Area(radius);
System.out.println("Area of rectangle is "+rect.rectArea());
System.out.println("Area of circle is "+circle.circleArea());
}}
Q.5. WAP using command line arguments to accept integer double string and
Boolean.

import java.util.*;
class Q5
{
public static void main (String args[])
{
int num1=Integer.parseInt(args[0]);
double num2=Double.parseDouble(args[1]);
String s=args[2];
boolean b=Boolean.parseBoolean(args[3]);
System.out.println("Entered integer is: " +num1);
System.out.println("Entered double is: " +num2);
System.out.println("Entered string is: " +s);
System.out.println("Entered integer is: " +b);
}
}
Q.6 Program to find factorial of number accepted using buffered reader.

import java.io.*;
class Q6
{
public static void main(String[] args) throws IOException
{
int fact,i;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter a number: ");
int number = Integer.parseInt(br.readLine());
fact=1;
for(i=1;i<=number;i++)
{
fact=fact*i;
}
System.out.println("Factorial of " +number+ " is " +fact);
}
}
Q.7. Method overloading.

import java.util.*;
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(System.in);
System.out.println("Enter base and height of the triangle");
base=sc.nextInt();
h=sc.nextInt();
System.out.println("Enter side of the square");
side=sc.nextInt();
Q7 a = new Q7();
System.out.println("Area of tringle is "+a.calArea(base,h));
System.out.println("Area of square is "+a.calArea(side));
}
}
Q.8 Sorting of array.

import java.util.*;
class Q8
{
public static void main (String args[])
{
int arr[]={55,20,87,90,12,8,16,76,33,45};
int i,j,temp;
System.out.println("Original array:");
for(i=0;i<10;i++)
{
System.out.print(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;
} } }
System.out.println("\nSorted array:");
for(i=0;i<10;i++)
{
System.out.print(arr[i]+",");
} }}
Q.9. ADDITION OF MATRICES

import java.util.*;
class Q9
{
public static void main(String args[])
{
int i,j;
Scanner sc=new Scanner(System.in);
//MATRIX1
int a[][]=new int[3][3];
System.out.println("matrix A(even numbers)");
System.out.println("Enter elements:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
a[i][j]=sc.nextInt();
if(a[i][j]%2!=0)
{
System.out.println("Please enter an even number:");
a[i][j]=sc.nextInt();
}

}
}

//MATRIX2
int b[][]=new int[3][3];
System.out.println("Matrix B(Odd Numbers");
System.out.println("Enter element:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
b[i][j]=sc.nextInt();
if(b[i][j]%2==0)
{
System.out.println("Please enter an odd number:");
b[i][j]=sc.nextInt();
}
}
}
//DISPLAY MATRICES
System.out.println("1st matrix:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("2nd matrix:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
//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
System.out.println("SUM:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(sum[i][j] +" ");
}
System.out.println();
}
}
}
Q.10. SWITCH STATEMENT.

import java.util.*;
class Q10
{
public static void main(String args[])
{
int ch;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of the month:");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("JAN");
break;
case 2:
System.out.println("FEB");
break;
case 3:
System.out.println("MARCH");
break;
default:
System.out.println("Month number should be between 1 to 12");
break;
}
}
}
Q.11. SINGLE LEVEL INHERITANCE.

import java.util.*;
class Typist
{
protected String name;
protected int code;
protected String address;
protected float speed;
protected float salary;
public void display()
{
System.out.println("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(System.in);
System.out.println("Enter name, code, address, speed and Salary of the regular typist:");
name=sc.next();
code=sc.nextInt();
address=sc.next();
speed=sc.nextFloat();
salary=sc.nextFloat();
}}
class Q11
{
public static void main(String args[])
{
Regular r=new Regular();
r.read();
r.display();
}}
Q.12. PACKAGE

package multiplication;
public class Table
{
int i;
public void calTable(int n)
{
for(i=1;i<=10;i++)
{
System.out.println(n*i);
}
}
}

MAIN PROGRAM:

import java.util.*;
import multiplication.Table;
class Table2
{
public static void main(String args[])
{
int num;
Scanner sc=new Scanner(System.in);
System.out.println("Enter any number");
num=sc.nextInt();
Table t1=new Table();
t1.calTable(num);
}
}
Q.13 AREA OF CIRCLE N RECTANGLE USING INTERFACE

import java.util.*;
interface Area
{
double area();
}
class Circle implements Area
{
double radius;
Circle(double radius)
{
this.radius = radius;
}
@Override
public double area()
{
return radius*radius*3.14;
}}
class Rectangle implements Area
{
double width, height;
Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
@Override
public double area()
{
return width*height;
}}
class Q13
{
public static void main(String args[])
{
double r,x,y;
Scanner sc=new Scanner(System.in);
System.out.println("Enter radius of the circle:");
r=sc.nextDouble();
Circle circle=new Circle(r);
System.out.println("Area of the circle :" +circle.area());
System.out.println("Enter width and height of the rectangle:");
x=sc.nextDouble();
y=sc.nextDouble();
Rectangle rectangle = new Rectangle(x,y);
System.out.println("Area of the rectangle :" +rectangle.area());
}}
Q14. MULTITHREADING – PRINTING THREE DIFFERNET VALUES OF I J K

class Thread1 extends Thread


{
public void run()
{
int i=1;
while(i<4)
{
System.out.println("Value of i = "+i);
i++;
}}}

class Thread2 extends Thread


{
public void run()
{
int j=4;
while(j<7)
{
System.out.println("Value of j = "+j);
j++;
}}}

class Thread3 extends Thread


{
public void run()
{
int k=7;
while(k<10)
{
System.out.println("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();

t1.start();
t2.start();
t3.start();
}
}
Q.15. APPLET – PRINT WELCOME AT CENTER

import java.applet.Applet;
import java.awt.*;

/*
<applet code="Welcome.class" 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 = g.getFontMetrics().stringWidth(message);
int stringHeight = g.getFontMetrics().getHeight();

// Draw the message at the calculated center coordinates


g.drawString(message, (width - stringWidth) / 2, (height + stringHeight) / 2);
} }
Q.17 (3) – CLASSES AND OBJECTS

import java.util.*;
class Bank
{
String name;
String acc_type;
int acc_no;
double balance=0;
Scanner sc=new Scanner(System.in);
void getData()
{

System.out.println("Enter your name:");


name=sc.nextLine();
System.out.println("Enter your account number:");
acc_no=sc.nextInt();
sc.nextLine();
System.out.println("Enter your account type:");
acc_type=sc.nextLine();
}
void deposit()
{
double amount;

System.out.println("Enter the amount to be deposited:");


amount=sc.nextFloat();
balance=balance+amount;
System.out.println("Amount deposited successfully!");
System.out.println("Current Balance: "+balance);
}
void withdrawl()
{
double amount;
System.out.println("Enter the amount to be withdrawn:");
amount=sc.nextFloat();
if(amount>balance)
{
System.out.println("You have insufficient balance");
}
else
{
balance=balance-amount;
System.out.println("Transaction Successful!");
System.out.println("Balance before transaction: "+(balance+amount));
System.out.println("Balance after transaction: "+balance);
}
}
}
class BankAccount
{
public static void main (String args[])
{
int ch;
Scanner sc=new Scanner(System.in);
Bank b1=new Bank();
b1.getData();
do
{
System.out.println("Enter your choice : 1.Deposit 2.Withdrawl");
ch=sc.nextInt();
switch(ch)
{
case 1:
b1.deposit();
break;
case 2:
b1.withdrawl();
break;
default:
System.out.println("Invalid input");
}
}while(ch!=0);
}
}
Q18. WAP TO SWAP TWO STRINGS.

import java.util.*;
class Q18
{
public static void main (String args[])
{

String s1,s2,temp;

Scanner sc=new Scanner(System.in);


System.out.println("Enter first string:");
s1=sc.nextLine();
System.out.println("Enter second string:");
s2=sc.nextLine();
temp=s1;
s1=s2;
s2=temp;
System.out.println("After swapping, strings are: First string = "+s1+" Second string = "+s2);
}
}

Q.19. PATTERN

import java.util.*;
class Q19
{
public static void main (String
args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
Q.20. TRUTH TABLE FOR LOGICAL OPERATORS.

import java.util.*;

class Q20
{
public static void main (String args[])
{
Boolean r,s;
System.out.println(" R \t S \t AND \t OR \t XOR \t NOT");
r= true;
s=true;
System.out.println( r + "\t" + s + "\t" + (r&&s) + "\t" +(r||s) + "\t"+ (r^s) + "\t" + (!r));
r=true;
s=false;
System.out.println( r + "\t" + s + "\t" + (r&&s) + "\t" +(r||s) + "\t"+ (r^s) + "\t" + (!r));
r=false;
s=true;
System.out.println( r + "\t" + s + "\t" + (r&&s) + "\t" +(r||s) + "\t"+ (r^s) + "\t" + (!r));
r=false;
s=false;
System.out.println( r + "\t" + s + "\t" + (r&&s) + "\t" +(r||s) + "\t"+ (r^s) + "\t" + (!r));
}}
Q22. WAP TO CALCULATE SUM AND REVERSE

import java.util.*;
class Q22
{
public static void main (String args[])
{
int num,rev,rem,sum,num1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter an number:");
num=sc.nextInt();
num1=num;
rev=0;
do
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}while(num!=0);
System.out.println("Reversed number is :"+rev);
sum=0;
do
{
rem=num1%10;
sum=sum+rem;
num1=num1/10;
}while(num1!=0);
System.out.println("Sum is :"+sum);
} }

Q25. APPLETS – HORIZONTAL LINE

import java.applet.*;
import java.awt.*;

/*
<applet code="Q25.class" width="600"
height="200">
</applet>
*/

public class Q25 extends Applet


{
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
g.drawLine(10,20,200,20);
}}
Q.26 – STAR PATTERN

import java.util.*;
class Q26
{
public static void main (String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}

Q.27. STAR PATTERN USING APPLET

import java.applet.Applet;
import java.awt.*;

/*
<applet code="Q27.class" width="600"
height="200">
</applet>
*/

public class Q27 extends Applet {


public void paint(Graphics g) {

int x= 80;
int y= 80;
g.drawString("/", x, y);
for (int i = 1; i < 30; i++) {
g.drawString("*", x + (i * 10), y); // Draw each star with same spacing
}
g.drawString("/", x + (300), y);

}
}
Q.24 – MULTITHREADING TO ASSIGN PRIORITY TO THREADS.

class Thread1 extends Thread


{
public void run()
{
System.out.println(Thread.currentThread().getName() +"Thread with MINIMUM priority");
}
}

class Thread2 extends Thread


{
public void run()
{
System.out.println(Thread.currentThread().getName() +"Thread with MAXIMUM priority");
}
}

class Thread3 extends Thread


{
public void run()
{
System.out.println(Thread.currentThread().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();

t1.setName("Thread-1");
t2.setName("Thread-2");
t3.setName("Thread-3");

t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.NORM_PRIORITY);

t1.start();
t2.start();
t3.start();
}
}

You might also like