Java Practicle File Bpibs
Java Practicle File Bpibs
BPIBS
BCA-4TH SEM
SOURCE CODE
Import.java.io.*
public class hello{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
OUTPUT
EXPERIMENT-2
AIM – Write a program to calculate simple interest
and input should be by the user.
SOURCE CODE
import java.util.Scanner;
public class SI {
public static void main(String[] args) {
float si,principle,rate,time;
System.out.println(" Program to Calculate Simple
Interest ");
Scanner sc = new Scanner(System.in);
System.out.print("~ Enter Principle Amount > ");
principle = sc.nextFloat();
System.out.print("~ Enter Rate of Interest > ");
rate = sc.nextFloat();
System.out.print("~ Enter Time > ");
time = sc.nextFloat();
si = (principle*rate*time)/100;
System.out.println("Simple Interest = "+si);
}
}
OUTPUT
EXPERIMENT-3
AIM – Write a program to find the average and
sum of n numbers using command line
argument.
SOURCE CODE
public class avg {
public static void main(String[] args) {
int sum = 0;
for(int i=0;i<args.length;i++)
{
sum +=Integer.parseInt(args[i]);
}
int average = (sum / args.length);
System.out.println("Sum = "+sum);
System.out.println("Average of " + args.length + "
command line arguments is " +average);
}
}
OUTPUTEXPERIMENT-4
AIM – Write a program to find the number of
arguments provided at command line.
SOURCE CODE
public class argument {
public static void main(String[] args) {
int count=0;
for (int i = 0; i < args.length; i++) {
count=args.length;
}
System.out.println("Number of Arguments Passed
are "+count);
}
}
OUTPUTEXPERIMENT-5
AIM – Write a program to find GCD of a number
input by the user.
SOURCE CODE
import java.util.Scanner;
public class gcd {
public static void main(String[] args) {
int x=0, y=0, gcd = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Program to Find GCD");
System.out.print("Enter Integer [1] > ");
x=sc.nextInt();
System.out.print("Enter Integer [2] > ");
y=sc.nextInt();
for(int i = 1; i <= x && i <= y; i++)
{
if(x%i==0 && y%i==0)
gcd = i;
}
System.out.println("GCD of "+ x +" and "+ y+" is:
"+gcd);
}
}
OUTPUTEXPERIMENT-6
AIM – Write a program to create a simple class to
find out the area and perimeter of
rectangle.
SOURCE CODE
import java.util.Scanner;
public class rectangle {
public static void main(String[] args) {
int length, breadth, perimeter, area;
Scanner s = new Scanner(System.in);
System.out.print("Enter length of rectangle:");
length = s.nextInt();
System.out.print("Enter breadth of rectangle:");
breadth = s.nextInt();
perimeter = 2 * (length + breadth);
System.out.println("Perimeter of
rectangle:"+perimeter);
area = length * breadth;
System.out.println("Area of rectangle:"+area);
}
}
OUTPUTEXPERIMENT-7
AIM – Write a program to find sum of elements of
a one dimensional array entered
dynamically by the user.
SOURCE CODE
import java.util.*;
public class sumof1darray {
public static void main(String[] args) {
int size=0,sum=0;
Scanner sc = new Scanner(System.in);
System.out.println("Program to Find Sum of
Elements of Array");
System.out.print(" ~ Enter Size of Array > ");
size = sc.nextInt();
int[] arr = new int[size];
for (int i = 0; i < arr.length; i++) {
System.out.print("~ Enter Element ["+i+"] > ");
arr[i]=sc.nextInt();
sum +=arr[i];}
System.out.println("Elements of Array");
for (int i = 0; i < arr.length; i++) {
System.out.print(" "+arr[i]);}
System.out.println("\nSum of Array = "+sum);}}
OUTPUTEXPERIMENT-8
AIM – Write a program to find multiplication of
two 3X3 matrices. Data should be
entered by user.
SOURCE CODE
import java.util.*;
public class matrix {
public static void main(String[] args) {
int[][] matrixA = new int[3][3];
int[][] matrixB = new int[3][3];
int[][] matrixC = new int[3][3];
Scanner sc = new Scanner(System.in);
System.out.println("Program For Matrix
Multiplication");
System.out.println("Enter Elements for Matrix
[A]");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("~ Enter Element
["+i+"]"+"["+j+"] > ");
matrixA[i][j]=sc.nextInt();}}
System.out.println("Enter Elements for Matrix
[B]");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("~ Enter Element
["+i+"]"+"["+j+"] > ");
matrixB[i][j]=sc.nextInt();}}
System.out.println(" ~ Elements of Matrix [A]");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(" "+matrixA[i][j]);}
System.out.println(" ");}
System.out.println(" ~ Elements of Matrix [B]");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {System.out.print("
"+matrixB[i][j]);}
System.out.println(" ");}
System.out.println("Multiplying the matrices...");
for (int i = 0; i <3; i++){
for (int j = 0; j < 3; j++){
for (int k = 0; k < 3; k++){
matrixC[i][j] = matrixC[i][j] + matrixA[i][k] *
matrixB[k][j];}}}
System.out.println(" ~ Product of Matrix [A] and
Matrix [B] is");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(" "+matrixC[i][j]);}
System.out.println(" ");}}}
OUTPUTEXPERIMENT-9
AIM – Write a program to demonstrate type
casting.
SOURCE CODE
public class type{
public static void main(String[] args){
int i = 200;
long l = i;
float f = l;
System.out.println("\n~ Automatic Type
Conversion ~");
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
System.out.println("\n~ Explicit Type Casting ~");
double d = 200.06;
long la = (long)d;
int ia = (int)la;
byte b=(byte)(d);
System.out.println("Double Data type value "+d);
System.out.println("Long Data type value "+la);
System.out.println("Int Data type value "+ia);
System.out.println("Byte Data type value "+b);}}
OUTPUTEXPERIMENT-10
AIM –
SOURCE CODE
OUTPUTEXPERIMENT-11
AIM –
SOURCE CODE
OUTPUTEXPERIMENT-12
AIM – Write a program “DivideByZero” that takes
two numbers a and b as input,
computes a/b, and invokes Arithmetic Exception to
generate a message when the
denominator is zero.
SOURCE CODE
import java.util.Scanner;
public class DivideByZero {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Input A: ");
int a=input.nextInt();
System.out.print("Enter Input B: ");
int b=input.nextInt();
try{
int result =a/b;
System.out.println("a/b = "+a+"/"+b+" = "+result);
}catch (ArithmeticException e) {
System.out.println("Division by zero.");
System.out.println("Exception - "+e);}}}
OUTPUTEXPERIMENT-13
AIM – Write a program to show the use of nested
try statements that emphasizes the
sequence of checking for catch handler statements.
SOURCE CODE
public class nestedtry {
public static void main(String args[]) {
try {
int a = args.length;
int b = 42 / a;
System.out.println("a = " + a);
try {
if(a==1) a = a/(a-a);
if(a==2) {
int c[] = { 1 };
c[42] = 99;}}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds: " +
e);}
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);}}}
OUTPUTEXPERIMENT-14
AIM – Write a program to create your own
exception types to handle situation specific
to your application (Hint: Define a subclass of
Exception which itself is a subclass of
Throwable).
SOURCE CODE
class MyException extends Exception {
private int detail;
MyException(int a) {
detail = a;
}
public String toString() {
return "MyException[" + detail + "]";}}
public class ExceptionDemo {
static void compute(int a) throws MyException {
System.out.println("~ Called compute(" + a + ")");
if(a > 10)
throw new MyException(a);
System.out.println("~ Normal exit ~");
}
public static void main(String args[]) {
try {
compute(1);
compute(9);
compute(20);
} catch (MyException e) {
System.out.println("~ Caught " +
e);}}}OUTPUTEXPERIMENT-15
AIM – Write a program to create your own
exception to check whether the string
entered at command line is valid or invalid. A
string will be valid if the string value is “
Hello Java”
SOURCE CODE
OUTPUTEXPERIMENT-15
AIM –
SOURCE CODE
OUTPUT