Java Lab MCA R20 Programs
Java Lab MCA R20 Programs
----------------------------------------------------------------------------------------------------
List of Experiments :-
1) Write a Java Program that uses both recursive and non recursive functions to print the
nth value of the Fibonacci sequence. (Fibonacci Sequence is 1 1 2 3 5 8 13 21 34…)
Program:- p1.java
import java.util.Scanner;
class p1
{
public static void main(String args[ ])
{
System.out.print("Enter a Number : ");
Scanner obj=new Scanner(System.in);
int a=obj.nextInt();
x obj1=new x();
int b=obj1.input(a);
System.out.println("The "+a+"th Number of the Fibonacci sequence is : "+b);
}
}
class x
{
int a=1;
int b=1;
int c=0;
int count;
int input(int a)
{
count=a;
count=fib(count);
return count;
}
---------------------------------------------------------------------------------------------------------------------
2) Write a Java Program that prompts the user for an integer and then prints out all the
prime numbers up to that Integer. (Prime Number sequence is 2 3 5 7 11 13 17 19…)
Program:- p2.java
import java.util.Scanner;
class p2
{
public static void main(String args[ ])
{
int n;
int p;
Scanner s=new Scanner(System.in);
System.out.print("Enter a Number : ");
n=s.nextInt();
System.out.println("Prime Numbers upto "+n+" are");
for(int i=2;i<n;i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0)
System.out.println(i);
}
}
}
---------------------------------------------------------------------------------------------------------------------
3) Write a Java Program that checks whether a given string is a palindrome or not.
(Palindrome is “abba”, “abcdcba”, “madam”, “level”, “radar”, “mom”, “refer”,….)
Program:- p3.java
import java.util.Scanner;
class p3
{
public static void main(String args[])
{
String str, rev = "";
Scanner sc = new Scanner(System.in);
if (str.equals(rev))
System.out.println(str+" is a Palindrome");
else
System.out.println(str+" is Not a Palindrome");
}
}
---------------------------------------------------------------------------------------------------------------------
4) Write a Java Program for sorting a given list of names in ascending order.
Program:- p4.java
import java.util.Scanner;
class Sorting
{
void sortStrings()
{
Scanner s = new Scanner(System.in);
System.out.print("How many Names you want to Sort : ");
int n = s.nextInt();
String[] str = new String[n];
System.out.println("Enter the Names : ");
for(int i = 0; i < n; i++)
{
str[i] = new String(s.next());
}
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
if(str[i].compareTo(str[j])>0)
{
String temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
System.out.println("\nSorted list of Names in Ascending Order :");
for(int i = 0; i < n ; i++)
{
System.out.println(str[i]);
}
}
}
class p4
{
public static void main(String args[])
{
Sorting obj = new Sorting();
obj.sortStrings();
}
}
---------------------------------------------------------------------------------------------------------------------
5) Write a Java Program that illustrates how runtime polymorphism is achieved. (Method
Overriding)
Program:-p5.java
public class p5
{
public static void main(String[] args)
{
Boy obj1 = new Boy();
obj1.eat();
}
}
class Human
{
public void eat()
{
System.out.println("Human is eating");
}
}
---------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
---------------------------------------------------------------------------------------------------------------------
Note:
Compile package program with -d (it represents destination directory) and .(it represents the
current folder/Path also can be mentioned in place of “.”). Eg:- javac -d . <pgmName.Java>
7) Write a Java Program, using StringTokenizer class, which reads a line of integers and
then displays each integer and the sum of all integers.
Program:-p7.java
import java.util.Scanner;
import java.util.StringTokenizer;
class p7
{
public static void main(String args[])
{
int n;
int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter integers with one space gap : ");
String s = sc.nextLine();
StringTokenizer st = new StringTokenizer(s, " ");
System.out.println("Integers are..");
while (st.hasMoreTokens())
{
String temp = st.nextToken();
n = Integer.parseInt(temp);
System.out.println(n);
sum = sum + n;
}
System.out.println("Sum of all integers : " + sum);
sc.close();
}
}
---------------------------------------------------------------------------------------------------------------------
8) Write a Java Program that reads a file name from the user and then displays
information about whether the file exists, whether the file is readable/ writable, the type of
file and the length of the file in bytes and display the content of the using FileInputStream
class.
Program:-p8.java
import java.io.File;
class p8
{
static void p(String s)
{
System.out.println(s);
}
---------------------------------------------------------------------------------------------------------------------
9) Write a Java Program that displays the number of characters, lines and words in a
text/text file.
my name
is
Pradeep
Program2: p9.java
import java.util.*;
import java.io.*;
class p9
{
public static void main(String args[ ])throws IOException
{
int nl=1,nw=0;
char ch;
Scanner scr=new Scanner(System.in);
System.out.print("\nEnter File name: ");
String str=scr.nextLine();
FileInputStream f=new FileInputStream(str);
int n=f.available();
for(int i=0;i<n;i++)
{
ch=(char)f.read();
if(ch=='\n')
nl++;
else if(ch==' ')
nw++;
}
System.out.println("\nNumber of lines : "+nl);
System.out.println("\nNumber of words : "+(nl+nw));
System.out.println("\nNumber of characters : "+n);
}
}
---------------------------------------------------------------------------------------------------------------------
my name
is
Pradeep
Program2:- p10.java
import java.applet.*;
import java.awt.*;
import java.io.*;
---------------------------------------------------------------------------------------------------------------------
11)Write a Java Program that works as a simple calculator. Use a grid layout to arrange
buttons for the digits and for the +-*?% operations. Add a text field to display the result.
Program:-p11.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);}
add(add); add(sub); add(mul);
add(div); add(mod); add(clear); add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this); sub.addActionListener(this);
mul.addActionListener(this); div.addActionListener(this);
mod.addActionListener(this); clear.addActionListener(this);
EQ.addActionListener(this);
}
if(str.equals("EQ"))
{
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2;
else if(OP=='-')
result=v1-v2;
else if(OP=='*')
result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("clear"))
{
t1.setText("");
}
}
}
---------------------------------------------------------------------------------------------------------------------
Program:-p12.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
---------------------------------------------------------------------------------------------------------------------
Program:-p13.java
class x
{
synchronized void m1(int n)
{
for(int i=1;i<=5;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(200);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
public class p13
{
public static void main(String args[])
{
x obj = new x();
t1.start();
t2.start();
}
}
---------------------------------------------------------------------------------------------------------------------
14)Write a Java Program to illustrate user defined Exception Handling (also make use of
throw, throws).
Program1:-p14a.java(throw)
class p14a
{
public static void main(String args[])
{
try
{
throw new MyException();
}
catch(MyException e)
{
System.out.println(e) ;
}
}
}
---------------------------------------------------------------------------------------------------------------------
Program2:-p14b.java(throws)
class p14b
{
public static void main(String args[]) throws InterruptedException
{
for(int i=1;i<=5;i++)
{
Thread.sleep(700);
System.out.println(i);
}
}
}
---------------------------------------------------------------------------------------------------------------------