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

Core-5 Practical

Java Programs
CSPCB 2021 Batch (Group A)
[1]

No.1 - To find the sum of any number of integers entered as


command line arguments. (14th September 2022)

class CommandArgs

public static void main(String args[])

int a= Integer.parseInt(args[0]);

int b= Integer.parseInt(args[1]);

int sum=a+b;

System.out.println("Sum is "+sum);

OUTPUT
D:\Program>javac CommandArgs.java
D:\Program>java CommandArgs 12 18
Sum is 30

Created by Swadhin Panda


[2]

No.2 - To find the factorial of a given number (16th September 2022)

import java.util.Scanner;
public class factorial
{
public static void main(String fact[])
{
int i,n,f=1;
System.out.println("Enter any number to find factorial");
Scanner s=new Scanner(System.in);
n=s.nextInt();
for( i=n ; i>=1 ; i-- )
{ f=f*i; }
System.out.println("Factorial of given number is "+f);
} }
OUTPUT
D:\Program>javac factorial.java
D:\Program>java factorial
Enter any number to find factorial
7
Factorial of given number is 5040

Created by Swadhin Panda


[3]

No.3 - To convert a decimal to binary (21st September 2022)

import java.util.Scanner;

public class DecimalToBinary

public static void main(String args[])

{ Scanner in=new Scanner(System.in);

int i=1,b=0,rem;

System.out.println("Enter a decimal number");

int num= in.nextInt();

while(num!=0)

{ rem=num%2;

b=b+(i*rem);

num=num/2;

i=i*10;

System.out.println("Binary form is : "+b);

Created by Swadhin Panda


[4]

OUTPUT
D:\Program>javac DecimalToBinary.java
D:\Program>java DecimalToBinary
Enter a decimal number
56
Binary form is : 111000
D:\Program>java DecimalToBinary
Enter a decimal number
39
Binary form is : 100111

Created by Swadhin Panda


[5]

No.4 - To check if a number is prime or not, by taking the number as


input from the keyboard (21st September 2022)

import java.util.Scanner;

public class prime

public static void main(String prm[])

int i,n,c=0;

System.out.println("Enter a number");

Scanner s=new Scanner(System.in);

n=s.nextInt();

for(i=2;i<n;i++)

{ if(n%i==0)

c++; }

if(c==0)

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

else

System.out.println(n+" "+"is not a prime number"); } } }

Created by Swadhin Panda


[6]

OUTPUT
D:\Program>javac prime.java
D:\Program>java prime
Enter a number
92
92 is not a prime number
D:\Program>java prime
Enter a number
5
5 is a prime number

Created by Swadhin Panda


[7]

No.5 - To find the sum of any number of integers interactively, i.e.,


entering every number from the keyboard, whereas the total
number of integers is given as a command line argument (21st
September 22)

class SumAll
{
public static void main(String args[])
{
int i,s=0,l;
l=args.length;
for(i=0;i<l;i++)
{ int n=Integer.parseInt(args[i]);
s=s+n; }
System.out.println("Sum of these numbers : "+" "+s); } }
OUTPUT
D:\Program>javac SumAll.java
D:\Program>java SumAll 32 45 780 12
Sum of these numbers is :
869
D:\Program>java SumAll 4580 6735 765
Sum of these numbers is :
12080

Created by Swadhin Panda


[8]

No.6 - Write a program that shows working of different functions of


String and StringBuffer classes like setCharAt( ), setLength( ), append(
), insert( ), concat( )and equals( ). (23rd September 2022)

public class StringProgram


{
public static void main(String[] args)
{
StringBuffer sb=new StringBuffer("COMPUTER");
StringBuilder stbr=new StringBuilder("Sita is a good girl");

System.out.println("Original String is : "+ stbr);


stbr.setCharAt(0,'R');
System.out.println("After using setCharAt(0,'R') : " +stbr);
stbr.append(true);
System.out.println("After using append :" + stbr);
System.out.println("Original Length : " + sb.length() +
“String :” + sb);
sb.setLength(7);
System.out.println("After using setLength(7) : " +
sb.length() + "String : " +sb);
sb.insert(7,'R');
System.out.println("After using insert(7,'R') : " +sb);

Created by Swadhin Panda


[9]

String str1="SWADHIN" , test="SWADHIN";


String str2="PANDA";
System.out.println(str1.equals(test));
System.out.println(str1.equals(str2));
System.out.println(str1.concat(str2));
}
}
OUTPUT
D:\Program>javac StringProgram.java
D:\Program>java StringProgram
Original String is : Sita is a good girl
After using setCharAt(0,'R') : Rita is a
good girl
After using append :Rita is a good girltrue
Original Length : 8 String : COMPUTER
After using setLength(7) : 7String :
COMPUTE
After using insert(7,'R') : COMPUTER
true
false
SWADHINPANDA

Created by Swadhin Panda


[ 10 ]

No.7 - Write a program to create a – “distance” class with methods


where distance is computed in terms of feet and inches, how to create
objects of a class and to see the use of this pointer (19 Oct 2022)

import java.io.*;

import java.util.Scanner;

class Dist {

int feet, inches;

void getDistance(int feet, int inches)

{ this.feet=feet;

this.inches=inches; }

void showDistance() {

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

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

} }

public class Distance {

public static void main(String args[])

Scanner sc= new Scanner(System.in);

System.out.println("Enter the feet : ");

int x=sc.nextInt();

Created by Swadhin Panda


[ 11 ]

System.out.println("Enter the inches : ");

int y= sc.nextInt();

Dist obj=new Dist();

obj.getDistance(x,y);

sc.close();

obj.showDistance();

OUTPUT
D:\Program>javac Distance.java

D:\Program>java Distance

Enter the feet :

Enter the inches :

feet = 5

inches = 6

Created by Swadhin Panda


[ 12 ]

No.12 - Write a program to demonstrate the concept of boxing and


unboxing. (23 Nov 2022)

import java.io.*;

public class BoxingUnboxing

public static void main(String []args)

Integer i=100; // boxing on int

Int a=i; // unboxing

System.out.println("Value of i = ”+i+ “ and a= "+a+);

OUTPUT
D:\Program>javac BoxingUnboxing.java

D:\Program>java BoxingUnboxing

Value of i= 100 and a= 100

Created by Swadhin Panda


[ 13 ]

15. Write a program that creates illustrates different levels of


protection in classes/subclasses belonging to same package or
different packages(23 Nov 2022)

package protection;

public class Protection

private int x=20;

public int y=30;

protected int z=10;

int getX()

{ return x; }

public static void main(String []args)

addidas ad=new addidas();

ad.display();

class addidas extends Protection

Created by Swadhin Panda


[ 14 ]

public void display()

System.out.println(" The Value of x is\t"+ getX());

System.out.println(" The Value of z is\t"+ z);

OUTPUT
D:\Program>javac Protection.java

D:\Program>java Protection

The Value of x is 20

The Value of z is 10

Created by Swadhin Panda


[ 15 ]

16.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.(29.11.22)

import java.io.*;

import java.util.Scanner;

public class DividebyZero

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter value of a: ");

int a=sc.nextInt();

System.out.println("Enter value of b: ");

int b=sc.nextInt();

int c;

sc.close();

try{

c=a/b;

System.out.println("Value of "+a+"/"+b+"="+c);

Created by Swadhin Panda


[ 16 ]

catch(ArithmeticException e)

{ System.out.println(e); }

OUTPUT
D:\Program>javac DividebyZero.java

D:\Program>java DividebyZero

Enter value of a:

Enter value of b:

Value of 5/2=2

D:\Program>java DividebyZero

Enter value of a:

Enter value of b:

java.lang.ArithmeticException:/by zero

Created by Swadhin Panda

You might also like