Computer Java
Computer Java
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
freq[ ]
of numbers.
Member functions :
PrimeFac( )
void enter( )
void frefac( )
the numbers stored
assign it to freq[ ]
void disp( )
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
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i < 5;i++)
{
System.out.println("Enter a Number");
num[i]=Integer.parseInt(br.readLine());
}
}
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
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;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void enter( )throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter size of Array");
n=Integer.parseInt(br.readLine( ));
a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine( ));
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");
item=Integer.parseInt(br.readLine( ));
System.out.println("Enter position");
Pos1=Integer.parseInt(br.readLine( ));
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");
Pos2=Integer.parseInt(br.readLine( ));
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
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);
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
void readstring() : reads the given string from the input
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;
}
void readstring()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter string");
str=br.readLine();
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:
PROGRAM:
class Modify
{
String st;
int len;
void read(String s)
{
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
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
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( )
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