0% found this document useful (0 votes)
26 views

Import Java

Uploaded by

mairasworld202
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Import Java

Uploaded by

mairasworld202
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

PROGRAM 2 : SORTING NUMBERS USING SELECTION SORT METHOD

import java.util.*;
class SelSort
{
int a[],n;
SelSort(int nn)=]
{
n=nn;
a=new int[n];
}
void input()
{
Scanner sc=new Scanner(System.in);
int i;
System.out.println("enter "+ n +" numbers");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
}
void sort()
{
int i,j,t,small,pos;
for(i=0;i<n-1;i++)
{
small=a[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
t=a[i];
a[i]=a[pos];
a[pos]=t;
}
}
void display()
{
int i;
System.out.println("sorted array");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
int p;
System.out.println("enter size of array");
p=sc.nextInt();
SelSort ob=new SelSort(p);
ob.input();
ob.sort();
ob.display();
}
}

PROGRAM 3
A class Rearrange has been defined to modify a word by
bringing all the vowels in the word at the beginning
followed by the consonants.
Example:
ORIGINAL becomes OIIARGNL
Some of the members of the class are given below:
Class name: Rearrange
Data Member/instance variable:
wrd: to store a word
newwrd: to store the rearranged word
Member functions/methods:
Rearrange(): default constructor
void readword(): to accept the word in UPPER case
void arrange(): rearranges the word by bringing the
vowels at the beginning followed by consonants
void display(): displays the original word along with the
rearranged word
Specify the class Rearrange, giving the details of the
constructor(), void readword(), void arrange() and void
display(). Define the main() function to create an object
and all the functions accordingly to enable the task.

import java.util.*;
class Rearrange
{
String wrd;
String newwrd;
Rearrange()
{
wrd="";
newwrd ="";
}
void readword()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the word in Uppercase:");
wrd = sc.next();
}
void arrange()
{
int l=wrd.length();
char ch;
String vs ="";
String cs ="";
for(int i = 0; i < l; i++)
{
ch = wrd.charAt(i);
if(ch=='A'||ch=='E'||ch== 'I'||ch== 'O'||ch== 'U' )
vs=vs+ ch;
else
cs=cs+ ch;
}
newwrd = vs + cs;
}
void display()
{
System.out.println("Original word:" + wrd);
System.out.println("Rearranged word:" + newwrd);
}
public static void main()
{
Rearrange obj = new Rearrange();
obj.readword();
obj.arrange();
obj.display();
}
}
import java.util.*;
class VowelWord
{
String str;
int freq;

VowelWord()
{
str="";
int i,c=0;
char ch; String wd="";
StringTokenizer st=new StringTokenizer(str," !?.");
c=st.countTokens();
for(i=0;i<c;i++)
{
wd=st.nextToken();
if(wd.charAt(0)=='A' || wd.charAt(0)=='E' ||wd.charAt(0)=='I' ||
wd.charAt(0)=='O' ||wd.charAt(0)=='U')

freq++;
}
}

void display()
{
System.out.print("Frequency of words beginning with vowel=" +freq);
}
public static void main()
{
VowelWord ob=new VowelWord();
ob.readstr();
ob.freq_vowel();
ob.display();
}
}

1
{
if(n%i==0)
{
c++;
}
}
if (c==2)
return 1;
int r,rev=0;
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
return rev;
}
int isadam (int n)
{
int sq=1 , sqr=1,p;
sq= n*n;
p= reverse(n);
sqr=p*p;
int a= reverse(sqr);
if (a==sq)
return 1;
else
return 0;
}
void main ()
{
int i , c=0,m,n;
Scanner sc=new Scanner(System.in);
System.out.println("INPUT");
System.out.print("m=");
m=sc.nextInt();
System.out.print("n=");
n=sc.nextInt();
System.out.println("OUTPUT");
if (m>0)
{
System.out.println("the prime adam integers are");
for(i=m;i<=n;i++)
{
if(prime(i)==1 && isadam(i)==1)
{
System.out.print(i+"\t");
c++;
}
}
System.out.println();
System.out.println("Frequency of prime adam integer is "+c);
if(c==0)
System.out.println("NIL");
}
else
System.out.println("Invalid Input");
}
}
ALGORITHM
Step 1: Start
Step 2: Create a function to check if a number is prime or not.
Step 3: Create a function to store the reverse.
Step 4: Create a function to check if a number is an Adam number or not.
Step 5: Iterate through a range of numbers and check if each number is both
prime and Adam.
Step 6: If a number satisfies both conditions, print it.
Step 7: Create main function.
Step 8: stop.

A class Fibo has been designed to generate Fibonacci Series-


0,1,1,2,3,5,13……..
CLASS : Fibo
DATA MEMBERS:
start : int to store start value
end : int to store end value
MEMBER FUNCTIONS:
Fibo(): default constructor
void read(): to accept start and end
int fibo(int n): returns nth term of Fibonacci series using recursive technique
void display():displays Fibonacci series from start to end by invoking function
Fibo()
Define the main() function to create an object and all the functions accordingly
to enable the task.

import java.util.*;
class Fibo
{
int start,end;
Fibo()
{
start=0;
end=0;
}
void read()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter starting and end limit");
start=sc.nextInt();
end=sc.nextInt();
}
int fibo(int n)
{
if(n==1||n==0)
return n;
else
return(fibo(n-1)+fibo(n-2));
}
void display()
{
int i;
for(i=start;i<=end;i++)
{
System.out.println(fibo(i)+" ");
}
}
}

11 5 7
8 13 9 A class Trans is defined to find the transpose of a square matrix. A
1 6 20 transpose of a matrix is obtained by interchanging the elements of
rows and columns.
Example : If size of matrix is 3

ORIGINAL 11 8 1
TRANSPOSE 5 13 6
7 9 20

CLASS NAME : Trans


DATA MEMBERS:
arr[][]: to store numbers in array
m: to store array size
MEMBER METHODS:
Trans(int mm): to initialize m=mm
void fillarray(): to input numbers in array.
void transpose(): to create transpose of matrix.
void display(): display original matrix and transposed matrix by invoking
transpose()
Define the main() function to create an object and all the functions accordingly
to enable the task.

You might also like