Annual Comp Prol
Annual Comp Prol
TERM
Question 1: Write a program to store
student information in “stud.dat” the
following format:
Roll No:
Name:
Class:
Section:
ALGORITHM:
Step 1: Input name.
Step 2: Input class and section
Step 3: FileInputStream fr new FileInputStream("e:\\
stud.dat"); DataInputStream dr new
DataInputStream(fr);
Try
{
while(true) {
roll dr.readInt()
name dr.readUTF();
cls- dr.readInt();
sec dr.readUTF();
display();
}
}
Step 4: catch (EOFException e) {
fr.close();
}
public static void main() throws IOException
{
studread ob-new studread();
ob.read_data();
PROGRAM:
import java.io.*;
class studread
{
int roll;
String name;
int cls;
String sec;
void display()
{
System.out.println("Roll No"+roll);
System.out.println("Name"+name);
System.out.println("Class "+cls);
System.out.println("Section "+sec);
}
void read_data() throws IOException
{
while(true) {
roll dr.readInt();
name dr.readUTF();
cls- dr.readInt();
sec dr.readUTF();
display();
}
}
catch (EOFException e) {
fr.close();
}
public static void main() throws IOException
{
studread ob-new studread();
ob.read_data();
}
}
}
Variable List
Name Type Description
roll int Accepts roll
number
name String Accepts
name.
Class int Accepts
class
sec Char Accepts
section.
Question 2:
Design a class VowelWord to accept a sentence and
calculate the frequency of words that begin with a
vowel. The words in the input string are separated by
a single blank space and terminated by a full stop.
ALGORITHM:
Step 1: Input a sentence from user
Step 2: freq=0
Step 3: StringTokenizer x=new
StringTokenizer(str);
int ctr=x.countTokens();
for(int i=0;i<ctr;i++)
{
String w="";
w=x.nextToken();
if(w.charAt(0)=='a'||w.charAt(0)=='e'||
w.charAt(0)=='i'||w.charAt(0)=='o'||
w.charAt(0)=='u'||w.charAt(0)=='A'||
w.charAt(0)=='E'||w.charAt(0)=='I'||
w.charAt(0)=='O'||w.charAt(0)=='U')
freq++;
Step 4: System.out.println(str);
System.out.println(freq);
Step 5: In main function-
VowelWord obj= new VowelWord();
obj.readstr();
obj.freq_vowel();
obj.display();
PROGRAM:
import java.util.*;
class VowelWord
{
String str;
int freq;
VowelWord()
{
str=" ";
freq=0;
}
void readstr()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter a string");
str=sc.nextLine();
}
void freq_vowel()
{
StringTokenizer x=new
StringTokenizer(str);
int ctr=x.countTokens();
for(int i=0;i<ctr;i++)
{String w="";
w=x.nextToken();
if(w.charAt(0)=='a'||w.charAt(0)=='e'||
w.charAt(0)=='i'||w.charAt(0)=='o'||
w.charAt(0)=='u'||w.charAt(0)=='A'||
w.charAt(0)=='E'||w.charAt(0)=='I'||
w.charAt(0)=='O'||w.charAt(0)=='U')
{
freq++;
}
}
}
void display()
{
System.out.println(str);
System.out.println(freq);
}
void main()
{
VowelWord obj= new VowelWord();
obj.readstr();
obj.freq_vowel();
obj.display();
}
}
Variable List
Name Type Description
freq int Inputs
frequency
w String Accepts
String
ctr int Loop
variable
str String Tokenizer
variable
Question 3:
A class Adder has been defined to add any
two accepted time.
Example:
Time A
- 6 hours 35 minutes
Time B
7 hours 45 minutes
Their sum is 14 hours 20 minutes (where 60
minutes = 1 hour)
The details of the members of the class are
given below:
Class name:
Adder
Data member/instance variable:
a[]: integer array to hold two elements
(hours and minutes)
Member functions/methods:
Adder () : constructor to assign 0 to the array
elements
void readtime (): to enter the elements of the
array
void addtime (Adder X, Adder Y): adds the
time of the two parameterized objects X and
Y and stores the sum in the current calling
object
void disptime(): displays the array elements
with an appropriate message (i.e., hours and
minutes)
PROGRAM:
import java.util.*;
class Adder
{
Scanner sc = new Scanner ( System.in );
int a[];
Adder()
{
for(int i=0;i<2;i++)
{
a[i]=0;
}
}
void readtime()
{
System.out.println("Enter hour");
a[0]=sc.nextInt();
System.out.println("Enter minutes");
a[1]=sc.nextInt();
}
void disptime()
{
System.out.println("Hours:" +a[0]);
System.out.println("Minutes:"+a[1]);
}
void main()
{
Adder ob1 = new Adder();
Adder ob2 = new Adder();
Adder ob3 = new Adder();
ob1.readtime();
ob2.readtime();
ob1.disptime();
ob2.disptime();
ob3.addtime(ob1,ob2);
ob3.disptime();
}
}
Variable List
Name Type Description
a int Accepts array
i int Loop variable
Question 4:
Class name : angle
Data Members:
int dg: stores value in degrees
int mnt : stores value in minutes
Member Methods:
angle(): constructor used to initial data
member with their legal initial values.
void input(): to enter an angle value
void display(): to display to value of the
angle in degrees and minutes
angle sumangle(angle a1, angle a2):adds the
degree of the two parameterised objects a1
and a2 and stores the sum in the current
calling object
Define the main() to create objects and call
the functions accordingly to enable the task.
ALGORITHM:
Step 1: Input angle from user.
Step 2: Enter degrees and minutes.
Step 3: angle sumangle(angle a1,angle a2)
{
angle t=new angle();
t.dg=a1.dg+a2.dg;
t.mnt=a1.mnt+a2.mnt;
if(t.mnt>60)
{
t.dg=t.dg+t.mnt/60;
t.mnt=t.mnt%60;
}
return t;
}
Step 4: Create main function to call above
methods.
PROGRAM:
import java.util.*;
class angle
{
int dg;
int mnt;
angle()//constructor
{
dg=0;
mnt=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Angle");
dg=sc.nextInt();
mnt=sc.nextInt();
}
void display()
{
System.out.println("Degree "+dg);
System.out.println("Minutes"+mnt);
}
Question 5:
class pal
Data members:
int x,y
Functions:
pal()
//Constructor
void input() //input the value of x,y
int rev(int v) //return revers of v
void display() //Display all
Pal. no from x and y
ALGORITHM:
Step 1: Input x and y
Step 2: int rev(int v)
{
int d=0; int rev=0;
for(int i=v;i>0;i=i/10)
{
d=i%10;
rev=rev*10+d;
}
return rev;
}
Step 3: void display()
{
int r=0;
for (int i=x;i<y;i++)
{
r= rev(i);
if(i==r)
{
System.out.println(r+"yes");
}
PROGRAM:
import java.util.*;
class pal
{
int x,y;
pal()
{
x=0;
y=0;
}
void input()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter vals of x and
y");
x=sc.nextInt();
y=sc.nextInt();
}
int rev(int v)
{
int d=0; int rev=0;
for(int i=v;i>0;i=i/10)
{
d=i%10;
rev=rev*10+d;
}
return rev;
}
void display()
{
int r=0;
for (int i=x;i<y;i++)
{
r= rev(i);
if(i==r)
{
System.out.println(r+"yes");
}
}
}
}
Variable List
Name Type Description
x int User input
value
y int User input
value.
Question 6:
A class Shift contains a 2D array of order
(4x4). Design the class Shift to shuffle the
matrix (the first row becomes the last, the
last row becomes the first.)
Class name:
Shift
Data members:
mat[][], m,n;
Member functions:
void input(): enters the elements of the array
void cyclic(): enables the matrix to shift rows.
matrix (the first row becomes the last, the
last row becomes the first.)
void display(): display the matrix elements.
Define the main() to create an object and call
the methods accordingly to enable the task
of shifting the array elements.
ALGORITHM:
Step 1: Input array and enter elements.
Step 2: Initialize variables using constructor.
Step 3: Shift the matrix such that first row
becomes the last and last row becomes the
first.
Step 4: void cyclic (Shift P)
{
for (int i=0;i<m:1++)
{
for (int j=0;j<n;j++)
{
if(i!=0)
{
mat[i-1][j]=P.mat[i][j];
}
else
{
mat [m-1][j]=P.mat[0][j];
}
Step 5: Create main function and call the
above functions.
PROGRAM:
import java.util.*;
class Shift
{
int m,n;
Shift(int mm, int nn)
{
m=mm;
n=nn;
}
mat=new int[m] [n];
void input()
System.out.println("enter elements of the
array");
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
mat [1][j]=sc.nextInt();
}
}
void display()
{
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
System.out.print(mat [1] [3]+" ");
}
System.out.println();
}
void cyclic (Shift P)
{
for (int i=0;i<m:1++)
{
for (int j=0;j<n;j++)
{
if(i!=0)
{
mat[i-1][j]=P.mat[i][j];
}
else
{
mat [m-1][j]=P.mat[0][j];
}
public void main()
{
Scanner sc1=newScanner(System.in);
System.out.println("enter the number of
rows and columns");
int r=sc.nextInt();
int c=sc1.nextInt();
ob1.input();
ob2.cyclic (ob1);
ob1.display();
ob2.display();
}
}
Variable list
Name Type Description
mat int Accepts
matrix
m int No of rows
n int No of
columns
i int Loop variable
j int Loop variable