Java Manual
Java Manual
PART- A
1. Write a Java program to display only those multi-digit prime numbers between a given range whose
digit sum is prime. Display the prime number and its digit sum side by side. Read the value for the range
using readLine() method of BufferedReader class.
Sample output:
If range is; m = 20, n=50
Prime number Sum of digits
23 - 5
29 - 11
41 - 5
43 - 7
47 – 11
import java.io.*;
int m, n, val = 0;
while(true)
m = Integer.parseInt(reader.readLine());
n = Integer.parseInt(reader.readLine());
if (isPrime(i))
int x= digitSum(i);
if(isPrime(x))
System.out.print(i);
System.out.println("\t\t"+x);
System.exit(0);
else
int sum = 0;
while (x != 0)
x = x/10;
return sum;
if (y % i == 0)
BCA, SRNMNC, SHIMOGA Page 2
JAVA MANUAL
return false;
return true;
2. Write a Java program to sort the elements of a square matrix. Read the order and elements of the
matrix during execution. Before sorting the elements of the matrix, display the source matrix.
Sample output:
Input Matrix is:
20 2 35
4 17 7
41 3 2
Matrix elements after sorting:
2 2 3
4 7 17
20 35 41
import java.io.*;
import java.util.*;
class SquareMatrixSum {
int k = 0;
temp[k++] = mat[i][j];
Arrays.sort(temp);
k = 0;
mat[i][j] = temp[k++];
System.out.println();
while(true)
int m,n,i,j;
m = input.nextInt();
n = input.nextInt();
if(m==n)
{
BCA, SRNMNC, SHIMOGA Page 4
JAVA MANUAL
mat[i][j] = input.nextInt();
System.out.println("Original Matrix:");
printMat(mat, m, n);
sortMat(mat, m, n);
printMat(mat, m, n);
System.exit(0);
else
4.Write java program to demonstrate method overloading to generate random numbers, random
alphabet sequence and random strings.
import java.io.*;
import java.lang.Math;
import java.util.Random;
import java.util.*;
printRandom(count,max);
int n = Integer.parseInt(br.readLine());
return c;
{
BCA, SRNMNC, SHIMOGA Page 6
JAVA MANUAL
for(int i=0;i<count;i++){
int r=rnd.nextInt(max);
System.out.println(r);
return 0;
return res;
5. Assume that an examination authority conducts qualifying examination for candidates twice
eachyear. First, in the month of June, second, in the month of December. Before the exam, it
opens a registration process so that candidates register themselves. After the end of the
registration dates, the authority consolidates the list of candidates and generates the unique
register numbers. These numbers are assigned to each candidate. The format of the register
numbers is as below. Each register number should contain exactly 10 characters.
For example, if year of registration 2018, cycle 2 and there are five candidates registered then,
registration numbers are: QE20182001, QE20182002, QE20182003, QE20182004, QE20182005.
The serial numbers should contain exactly 3 digits. To maintain it, prefix zeros as needed. (up to
9 serial number should be prefixed with two zeros, after 9, upto 99 it should be prefixed with
single zero and after 99, no zeros). Write a Java program to generate the registration numbers
as per the above requirement.
import java.io.*;
import java.lang.*;
import java.util.Formatter;
String comb="QE",year,cycle,srNo;
int num=0;
String[] regArray;
int n = Integer.parseInt(br.readLine());
//srNo=String.format("%03d",n);
regArray=new String[n];
String reg="";
year = br.readLine();
while(true)
cycle = br.readLine();
int c = Integer.parseInt(cycle);
if(c<3)
break;
else
for(int i=0;i<n;i++)
num++;
if(num<=9)
srNo=String.format("%03d",num);
srNo=String.format("%03d",num);
else
srNo=String.valueOf(num);
reg=comb+year+cycle+srNo;
regArray[i]=reg;
for(String val:regArray){
System.out.println(val);
6. Write a Java program to read name, register number, date of birth, address, phone number a student.
Concatenate these to frame a single content by delimiting each detail with a special symbol, pass it to
a method which should separate and display the details of the student. Declare a class containing the
following methods:
void getInformation() – to read student information. It should call concatenate(,,,,) by passing relevant
information.
void concatenate(String name, string regNo, String dob, String address, String phoneNo) to join the
information to frame a single content. It should call extractInformation(…) by passing the concatenated
information.
void extractInformation(String joinedInfo) to extracted concatenated contents and to display the
information.
Declare another class to contain main () method which calls void getInformation().
Sample output:
Student Name: Venkata Krishna
Register Number: BC171128
Date of Birth: 10/05/1996
Address: No. 5, First Cross, Nehru Nagar, Sagar.
Phone Number: 9900990099
Concatenated content:
Venkata Krishna%BC171128%10/05/1996%No. 5, First Cross, Nehru Nagar, Sagar.%9900990099
(Application: This is the way using which collection of information is communicated between client
and server in networked environment)
import java.util.*;
import java.io.*;
class ConcatDemo
String name;
String regNo;
String dob;
String address;
String phoneNo;
name=br.readLine();
regNo=br.readLine();
dob=br.readLine();
address=br.readLine();
phoneNo=br.readLine();
System.out.println("===================");
System.out.println("Student Name:"+name);
System.out.println("Register Number"+regNo);
System.out.println("Date of Birth::"+dob);
System.out.println("Address:"+address);
System.out.println("Phone Number:"+phoneNo);
System.out.println("===================");
concatenate(name, regNo,dob,address,phoneNo);
void concatenate(String name, String regNo, String dob, String address, String phoneNo)
System.out.println("concatenated info="+joinedInfo);
extractInformation(joinedInfo);
String[] words=s1.split("\\%");
System.out.println("===================");
System.out.println("extracted information");
for(String w:words){
System.out.println(w);
c.getInformation();
}
BCA, SRNMNC, SHIMOGA Page 11
JAVA MANUAL
7. Consider class person with fields name, address and date of birth and methods read_data() and show()
and another class employee inherited form person class with fields emp_id, date of join and salary and
methods read() and show(). Write java program to implement the concept of single inheritance
with method overriding concepts for the above classes.
import java.util.*;
class Person
String name;
String address;
String dob;
void read_data()
name=sc.next();
address=sc.next();
dob=sc.next();
void show()
System.out.println("name="+name);
System.out.println("address="+address);
System.out.println("dob="+dob);
int emp_id;
String date_of_joining;
int salary;
void read()
read_data();
emp_id=sc.nextInt();
date_of_joining=sc.next();
salary=sc.nextInt();
void show()
System.out.println("emp_id="+emp_id);
System.out.println("address="+date_of_joining);
System.out.println("dob="+salary);
e.read();
p.show();
e.show();
Part B
1. Write a Java program to create a vector, add elements at the end, at specified location onto the
vectorand display the elements. Write an option driven program using switch…case and also insertion
ofany type of objects must be possible. Read input as strings and find the type of data and convert them
into appropriate objects of appropriate classes. ( Ex: 10 must be converted to object of Integer class, 2.5
into object of Float class etc.). Handle exception while converting the inputs.
import java.io.*;
import java.util.*;
int ch,pos;
String c;
while(true)
System.out.println(" 3 to diplay");
System.out.println(" 4 to exit");
ch=sc.nextInt();
BCA, SRNMNC, SHIMOGA Page 14
JAVA MANUAL
switch(ch)
c=sc.next();
try
Double ele=Double.valueOf(c);
v.addElement(ele);
catch (Exception e)
break;
break;
c=sc.next();
try
Integer el=Integer.valueOf(c);
pos=sc.nextInt();
v.insertElementAt(el,pos);
catch (Exception e)
break;
3. Write java program to demonstrate multi level inheritance using appropriate real life example.
class Car
public Car()
System.out.println("Class Car");
public Maruti()
System.out.println("Class Maruti");
System.out.println("Brand: Maruti");
System.out.println("Max: 90Kmph");
public Maruti800()
System.out.println("Max: 80Kmph");
obj.vehicleType();
obj.brand();
obj.speed();
4. Write a java program to create a package Number which contains a class with three static methods
prime, fibanocii and Armstrong that checks whether the passed value is belongs to the corresponding
types.
package number;
import java.util.Scanner;
int secondTerm = 1;
int thirdTerm = 0;
firstTerm = secondTerm;
secondTerm = thirdTerm;
}
if(thirdTerm == n)
System.out.println(n+" is fib");
else
num1 = num;
while (num1 != 0)
{
remainder = num1 % 10;
result += Math.pow(remainder, 3);
num1 /= 10;
}
if(result == num)
System.out.println(num + " is an Armstrong number.");
else
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
import number.NumDemo;
import java.util.*;
class Packnum
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
}
}
import java.util.*;
class ThreadDemo
{
public static void main (String [] args)
{
GenOdd g=new GenOdd();
GenEven e=new GenEven();
GenFib f=new GenFib();
Thread t1=new Thread(g);
Thread t2=new Thread(e);
Thread t3=new Thread(f);
t2.start();
t1.start();
t3.start();
}
}
6. Write an applet to display the address of a person (atleast 4 lines) using parameter passing concept.
Appropriate message should be displayed for wrong input.
import java.applet.Applet;
import java.awt.Graphics;
public class UseParam extends Applet
{
public void paint(Graphics g)
{
String str1=getParameter("name");
g.drawString("Ram",50,50);
g.drawString(str1,50, 50);
String str2=getParameter("home");
g.drawString(str2,50, 70);
String str3=getParameter("road");
g.drawString(str3,50, 90);
String str4=getParameter("name");
g.drawString(str4,50, 110);
}
}
<html >
<body>
<applet code="UseParam.class" width="300" height="300">
<param name="name" value="SRNM">
<param name="home" value="#111,marvel ">
<param name="road" value="old post office road">
<param name="city" value="shivamoga,577201">
</applet>
</body>
</html>
import java.awt.*;
import java.applet.*;
import java.util.*;
public class Polly extends Applet
{
TextField t1;
int n;
public void init()
{
t1=new TextField(3);
add(t1);
t1.setText("0");
}
public boolean action(Event e,Object o)
{
repaint();
return true;
}
public void paint(Graphics g)
{
g.drawString(" enter a no. ", 60,50);
try
{
n=Integer.parseInt(t1.getText());
}
catch(Exception e)
{
}
for(int i=0;i<n;i++)
{
g.drawOval(100+i*5,100+i*5,100-i*10,100-i*10);
g.drawRect(100+i*5,250+i*5,100-i*10,100-i*10);
g.drawRect(100+i*5,400+i*5,250-i*10,100-i*10);
<html>
<body>
<applet code="Polly" height=500 width=500>
</applet>
</body>
</html>