Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab Manual

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

LAB MANUAL

1. a) Write a java program that displays welcome to follow by user


name. Accept user name from the user.

Program:

import java.util.*;

class Welcome{

public static void main(String args[]){

Scanner obj=new Scanner(System.in);


System.out.print("enter a User name:");

String str=obj.nextLine();
System.out.print("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 java.util.Scanner;

class PrimeNumber

public static void main(String[] args)


{
int n,count;
Scanner s=new Scanner(System.in);
System.out.print("Enter number :");
n=s.nextInt();

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){
System.out.print(" "+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 java.util.Scanner;

class Rectangle
{

double length,width;

void read_attributes()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:");
length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
width = scanner.nextDouble();

void area()
{
double area=length*width;
System.out.println("Area of Rectangle ="+area);
}
void perimeter()
{

double perimeter=2*(length+width);
System.out.println("perimeter of Rectangle ="+perimeter);

class CalRectangle{
public static void main (String[] args)
{
Rectangle rec=new Rectangle();
rec.read_attributes();
rec.area();
rec.perimeter();

}
}
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 java.util.*;

class Fibo
{
public static void main(String args[])
{
int c=1,n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
n=sc.nextInt();
int a=1;
int b=0;

System.out.println("Fibonacci series upto "+n+" is :-");


while(c<=n)
{
System.out.print(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 java.util.Scanner;
class Factorial_non
{
public static void main(String[] args)
{
int n, mul = 1;
Scanner s = new Scanner(System.in);
System.out.print("Enter any integer:");
n = s.nextInt();
for(int i = 1; i <= n; i++)
{
mul = mul * i;
}
System.out.println("Factorial of "+n+" :"+mul);
}
}
Output::

Recursive:
Program:

import java.util.Scanner;
class Factorial{
public static void main(String args[]){

//Scanner object for capturing the user input


Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number:");

//Stored the entered value in variable


int num = scanner.nextInt();

//Called the user defined function fact


int factorial = fact(num);
System.out.println("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 java.util.*;
public class Palindromes
{
public static void main(String args[])
{
String str1, str2 = "";
Scanner s = new Scanner(System.in);
System.out.print("Enter the string:");
str1 = s.nextLine();
int n = str1.length();
for(int i = n - 1; i >= 0; i--)
{
str2 = str2 + str1.charAt(i);
}
if(str1.equalsIgnoreCase(str2))
{
System.out.println("The string is palindrome.");
}
else
{
System.out.println("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() {
System.out.println("No parameters");
}

void test(int a) {
System.out.println("a: " + a);
}

void test(int a, int b) {


System.out.println("a and b: " + a + " " + b);
}

double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}

class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
Output:

Method overriding

Program:

class Vehicle{
void run(){
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle{
void run()
{
System.out.println("Bike is running safely");
}
}

class Override{
public static void main(String args[])
{

Bike obj = new Bike();


obj.run();
}
}

Output:

B) a java program that illustrates how java achieved Run Time


Polymorphism.

Program:

class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A {

void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A {

void callme() {
System.out.println("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;
r.callme();
r = b;
r.callme();
r = c;
r.callme();
}
}

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() {
System.out.println("values in subclass:a="+a+" b="+b);
}
void add()
{
c=a+b;
System.out.println("addition of values in subclass:"+c);
}

class Demo_inherit{
public static void main(String args[]) {

Subclass ob=new Subclass();


ob.display();
ob.add();
}
}
Output:

B) Write a java program for abstract class to find areas of different


shapes.
Program:

import java.util.Scanner;

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;
System.out.println("Area of Rectangle: "+area);
}

void square(double s)
{
double area = s*s;
System.out.println("Area of Square: "+area);
}

void circle(double r)
{
double area = 3.14*r*r;
System.out.println("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(System.in);

System.out.print("\nEnter Length & Breadth of Rectangle: ");


l = get.nextDouble();
b = get.nextDouble();
area.rectangle(l, b);

System.out.print("\nEnter Side of a Square: ");


s = get.nextDouble();
area.square(s);
System.out.print("\nEnter Radius of Circle: ");
r = get.nextDouble();
area.circle(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)
{
System.out.println("Area of Rectangle :"+(length*width));
}
}

GSquare.java

package Geometry;

public class GSquare


{
public void area(double length)
{
System.out.print("Area of Rectangle :"+(length*length));
}
}

Demo_packages.java

import Geometry.*;

class Demo_packages
{
public static void main(String args[])
{

GRectangle rec=new GRectangle();


rec.area(10,20);
GSquare s=new GSquare();
s.area(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;
System.out.println("This will not be printed.");
}
catch (ArithmeticException e)
{
System.out.println("Division by zero.");
}
System.out.println("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)
{
System.out.println("Caught inside demoproc.");
throw e; // rethrow the exception
}

}
public static void main(String args[]) {
try {
demoproc();
}
catch(NullPointerException e)
{
System.out.println("Recaught: " + e);
}
}
}
Output:

Caught inside demoproc.


Recaught: java.lang.NullPointerException: 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++)
{
System.out.println( "Messag from First Thread : " +i);
try
{
Thread.sleep(1000);
}
catch (InterruptedException interruptedException)
{
System.out.println( "First Thread is interrupted when it is sleeping"
+interruptedException);
}
}
}
}
class SecondThread extends Thread
{

public void run()


{
for (int i=1; i<=5; i++)
{
System.out.println( "Messag from Second Thread : " +i);
try
{
Thread.sleep (1000);
}
catch (InterruptedException interruptedException)
{
System.out.println( "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();

firstThread.start();
secondThread.start();
}
}

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++)
{
System.out.println( "Messag from First Thread : " +i);
try
{
Thread.sleep(1000);
}
catch (InterruptedException interruptedException)
{
System.out.println( "First Thread is interrupted when it is sleeping"
+interruptedException);
}
}
}
}
class SecondThread implements Runnable
{

public void run()


{
for (int i=1; i<=5; i++)
{
System.out.println( "Messag from Second Thread : " +i);
try
{
Thread.sleep (1000);
}
catch (InterruptedException interruptedException)
{
System.out.println( "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);

t1.start();
t2.start();
}
}
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 java.awt.*;
import java.applet.*;

/*
<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)
{
g.drawString("Reading parameters passed to this applet -", 20, 20);
g.drawString("Name -" + name, 20, 40);
g.drawString("Location-" + location, 20, 60);
}
}
Output:

10 Write a java program to implement thread priorities

Program:

class ThreadDemo extends Thread


{
public void run()
{
System.out.println("Inside run method");
}

public static void main(String[]args)


{
ThreadDemo t1 = new ThreadDemo();
ThreadDemo t2 = new ThreadDemo();
ThreadDemo t3 = new ThreadDemo();
System.out.println("t1 thread priority : " +t1.getPriority());
System.out.println("t2 thread priority : " +t2.getPriority());
System.out.println("t3 thread priority : " +t3.getPriority());

t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);

System.out.println("t1 thread priority : " +t1.getPriority());


System.out.println("t2 thread priority : " +t2.getPriority());
System.out.println("t3 thread priority : " +t3.getPriority());

System.out.print(Thread.currentThread().getName());
System.out.println("Main thread priority : "+ Thread.currentThread().getPriority());

Thread.currentThread().setPriority(10);
System.out.println("Main thread priority : "+ Thread.currentThread().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

You might also like