0% found this document useful (0 votes)
2 views37 pages

Annual Comp Prol

The document outlines several programming tasks involving Java classes, including storing student information, counting words that begin with vowels, adding time, summing angles, identifying palindromic numbers, and shifting a 2D array. Each task includes a detailed algorithm and corresponding Java code to implement the functionality. Variable lists are provided for clarity on data types and purposes within the classes.

Uploaded by

Darshana Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views37 pages

Annual Comp Prol

The document outlines several programming tasks involving Java classes, including storing student information, counting words that begin with vowels, adding time, summing angles, identifying palindromic numbers, and shifting a 2D array. Each task includes a detailed algorithm and corresponding Java code to implement the functionality. Variable lists are provided for clarity on data types and purposes within the classes.

Uploaded by

Darshana Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

ANNUAL

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
{

//open file for reading


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();
}
}
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.

The description of the class is given below:


Class name: VowelWord
Data Members/instance variables:
str: to store a sentence
freq: store the frequency of the words beginning with
a vowel.
Member functions:
VowelWord(): constructor to initialize data members
to a legal initial value
void readstr(): to accept a sentence
void freq_vowel: counts the frequency of the words
that begin with a vowel
void display(): to display the original string and the
frequency of the words that begin with a vowel.
Specify the class VowelWord giving details of the
constructor (), void readstr (), void freq_vowel () and
void display (). Also, define the main () function to
create an object and call the methods accordingly
to enable the task.

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)

Specify the class Adder giving details of the


constructor(), void readtime(), void
addtime(Adder. Adder) and void disptime().
Define the main() function to create objects
and call the functions accordingly to
enable the task.
ALGORITHM:
Step 1: Input an array.
Step 2: Input hours and minutes in position
0 and 1 respectively.
Step 3: void addtime(Adder X , Adder Y)
{
Int hr1=X.a[0];
Int min1=X.a[1]’
Int hr2=Y.a[0];
Int min2=Y.a[1];
Int h=hr1+hr2;
Int min=min1+min2;
a[0]=h+(min/60);
a[1]=min%60;
Step 4:
Print hours and minutes.
Step 5: Create main function and call other
methods.

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 addtime ( Adder X,Adder Y)


{
int addhr=0,addmin=0,finall=0;
addhr = X.a[0]+Y.a[0];
addmin = X.a[1]+Y.a[1];
finall =addhr*60+addmin;
a[0]=finall/60;
a[1]=finall%60;
}

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);
}

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;

public static void main()


{
angle ob1=new angle();
angle ob2=new angle();
ob1.input();
ob2.input();
angle ob3=new angle();
ob3=ob3.sumangle(ob1,ob2);
ob3.display();
}
}
Variable List
Name Type Description
mn int Accepts
minutes
deg int Accepts
degrees
t int Swap
variable.

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
{

Scanner sc=new Scanner(System.in);


int mat [] [];

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();

Shift ob1=new Shift(r,c);


Shift ob2=new Shift(r,c);

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy