CBCS5thSEMBCAJava Manual
CBCS5thSEMBCAJava Manual
VIJAYANAGARA
BENGALURU-560104
LAB MANUAL
2022-2023
INDEX
PAGE
S.NO PROGRAM NAME
NO.
PART-A
Write a program to find factorial of list of number reading input as
1 1
command line argument.
2 Write a program to display all prime numbers between two limits. 2
Write a program to sort list of elements in ascending and descending
3 3
order and show the exception handling.
4 Write a program to implement all string operations. 5
PART - A
1. Write a program to find factorial of list of number reading input as command line
argument.
Source Code
OUTPUT:
class Prime
{
public static void main(String args[])
{
int i,j;
if(args.length<2)
{
System.out.println("No command line Argruments ");
return;
}
int num1=Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);
System.out.println("Prime number between"+num1+"and" +num2+" are:");
for(i=num1;i<=num2;i++)
{
for(j=2;j<i;j++)
{
int n=i%j;
if(n==0)
{
break;
}
}
if(i==j)
{
System.out.println(" "+i);
}
}
}
}
OUTPUT:
3. Write a program to sort list of elements in ascending and descending order and
show the exception handling.
class Sorting
{
public static void main(String args[])
{
int a[] = new int[5];
try
{
for(int i=0;i<5;i++)
a[i]=Integer.parseInt(args[i]);
System.out.println("Before Sorting\n");
for(int i=0;i<5;i++)
System.out.println(" " + a[i]);
bubbleSort(a,5);
System.out.println("\n\n After Sorting\n");
System.out.println("\n\nAscending order \n");
for(int i=0;i<5;i++)
System.out.print(" "+a[i]);
System.out.println("\n\nDescending order \n");
for(int i=4;i>=0;i--)
System.out.print(" "+a[i]);
}
catch(NumberFormatException e)
{
System.out.println("Enter only integers");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Enter only 5 integers");
}
}
private static void bubbleSort(int [] arr, int length)
{
int temp,i,j;
for(i=0;i<length-1;i++)
{
for(j=0;j<length-1-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
}
OUTPUT:
Source code
class StringOperation
{
public static void main(String args[])
{
String s1="Hello";
String s2="World";
System.out.println("The strings are "+s1+"and"+s2);
int len1=s1.length();
int len2=s2.length();
System.out.println("The length of "+s1+" is :"+len1);
System.out.println("The length of "+s2+" is :"+len2);
System.out.println("The concatenation of two strings = "+s1.concat(s2));
System.out.println("First character of "+s1+"is="+s1.charAt(0));
System.out.println("The uppercase of "+s1+"is="+s1.toUpperCase());
System.out.println("The lower case of "+s2+"is="+s2.toLowerCase());
System.out.println(" the letter e occurs at position"+s1.indexOf("e")+"in"+s1);
System.out.println("Substring of "+s1+"starting from index 2 and ending at 4 is =
"+s1.substring(2,4));
System.out.println("Replacing 'e' with 'o' in "+s1+"is ="+s1.replace('e','o'));
boolean check = s1.equals(s2);
if(check==false)
System.out.println(""+s1+" and "+s2+" are not same");
else
System.out.println("" + s1+" and " + s2+"are same");
}
}
OUTPUT:
Source code:
import java.io.*;
class Area
{
public static double circleArea(double r)
{
return Math.PI*r*r;
}
public static double squareArea(double side)
{
return side*side;
}
public static double rectArea(double width, double height)
{
return width*height;
}
public static double triArea(double base, double height1)
{
return 0.5*base*height1;
}
public static String readLine()
{
String input=" ";
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try
{
input = in.readLine();
}
catch(Exception e)
{
System.out.println("Error" + e);
}
return input;
}
public static void main(String args[])
{
System.out.println("Enter the radius");
Double radius=Double.parseDouble(readLine());
System.out.println("Area of circle = " + circleArea(radius));
System.out.println("Enter the side");
Double side=Double.parseDouble(readLine());
System.out.println("Area of square = "+squareArea(side));
System.out.println("Enter the Width");
Double width=Double.parseDouble(readLine());
System.out.println("Enter the height");
Double height=Double.parseDouble(readLine());
System.out.println("Area of Rectangle = " + rectArea(width,height));
System.out.println("Enter the Base");
Double base=Double.parseDouble(readLine());
System.out.println("Enter the Height");
Double height1=Double.parseDouble(readLine());
System.out.println("Area of traingle ="+triArea(base,height1));
}
}
OUTPUT:
OUTPUT:
}
public void paint(Graphics g)
{
g.drawString(heading,30,30);
g.drawString(rno,30,80);
g.drawString(course,30,100);
g.drawString(sem,30,120);
g.drawString(sub1,30,140);
g.drawString(sub2,30,160);
g.drawString(avg,30,180);
}
}
Output:
OUTPUT:
Output:
Source code
import java.awt. *;
import java.awt.event.*;
import java.applet.*;
Output:
Output:
import java.io.*;
import java.io.DataInputStream;
class Room
{
float length,breadth;
Room(int a,int b)
{
length=a; breadth =b;
}
Room(int a)
{
length =a; breadth=100;
}
float getdata()
{
return(length*breadth);
}
}
class Lab11
{
public static void main(String args[])
{
float area;
int l=0,b=0;
DataInputStream in=new DataInputStream(System.in);
try
{
System.out.println("\n enter the value for Room length:");
l=Integer.parseInt(in.readLine());
System.out.println("\n enter the value for Room breadth:");
b=Integer.parseInt(in.readLine());
}
catch(Exception e)
{
}
Room room1=new Room(l,b);
area=room1.getdata();
System.out.println("\n length="+room1.length);
System.out.println("\n breadth="+room1.breadth);
System.out.println("\n Area="+area);
System.out.println("\n passing only length of the room with breadth=100");
Room room2=new Room(l);
area =room2.getdata();
System.out.println("\n length="+room2.length);
System.out.println("\n breadth="+room2.breadth);
System.out.println("\n Area="+area);
}
}
OUTPUT
OUTPUT:
import java.io.*;
class palind
{
public static void main(String[] args) throws IOException
{
int n,m,r,s=0;
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Enter the number");
n=Integer.parseInt(in.readLine());
m=n;
while(n!=0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(s==m)
{
System.out.println("The given number is palindrome"+s);
}
else
System.out.println("the given number is not palindrome"+s);
Output:
Enter the number
343
The given number is palindrome
4. Write a program to implement Rhombus pattern reading the limit form user.
Source code
import java.io.*;
public class RhombusPattern
{
public static void main(String args[]) throws IOException
{
int i,j,limit;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the limit");
limit=Integer.parseInt(br.readLine());
for(i=1;i<=limit;i++)
{
for(j=limit-i;j>0;j--)
System.out.print(" ");
for(j=1;j<=2*i-1;j++)
System.out.print("*");
System.out.println();
}
for(i=limit-1;i>=1;i--)
{
for(j=1;j<=limit-i;j++)
System.out.print(" ");
for(j=1;j<=2*i-1;j++)
System.out.print("*");
System.out.println();
}
}
}
OUTPUT:
import java.io.*;
class odd
{
public static void main(String[] args)throws IOException
{
int n;
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Enter a number");
n=Integer.parseInt(in.readLine());
if(n%2==1)
System.out.println("Number is odd="+n);
else
System.out.println("Number is even="+n);
}}
Output:
Enter a number
7
Number is odd=7
Output:
Thread A is started:
From thread A: i=1
From thread A: i=2
From thread A: i=3
From thread A: i=4
From thread A: i=5
Exit from thread A:
import java.io.*;
class Income
{
float iamount;
class Intsample
OUTPUT:
8. /*Write a Java program to implement the concept of importing classes from user
defined package and creating packages.*/
Output:
regno = 123
name = xyz
import java.lang.*;
class Exception_handle
{
public static void main(String argv[])
{
int a=10,b=5,c=5,x,y;
try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println("DIVISION BY ZERO");
}
y=a/(b+c);
System.out.println("y="+y);
}
}
Output:
DIVISION BY ZERO
y=1
OUTPUT:
66. Which class should you use to obtain design information about an object?
The Class class is used to obtain information about an object's design and java.lang.Class class instance
represent classes, interfaces in a running Java application.
67. What is the difference between static and non-static variables?
A static variable is associated with the class as a whole rather than with specific instances of a class. Non-
static variables take on unique values with each object instance.
68. What is Serialization and deserialization?
Serialization is the process of writing the state of an object to a byte stream. Deserialization is the
process of restoring these objects.
69. What's the difference between constructors and other methods?
Constructors must have the same name as the class and can not return a value. They are only called once
while regular methods could be called many times.
70. What's the difference between the methods sleep() and wait()?
The code sleep(2000); puts thread aside for exactly two seconds. The code wait(2000), causes a wait of up
to two second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method
wait() is defined in the class Object and the method sleep() is defined in the class
Thread.
71. What is the Collections API?
The Collections API is a set of classes and interfaces that support operations on collections of
objects.
72. Which class is the immediate superclass of the Container class?
Component class is the immediate super class.
73. What is the difference between an Interface and an Abstract class?
An abstract class can have instance methods that implement a default behavior. An Interface can
only declare constants and instance methods, but cannot implement default behavior and all
methods are implicitly abstract. An interface has all public members and no implementation.
What will happen if static modifier is removed from the signature of the main method?
Program throws "NoSuchMethodError" error at runtime.
74. What is the difference between error and an exception?
An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. Exceptions are
conditions that occur because of bad input etc. e.g., FileNotFoundException will be thrown if the specified
file does not exist.
75. What is Dynamic Binding (late binding)?
Binding refers to the linking of a procedure call to the code to be executed in response to the call.
Dynamic binding means that the code associated with a given procedure call is not known until the time of
the call at run-time.
76. What is type casting?
Type casting means treating a variable of one type as though it is another type.
77. Where import statement is used in a Java program?
Import statement is allowed at the beginning of the program file after package statement.
78. Life cycle of an applet includes which steps?
Life cycle involves the following steps −
Initialization
Starting
Stopping
Destroying
Painting
79. Why is the role of init() method under applets?
It initializes the applet and is the first method to be called.