Anda di halaman 1dari 19

# 1D

ARRAY

FREQUENCY
QUESTION:
A class PrimeFac contains an array of 50 integers. Some of the members of the class
are given below:
Class name

: PrimeFac

num[ ]

freq[ ]
of numbers.

## : array to store the frequency of prime factors

Member functions :
PrimeFac( )

void enter( )

## : to enter values into array num[ ]

void frefac( )
the numbers stored
assign it to freq[ ]

in num[ ]and

void disp( )

## : to display both the array

Specify the class PrimeFac giving the details of constructors, void enter ( ),void
refac( ) and void disp( ).
PROGRAM:
import java.io.*;
public class PrimeFac
{
int num[]=new int[5];
int freq[]=new int[5];
PrimeFac()
{
for(int i=0;i < 5;i++)
{
num[i]=0;
freq[i]=0;
}
}
void input()throws IOException
{
for(int i=0;i < 5;i++)
{
System.out.println("Enter a Number");

}
}
void frefac()
{
int i,j,k,c;
for(i=0;i < 5;i++)
{
c=0;
for(j=1;j <=num[i];j++)
{
if(num[i]%j==0)
{
c=0;
for(k=1;k <= j;k++)
{
if(j%k==0)
{
c++;
}
}
if(c==2)
{
freq[i]++;
}
}
}
}
}
void disp()
{
for(int i=0;i < 5;i++)
{
System.out.println(num[i]+"Frequency="+freq[i]);
}
}
}

VARIABLE DESCRIPTION:
VARIABLE
num[ ]
freq[ ]
i
j
k
c

DATA TYPE
int(Integer)
int(Integer)
int(Integer)
int(Integer)
int(Integer)
int(Integer)

USE

## INSERTION AND DELETION

QUESTION:

A class Rearrange has been defined to insert an element and to delete an element from an
array . Some of the members of the class are given below :
Class name :
Data members/instance variables
a[] : integer type array
n : size of array(integer)
pos1 : position of insertion(integer)
pos2 : position of deletion(integer)
item : item to be inserted(integer)
Members functions/methods
void enter() : to enter size, array elements and to display the entered elements
void insert() : to accept elements(item) to be inserted, element(item) at the position
of insertion.
void disp1() : to display array after item is inserted.
void disp2() : to disaplay array after item is deleted
void remov() : to accept the position of deletion and delete elements(item) at the
position of deletion.
Specify the class Rearrange giving details of the functions void enter(),void insert(),void
disp1(),void disp2() and void remov().
PROGRAM:
import java.io.*;
class Rearrange
{
int a[ ];
int n;
int Pos1;
int Pos2;
int item;
void enter( )throws IOException
{
System.out.println("Enter size of Array");
a=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Array elements are");
for(i=0; i <= n - 1;i++)
System.out.println(a[i]);
}
}
void disp1( )
{
System.out.println("\n Array after insertion\n");
for(int i=0;i<n;i++)
System.out.println(a[i]);
}

void disp2( )
{
System.out.println("Array After deletion\n");
for(int i=0;i < n;i++)
System.out.println(a[i]);
}
void insert( )throws IOException
{
System.out.println("enter element to be insert");
System.out.println("Enter position");
if(Pos1>=n)
{
System.out.println("Invalid position");
return;
}
for(int k=n-1;k>=Pos1;k--)
{
a[k+1]=a[k];
a[Pos1]= item;
n++;
disp1( );
}
}
void remove( )throws IOException
{
System.out.println("enter position");
if(Pos2<0||Pos2>n)
{
System.out.println("invalid position");
return;
}
for(int k=Pos2-1;k<n-1;k++)
a[k]=a[k+1];
n--;
disp2();
}
}

VARIABLE DESCRIPTION:
VARIABLE
a[]
n
Post1

DATA TYPE
int(integer)
int(integer)
int(integer)

USE

Post2
item
i
k

int(integer)
int(integer)
int(integer)
int(integer)

PSEUDOARITHMETIC SEQUENCE
QUESTION:
You are given a sequence of N integers, which are called as pseudo arithmetic sequence.
Eg:Sequence of N integers: 2,5,6,8,9,12
We observe that: 12+2=5+9=6+8=14

## The sum of that above sequence can be calculated as 14*3=42

For the sequence containing odd no. of elements, the rule is to double the middle element, for
eg., 2,5,7,9,12 = 2+12=5+9=7+7=14.
14*3=42
A class Pseudoarithmetic determines whether a given sequence is a pseudo-arithmetic
sequence. The details of the class are given below:Data members/Instance variables :n
: to store the sequence
a[ ]
: integer array to store the sequence of numbers
ans,flag
: to keep the record of the status
sum
: store the sum of sequence of numbers
r
: store the sum of the two numbers
Member function :Pseudoarithmetic( )
: default constructor
void accept(int n)
: to assign nn to n and to create an integer array . Fill in the
elements of the array.
boolean check( )
: return true if the sequence is a pseudo-arithmetic sequence
otherwise returns false
Specify the class Pseufoarithmetic ,giving details of the constructor( ), void accept(int), and
boolean check().Also define the main() function to create an object and call the member
functions accordingly to enable the task.

PROGRAM:
import java.util.*;
class Pseudoarithmetic
{
int n;
int[] a;
boolean ans,flag;
int sum,r;
Pseudoarithmetic()
{
n=0;
ans=false;
flag=false;
sum=0;
r=0;
}
void accept(int nn)
{
n=nn;
a=new int[n];
Scanner scan=new Scanner(System.in);

## System.out.println("Enter "+n+" elements of array ");

for(int i=0;i < n; i++)
{
a[i]=scan.nextInt();
}
}
boolean check()
{
if(n % 2==0)
flag=true;
int mid=(n-1)/2,midsum=0,i,j,k;
if(flag==true)
{
r=a[mid] + a[mid + 1];
}
else
r=a[mid]*2;
i=mid-1;
if(flag==true)
j=mid+2;
else
j=mid+1;
for(sum=r; (i >=0) &&(j < n); i -=1,j +=1)
{
midsum=a[i] + a[j];
if(midsum==r)
{
ans=true;
sum=sum + midsum;
}
else
{
ans=false;
sum=0;
for(k=0;k < n;k++)
sum += a[k];
break;
}
}
return ans;
}

## public static void main(String[]args)

{
Pseudoarithmetic seq=new Pseudoarithmetic();
Scanner kb=new Scanner(System.in);
System.out.println("Enter size of sequence :");
int size=kb.nextInt();
seq.accept(size);
if (seq.check ()==true)
System.out.println("It is a Pseudoarithmetic sequence with sum as"+seq.sum);
else
System.out.println("It is NOT a Pseudoarithmetic sequence with sum as
"+seq.sum);
}
}

VARIABLE DISCRIPTION:
VARIA
BLE

DATA TYPE

sum

Int(Integer)

ans

int(Integer)

size

int(Integer)

midsum

int(Integer)

mid

int(Integer)

a[]

int(Integer)

flag

int(Integer)

int(Integer)

int(Integer)

int(Integer)

int(Integer)

int(Integer)

USE

STRIN
G

LONGEST WORD
QUESTION:
A class Mystring has been defined for the following methods/functions:
Class name : Mystring
Data members/instance variables :
str[] : to store a string
len : length of the given string
Members function/methods :
Mystring() : constructor
int code(int index) : returns, ASCII code for the character at position index
void word() : displays longest word in the string.
specify the class Mystring giving the details of the constructor and void readstring(),
int code(int index),
void word() only.
PROGRAM:
import java.io.*;
class mystring
{
String str;
int len;
mystring()
{
str=" ";
len=0;
}
{
System.out.println("Enter string");
len=str.length();
}
int code(int index)
{
if(index>=len)
{
System.out.println("Invalid Index");

return 0;
}
return(int)str.charAt(index-1);
}
void word()
{
String s,p;
int k=0;
s=" ";
p=" ";
{
for(int i=0;i<len;i++)
{
if(str.charAt(i)==' ')
if(s.length()>p.length())
{
p=s;
s=" ";
}
else
{
s=s+str.charAt(i);
}
}
System.out.println("longest word"+p);
}
}
}

VARIABLE DISCRIPTION:
VARIABLE
str
len
index
s
p
k

DATA TYPE
String
int(Integer)
int(Integer)
String
String
int(Integer)

USE

STRING MODIFICATION
QUESTION:

## A class Modify has been defined with the following details:

Class Name : Modify
Data Members :
Str -- stores the string.
len -- to store the length of the string.
Member Functions :
void read() -- to accept the string in upper case alphabets
void putin(int char) -- to insert a character at the specified position in the string and
display the changed
string.
void takeout(int) -- to remove character from the specified position in the string and
display the changed
string.
void change() -- toreplace each character in the original string by the character which
is at a distance of 2
moves ahed.
For eg.,
"ABCD" becomes "CDEF"
"XYZ" becomes "ZAB"
Specify the class Modify giving details of the functions void read(), void
putin(int,chart), void takeout(int)
and void change().

PROGRAM:
class Modify
{
String st;
int len;
{
st=s;
len=st.length();
}
void putin(int P,char c)
{
int i=len;
char k;
String n;
n=st.substring(0,P-1);

n=n+c;
n=n+st.substring(P,len);
st=n;
len=st.length();
System.out.print(st);
}
void takeout(int p)
{
String n;
n=st.substring(0,p-1);
n=n+st.substring(p,len);
st=n;
len=st.length();
System.out.print(st);
}
void charge()
{
String n=" ";
int c;
for(int i=0;i<len;i++)
{
c=st.charAt(i);
if(c=='Y')
n=n+'A';
else if(c=='Z')
n=n+'B';
else
{
c=c+2;
n=n+c;
}
st=n;
System.out.print(st);
}
}
}

VARIABLE DESCRIPTION:
VARIABLE
st
len

DATA TYPE
String
int(Integer

USE

s
p
c
i
k
n

)
String
int(Integer
)
int(Integer
)
int(Integer
)
char(Chara
cter)
String

STRING
OPERATIONS
QUESTION:
You have been given a piece of text that contains words,blank spaces and tabs only.A
word is defined as a group of continuous non blank characters. A white space is a
tab('It') or a blank space(' ')
E.g
Text
"

No. of words

## No. of White Spaces

10

15

"

"This is beautiful
"
"blessed

"
A class Text is designed to handle text related operations. Some functions of class Text
are as follows:
Class name
Data members/instance variables:

Text
txt-to store the given string.
(You may assume that it has

## 200 characters at most.)

Member functions/ methods :
Text( )
char char At(int i)
int length( )
int noOfWhiteSpaces( )

constructor
reads the given string from the input
returns the character at position i of the string
returns the length of the string.
returns the total number of white spaces in the

text.
int noOfWords( )

## returns the number of words in the text.

Specify the class Text giving the details of int noOfWhiteSpaces( )and int No Of
Words9 ) only. You may assume that the other functions/methods are written for you .
PROGRAM:
class Text
{
String txt;
int noofWhitespaces( )

{
int l;
int c=0;
char a;
l=txt.length( );
for(int i=0;i < 1;i++)
{
a=txt.charAt(i);
if(a==' '|| a=='\t')
c++;
}
System.out.println("TheNo.of WhiteSpaces"+c);
return c;
}
int Noofwords( )
{
int w,l,i;
w=1;
char a;
l=txt.length( );
for(i=0;i < 1;i++)
{
a=txt.charAt(i);
if(a==' '||a=='\t')
w++;
}
System.out.println("No.of Words"+w);
return w;
}
}
VARIABLE DESCRIPTION:
VARIABLE
txt
l
c
a
i
w

DATA TYPE
String
int(Integer)
int(Integer)
char(Characte
r)
int(Integer)
int(Integer)

USE