LAB MANUAL
1. a) Write a java program that displays welcome to follow by user
name. Accept user name from the user.
Program:
import [Link].*;
class Welcome{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("enter a User name:");
String str=[Link]();
[Link]("Welcome "+str);
Output:
1 b) Write a java program that prompts the user for an integer and then
prints out all the prime numbers up to that integer.
Program:
import [Link];
class PrimeNumber
public static void main(String[] args)
{
int n,count;
Scanner s=new Scanner([Link]);
[Link]("Enter number :");
n=[Link]();
for(int i=2;i<=n;i++)
{
count=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
count=1;
}
if(count==0){
[Link](" "+i);
Output:
2. a) Write a java program to create a class Rectangle. The class has
attributes Length and Width. It should have methods that
calculate Area and Perimeter of the Rectangle. It should have read
attributes () method to read Length and Width from the user.
Program:
import [Link];
class Rectangle
{
double length,width;
void read_attributes()
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the length of Rectangle:");
length = [Link]();
[Link]("Enter the width of Rectangle:");
width = [Link]();
void area()
{
double area=length*width;
[Link]("Area of Rectangle ="+area);
}
void perimeter()
{
double perimeter=2*(length+width);
[Link]("perimeter of Rectangle ="+perimeter);
class CalRectangle{
public static void main (String[] args)
{
Rectangle rec=new Rectangle();
rec.read_attributes();
[Link]();
[Link]();
}
}
Output:
2 b) The Fibonacci sequence is defined by the following rule: The first
two values in the sequence are1 and 1. Every subsequent value is
the sum of the two values preceding it.
Program:
import [Link].*;
class Fibo
{
public static void main(String args[])
{
int c=1,n;
Scanner sc = new Scanner([Link]);
[Link]("Enter a number:");
n=[Link]();
int a=1;
int b=0;
[Link]("Fibonacci series upto "+n+" is :-");
while(c<=n)
{
[Link](c+" ");
a=b;
b=c;
c=a+b;
}
}
}
Output:
3 a) Write a java program that uses both Recursive and Non-Recursive
functions to find the factorial of a given number.
Non-Recursive:
Program:
import [Link];
class Factorial_non
{
public static void main(String[] args)
{
int n, mul = 1;
Scanner s = new Scanner([Link]);
[Link]("Enter any integer:");
n = [Link]();
for(int i = 1; i <= n; i++)
{
mul = mul * i;
}
[Link]("Factorial of "+n+" :"+mul);
}
}
Output::
Recursive:
Program:
import [Link];
class Factorial{
public static void main(String args[]){
//Scanner object for capturing the user input
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number:");
//Stored the entered value in variable
int num = [Link]();
//Called the user defined function fact
int factorial = fact(num);
[Link]("Factorial of entered number is: "+factorial);
}
static int fact(int n)
{
int output;
if(n==1){
return 1;
}
//Recursion: Function calling itself!!
output = fact(n-1)* n;
return output;
}
}
Output:
3 b) Write a java program that checks whether the given string is
Palindrome or not. Ex:MALAYALAM is a Palindrome.
Program:
import [Link].*;
public class Palindromes
{
public static void main(String args[])
{
String str1, str2 = "";
Scanner s = new Scanner([Link]);
[Link]("Enter the string:");
str1 = [Link]();
int n = [Link]();
for(int i = n - 1; i >= 0; i--)
{
str2 = str2 + [Link](i);
}
if([Link](str2))
{
[Link]("The string is palindrome.");
}
else
{
[Link]("The string is not palindrome.");
}
}
}
Output:
4. A) Write a java program to illustrate method overloading and method
overriding.
Method Overloading
Program:
class OverloadDemo {
void test() {
[Link]("No parameters");
}
void test(int a) {
[Link]("a: " + a);
}
void test(int a, int b) {
[Link]("a and b: " + a + " " + b);
}
double test(double a) {
[Link]("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
[Link]();
[Link](10);
[Link](10, 20);
result = [Link](123.25);
[Link]("Result of [Link](123.25): " + result);
}
}
Output:
Method overriding
Program:
class Vehicle{
void run(){
[Link]("Vehicle is running");
}
}
class Bike extends Vehicle{
void run()
{
[Link]("Bike is running safely");
}
}
class Override{
public static void main(String args[])
{
Bike obj = new Bike();
[Link]();
}
}
Output:
B) a java program that illustrates how java achieved Run Time
Polymorphism.
Program:
class A {
void callme() {
[Link]("Inside A's callme method");
}
}
class B extends A {
void callme() {
[Link]("Inside B's callme method");
}
}
class C extends A {
void callme() {
[Link]("Inside C's callme method");
}
}
class Override{
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
A r;
r = a;
[Link]();
r = b;
[Link]();
r = c;
[Link]();
}
}
Output:
5. A) Write a java program to demonstrate the use of subclass.
Program:
class Superclass{
int a ,b;
Superclass() {
a=10;
b=20;
}
}
class Subclass extends Superclass{
int c;
void display() {
[Link]("values in subclass:a="+a+" b="+b);
}
void add()
{
c=a+b;
[Link]("addition of values in subclass:"+c);
}
class Demo_inherit{
public static void main(String args[]) {
Subclass ob=new Subclass();
[Link]();
[Link]();
}
}
Output:
B) Write a java program for abstract class to find areas of different
shapes.
Program:
import [Link];
abstract class Shape_area {
abstract void rectangle(double l, double b);
abstract void square(double s);
abstract void circle(double r);
}
class Shape extends Shape_area{
void rectangle(double l, double b)
{
double area = l*b;
[Link]("Area of Rectangle: "+area);
}
void square(double s)
{
double area = s*s;
[Link]("Area of Square: "+area);
}
void circle(double r)
{
double area = 3.14*r*r;
[Link]("Area of Circle: "+area);
}
}
class Calcarea {
public static void main(String args[])
{
double l, b, h, r, s;
Shape area = new Shape();
Scanner get = new Scanner([Link]);
[Link]("\nEnter Length & Breadth of Rectangle: ");
l = [Link]();
b = [Link]();
[Link](l, b);
[Link]("\nEnter Side of a Square: ");
s = [Link]();
[Link](s);
[Link]("\nEnter Radius of Circle: ");
r = [Link]();
[Link](r);
}
}
Output:
6. Write a Java program to implement the concept of importing classes
from user defined package and creating packages
GRectangle,java
package Geometry;
public class GRectangle
{
public void area(double length,double width)
{
[Link]("Area of Rectangle :"+(length*width));
}
}
[Link]
package Geometry;
public class GSquare
{
public void area(double length)
{
[Link]("Area of Rectangle :"+(length*length));
}
}
Demo_packages.java
import Geometry.*;
class Demo_packages
{
public static void main(String args[])
{
GRectangle rec=new GRectangle();
[Link](10,20);
GSquare s=new GSquare();
[Link](10);
}
}
Output:
Area of Rectangle :200.0
Area of Rectangle :100.0
7 Write a java program to implement the concept of Exception
handling by using predefined and user defined exceptions.
Predefined Exception
Program
class Demo_Exception {
public static void main(String args [ ])
{
int d, a;
try
{
d = 0;
a = 42 / d;
[Link]("This will not be printed.");
}
catch (ArithmeticException e)
{
[Link]("Division by zero.");
}
[Link]("After catch statement.");
}
}
Output:
Division by zero.
After catch statement.
user defined exceptions
Program:
class Demo_Exception {
static void demoproc() {
try {
throw new NullPointerException("demo");
}
catch(NullPointerException e)
{
[Link]("Caught inside demoproc.");
throw e; // rethrow the exception
}
}
public static void main(String args[]) {
try {
demoproc();
}
catch(NullPointerException e)
{
[Link]("Recaught: " + e);
}
}
}
Output:
Caught inside demoproc.
Recaught: [Link]: demo
8 Write a java program to implement the concept of Threading by
extending Thread class and by Implementing Runnable Interface.
Thread class
Program:
class FirstThread extends Thread
{
public void run()
{
for (int i=1; i<=5; i++)
{
[Link]( "Messag from First Thread : " +i);
try
{
[Link](1000);
}
catch (InterruptedException interruptedException)
{
[Link]( "First Thread is interrupted when it is sleeping"
+interruptedException);
}
}
}
}
class SecondThread extends Thread
{
public void run()
{
for (int i=1; i<=5; i++)
{
[Link]( "Messag from Second Thread : " +i);
try
{
[Link] (1000);
}
catch (InterruptedException interruptedException)
{
[Link]( "Second Thread is interrupted when it is sleeping"
+interruptedException);
}
}
}
}
public class Demo_Thread
{
public static void main(String args[])
{
FirstThread firstThread = new FirstThread();
SecondThread secondThread = new SecondThread();
[Link]();
[Link]();
}
}
Output:
Messag from Second Thread : 1
Messag from First Thread : 1
Messag from First Thread : 2
Messag from Second Thread : 2
Messag from First Thread : 3
Messag from Second Thread : 3
Messag from First Thread : 4
Messag from Second Thread : 4
Messag from Second Thread : 5
Messag from First Thread : 5
Runnable Interface
Program:
class FirstThread implements Runnable
{
public void run()
{
for (int i=1; i<=5; i++)
{
[Link]( "Messag from First Thread : " +i);
try
{
[Link](1000);
}
catch (InterruptedException interruptedException)
{
[Link]( "First Thread is interrupted when it is sleeping"
+interruptedException);
}
}
}
}
class SecondThread implements Runnable
{
public void run()
{
for (int i=1; i<=5; i++)
{
[Link]( "Messag from Second Thread : " +i);
try
{
[Link] (1000);
}
catch (InterruptedException interruptedException)
{
[Link]( "Second Thread is interrupted when it is sleeping"
+interruptedException);
}
}
}
}
public class Demo_Thread
{
public static void main(String args[])
{
FirstThread firstThread = new FirstThread();
SecondThread secondThread = new SecondThread();
Thread t1=new Thread(firstThread);
Thread t2=new Thread(secondThread);
[Link]();
[Link]();
}
}
Output:
Messag from First Thread : 1
Messag from Second Thread : 1
Messag from First Thread : 2
Messag from Second Thread : 2
Messag from First Thread : 3
Messag from Second Thread : 3
Messag from First Thread : 4
Messag from Second Thread : 4
Messag from First Thread : 5
Messag from Second Thread : 5
9 Write a program using Applet to display a message in the Applet and
for configuring Applets by passing parameters.
Program:
import [Link].*;
import [Link].*;
/*
<applet code="Demo_Applet" width="400" height="200">
<param name="Name" value="AITAM">
<param name="Location" value="Tekkali">
</applet>
*/
public class Demo_Applet extends Applet
{
String name;
String location;
public void init()
{
name = getParameter("Name");
location = getParameter("Location");
}
public void paint(Graphics g)
{
[Link]("Reading parameters passed to this applet -", 20, 20);
[Link]("Name -" + name, 20, 40);
[Link]("Location-" + location, 20, 60);
}
}
Output:
10 Write a java program to implement thread priorities
Program:
class ThreadDemo extends Thread
{
public void run()
{
[Link]("Inside run method");
}
public static void main(String[]args)
{
ThreadDemo t1 = new ThreadDemo();
ThreadDemo t2 = new ThreadDemo();
ThreadDemo t3 = new ThreadDemo();
[Link]("t1 thread priority : " +[Link]());
[Link]("t2 thread priority : " +[Link]());
[Link]("t3 thread priority : " +[Link]());
[Link](2);
[Link](5);
[Link](8);
[Link]("t1 thread priority : " +[Link]());
[Link]("t2 thread priority : " +[Link]());
[Link]("t3 thread priority : " +[Link]());
[Link]([Link]().getName());
[Link]("Main thread priority : "+ [Link]().getPriority());
[Link]().setPriority(10);
[Link]("Main thread priority : "+ [Link]().getPriority());
}
}
Output:
t1 thread priority : 5
t2 thread priority : 5
t3 thread priority : 5
t1 thread priority : 2
t2 thread priority : 5
t3 thread priority : 8
mainMain thread priority : 5
Main thread priority : 10