Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

JAVA PRACTICAL FILE

Anmoldeep Singh

Submitted to: Dr Amitpal Singh


Class: B.Tech CSE 6th Sem.
17032001550
1. TO CALCULATE AREA OF A CIRCLE

import java.util.Scanner;

public class NewClass {

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

int a;

double area;

final double pi=3.14578;

System.out.println(“ENTER THE RADIUS OF THE CIRCLE);

a=sc.nextInt();

area=pi*a*a;

System.out.println("Area of circle with radius

"+a+" is given as :"+area);

OUTPUT:

ENTER THE RADIUS OF THE CIRCLE 12

Area of circle with radius 12 is given as :452.9923199999999


2. CALCULATE EXPONENT (M RAISED TO POWER N)

import java.lang.Math;

import java.util.Scanner;

class EXPONENTATION

public static void main(String args[])

{
Scanner sc=new Scanner(System.in);
int a,b;

double result;

System.out.println("ENTER IST INTEGER”);

a=sc.nextInt();

System.out.println("ENTER 2nd INTEGER”);

b=sc.nextInt();

result=Math.pow(a,b);

System.out.println("The result of exponentation is :"+result);

OUTPUT:

ENTER IST INTEGER 5

ENTER 2ND INTEGER 4

The result of exponentation is : 625.0


3. FIND GREATER OF THREE NUMBERS

class GRT_3_NOS {

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int a,b,c;

a=sc.nextInt();

b=sc.nextInt();

c=sc.nextInt();

if((a==b)&&(a==c))

System.out.println("All numbers are equal");

else if(a>b)

if(a>c)

System.err.println("Greater number is :"+a);

else

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

else

if(b>c)

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

else

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

}
OUTPUT:

45

99

88

Greater number is : 99
4. Find greater of three numbers using ternary operator

import java.util.Scanner;

class GRTER _3_NOS {

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int a,b,c,d;

a=sc.nextInt(); b=sc.nextInt();

c=sc.nextInt();

d=(a>b)?((a>c)?a:c):((b>c)?b:c);

System.out.println("Greater number is :"+d);

OUTPUT:

55

77

11

Greater number is :77


5. Calculate percentage(%) of a student in three subjects

import java.util.Scanner;

public class percentage {

public static void main(String args[])

Scanner sc=new Scanner(System.in);

double a,b,c,percent;

a=sc.nextDouble(); b=sc.nextDouble();

c=sc.nextDouble();

percent=((a+b+c)/300)*100;

if(percent>=75)

System.out.println("Distinction scored");

else if((percent>=65)&&(percent<75))

System.out.println("IST DIVISION SCORED");

else if((percent>=55)&&(percent<65))

System.out.println("2ND DIVISION SCORED");

else if((percent>=35)&&(percent<55))

System.out.println("3RD DIVISION SCORED");

else

System.out.println("student failed");

}
OUTPUT:

99

88

77

Distinction scored

55

44

66

2ND DIVISION SCORED


6. Find area of a rectangle using class

import java.util.Scanner;

public class rectangle_area {

public static void main(String args[])

rect r=new rect();

r.getdata();

class rect

Scanner sc=new Scanner(System.in);

int l,b,area;

void getdata()

System.out.println("Enter the length of rectangle :");

l=sc.nextInt();

System.out.println("Enter the breadth of rectangle :");

b=sc.nextInt();

area=l*b;

System.out.println("The area of rectangle is : "+area);

}
OUTPUT:

Enter the length of rectangle :

55

Enter the breadth of rectangle :

44

The area of rectangle is : 2420


7. Generate electric bill of a person under the following rules
a. a)for first 100 units rs. 1/unit.
b. b)for next 200 units rs.2/unit .
c. c)for further units rs.3/unit.

import java.util.Scanner;

public class electric_bill

int units,code;

int r;

String name;

void getdata()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the name");

name=sc.next();

System.out.println("Enter the code");

code=sc.nextInt();

System.out.println("Enter the units");

units=sc.nextInt();

int calculate()

int u;

if(units<=100)

r=units*1;

else if((units<=300)&&(units>100))
{

u=units-100;

r=100+u*2;

else if(units>300)

u=units-300;

r=500+u*3;

else

System.out.println("Enter correct units");

System.out.println("\t\t\tELECTRIC BILL");

System.out.println(" ");

System.out.println("\tNAME :"+name);

System.out.println("\tCODE :"+code);

System.out.println("\tUNITS CONSUMED :"+units);

System.out.println(" ");

System.out.println("\tTOTAL AMOUNT TO BE PAID :"+r);

return 0;

class Show

public static void main(String args[])

int rs;

electric_bill e=new electric_bill();


e.getdata();

rs=e.calculate();

OUTPUT:

Enter the name PRACTICAL

Enter the code 66

Enter the units 2041

ELECTRIC BILL

NAME :PRACTICAL

CODE :66

UNITS CONSUMED :2041

TOTAL AMOUNT TO BE PAID :5723


8. Show concept of switch statement

import java.util.Scanner;

public class CustomCalculator {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter first operand: ");


double a = sc.nextDouble();

System.out.print("Enter second operand: ");


double b = sc.nextDouble();

System.out.println("Choose a number for corresponding operation.");


System.out.println("1: Addition \n 2: Subtraction \n 3: Multiplication \n 4: Division");
int op = sc.nextInt();

switch (op) {
case 1:
double add = a +b;
System.out.println("Addition is: "+ add);
break;

case 2:
double sub = a - b;
System.out.println("Subtraction is: "+ sub);
break;

case 3:
double mult = a * b;
System.out.println("Multiplication is: "+ mult);
break;

case 4:
double div = a/b;
System.out.println("Division is: "+div);
break;

default:
System.out.println("Operation not found.");
break;
}
}
}
OUTPUT:
Enter first operand: 89
Enter second operand: 20
Choose a number for corresponding operation.1: Addition
2: Subtraction
3: Multiplication
4: Division
2
Subtraction is: 69.0
9. Use break and continue statement

public class break_continue {

public static void main(String args[])

int i;

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

System.out.println("i="+i);

if(i==5)

break;

System.out.println("======================================================");

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

if(i==5)

continue;

System.out.println("i="+i);

}
OUTPUT:

BREAK STATEMENT

i=0

i=1

i=2

i=3

i=4

i=5

======================================================

CONTINUE STATEMENT

i=0

i=1

i=2

i=3

i=4

i=6

i=7

i=8

i=9

i=10
10. Generate fibonacii series

import java.util.Scanner;

public class fibonaci {

public static void main(String args[])

int a=0,b=1,sum=0,limit,i=1;

System.out.println("Enter the limit upto which series is to be printed :");

Scanner sc=new Scanner(System.in);

limit=sc.nextInt();

System.out.print("\tTHE SERIES IS AS FOLLOW :");

System.out.print("0");

System.out.print("\t1");

while(i!=limit-1)

sum=a+b;

a=b;

b=sum;

System.out.print("\t"+sum);

i++;

System.out.println("");

OUTPUT:

Enter the limit upto which series is to be printed : 10

THE SERIES IS AS FOLLOW :0 1 1 2 3 5 8 13 21 34


11. Convert binary no. into decimal no.

import java.util.Scanner;

import java.lang.Math;

public class bin_to_dec

public static void main(String args[])

int bnum,dnum=0,rem,i=0;

Scanner sc=new Scanner(System.in);

System.out.println("ENTER ANY BINARY NUMBER");

bnum=sc.nextInt();

int num=bnum;

while(bnum!=0)

rem=bnum%10;

dnum=dnum+rem*(int)Math.pow(2,i);

bnum=bnum/10;

i++;

System.out.print("DECIMAL NUMBER EQUIVQLENT TO "+num);

System.out.print(" IS "+dnum);

System.out.println();

} }

OUTPUT:

ENTER ANY BINARY NUMBER

101010111

DECIMAL NUMBER EQUIVQLENT TO 101010111 IS 343


12. Check whether character is vowel or not

import java.util.Scanner;
public class Main
{

public static void main(String args[])

Scanner sc=new Scanner(System.in); String ch;

System.out.println("Enter an Alphabet"); ch=sc.next();


switch(ch)

case "A":

case "a":

case "E":

case "e":

case "I":

case "i":

case "O":

case "o":

case "U":

case "u":
System.out.println("its an vowel");
break;
default:
System.out.println("its not an vowel");

}
OUTPUT:

Enter an Alphabet A

its an vowel

Enter an Alphabet B

its not an vowel

Enter an Alphabet E

its an vowel

Enter an Alphabet O

its an vowel

Enter an Alphabet Z

its not an vowel


13. Overlad a method with different number of arguments

import java.util.Scanner; import java.lang.Math;


class area_overload

int area(int l,int b)

int area=l*b; return area;


}

int area(int s)

int area=(int)Math.pow(s,2); return area;


}

double area(int a,int b,int c)

double s=(a+b+c)/2;

int s1=(int) (s*((s-a)*(s-b)*(s-c))); double area=(double)Math.sqrt(s1);


return area;

public class Main

public static void main(String args[])

Scanner sc=new Scanner(System.in);

area_overload a=new area_overload();

System.out.println("ENTER THE LENGTH AND BREADTH OF RECTANGLE :");

int l=sc.nextInt(); int b=sc.nextInt();

System.out.println("ENTER THE SIDE OF SQUARE :");

int s=sc.nextInt();
System.out.println("ENTER THE THREE SIDES OF TRIANGLE :");

int A=sc.nextInt(); int B=sc.nextInt(); int C=sc.nextInt();

int a1=a.area(l,b); int a2=a.area(s);

double a3=a.area(A,B,C);

System.out.println("AREA OF RECTANGLE IS CALCULATED AS :"+a1);

System.out.println("AREA OF SQUARE IS CALCULATED AS :"+a2);

System.out.println("AREA OF TRIANGLE IS CALCULATED AS :"+a3);

}
}
OUTPUT:

ENTER THE LENGTH AND BREADTH OF RECTANGLE :

10

ENTER THE SIDE OF SQUARE :

15

ENTER THE THREE SIDES OF TRIANGLE :

10

15

20

AREA OF RECTANGLE IS CALCULATED AS :50

AREA OF SQUARE IS CALCULATED AS :225

AREA OF TRIANGLE IS CALCULATED AS :60.794736614282655


14. Concept of static variable and static method

public class static_method

static double radius=100,area;

final static double pi=3.14578;

static void display()

static_method.area=(double)(pi*static_method.radius*static_method.radius);

System.out.println("THE AREA OF CIRCLE IS : "+static_method.area);

class circle

public static void main(String args[])

static_method s=new static_method();

static_method.display();

OUTPUT:

THE AREA OF CIRCLE IS : 31457.799999999996


15. Default constructor

public class def_constructor

int a,b,c,area; final double pi=3.14;

def_constructor()

a=10;

b=20;

void area()

area=a*b;

System.out.println("AREA = "+area);

class Showing{

public static void main(String args[]) {

def_constructor dc=new def_constructor();

dc.area();

}}

OUTPUT:

AREA = 200

You might also like