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

Program_Project

java programing class 11th icse project file with questions & answers and written programs

Uploaded by

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

Program_Project

java programing class 11th icse project file with questions & answers and written programs

Uploaded by

Rakshit Soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Program 1- Write a program to accept a string. Convert the string to uppercase.

Count and
output the number of double letter sequences that exist in the string. Sample Input: “SHE WAS
FEEDING THE LITTLE RABBIT WITH AN APPLE
Answer:
import java.util.Scanner;
class LetterSeq
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter string: ");
String s = in.nextLine();
String str = s.toUpperCase();
int count = 0;
int len = str.length();
for (int i = 0; i < len - 1; i++) {
if (str.charAt(i) == str.charAt(i + 1))
count++;
}
System.out.println("Double Letter Sequence Count = " + count);
}
}

Program 2- Write a program to input 10 numbers into an integer array and interchange the
largest number with the smallest number within the array and print the modified array. Assume
that there is only one largest and smallest number. For example, if array contains
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
9 12 3 7 67 34 15 16 89 15
After interchange it should have the elements arranged as:
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
9 12 89 7 67 34 15 16 3 15
Answer:
import java.util.Scanner;

class Q3
{
public static void main(String args[])
{
int a[]=new int[10], i,ln=0,lnp=0,sn=0,snp=0;
Scanner sc=new Scanner(System.in);

System.out.println("Enter 10 numbers");
for(i=0; i<10; i++)
{
a[i]=sc.nextInt();
}

for(i=0; i<10; i++)


{
if(i==0)
{
ln=a[i];
sn=a[i];
lnp=i;
snp=i;
}
else if(a[i]>ln)
{
ln=a[i];
lnp=i;
}
else if(a[i]<sn)
{
sn=a[i];
snp=i;
}
}

a[lnp]=sn;
a[snp]=ln;

System.out.println("\nArray after interchanging largest with the smallest");


for(i=0; i<10; i++)
{
System.out.print(a[i]+" ");
}
}
}

Program 3- Write a program to input 10 numbers into an integer array and interchange the
consecutive elements in it. That is, interchange a[0] with a[1], a[2] with a[3], a[4] with a[5] …
For example, if array contains
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
9 12 3 7 89 34 15 16 67 25
After interchange it should have the elements arranged as:
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
12 9 7 3 34 89 16 15 25 67

Answer:
import java.util.Scanner;

public class Q8
{
public static void main(String args[])
{
int a[]=new int[10], i,t;
Scanner sc=new Scanner(System.in);

System.out.println("Enter 10 numbers");
for(i=0; i<10; i++)
{
a[i]=sc.nextInt();
}
for(i=0; i<10; i=i+2)
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
System.out.println("\nModified array after interchanging the consecutive numbers");
for(i=0; i<10; i++)
{
System.out.print(a[i]+" ");
}
}
}
Program 4- Write a program to accept the names of 10 cities in a single dimension string array
and their STD (Subscribers Trunk Dialing) codes in another single dimension integer array.
Search for a name of a city input by the user in the list. If found, display “Search Successful” and
print the name of the city along with its STD code, or else display the message “Search
Unsuccessful, No such city in the list’.
Answer:
import java.util.Scanner;

public class StdCodes


{
public static void main(String args[]) {
final int SIZE = 10;
Scanner in = new Scanner(System.in);
String cities[] = new String[SIZE];
String stdCodes[] = new String[SIZE];
System.out.println("Enter " + SIZE +
" cities and their STD codes:");

for (int i = 0; i < SIZE; i++) {


System.out.print("Enter City Name: ");
cities[i] = in.nextLine();
System.out.print("Enter its STD Code: ");
stdCodes[i] = in.nextLine();
}

System.out.print("Enter name of city to search: ");


String city = in.nextLine();

int idx;
for (idx = 0; idx < SIZE; idx++) {
if (city.compareToIgnoreCase(cities[idx]) == 0) {
break;
}
}

if (idx < SIZE) {


System.out.println("Search Successful");
System.out.println("City: " + cities[idx]);
System.out.println("STD Code: " + stdCodes[idx]);
}
else {
System.out.println("Search Unsuccessful");
}
}
}

Program 5-Write a program to store 6 element in an array P, and 4 elements in an array Q and
produce a third array R, containing all elements of array P and Q. Display the resultant array.
Answer:
import java.util.Scanner;

class P6
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

int P[] = new int[6];


int Q[] = new int[4];
int R[] = new int[10];
int i = 0;

System.out.println("Enter 6 elements of array P:");


for (i = 0; i < P.length; i++) {
P[i] = in.nextInt();
}

System.out.println("Enter 4 elements of array Q:");


for (i = 0; i < Q.length; i++) {
Q[i] = in.nextInt();
}

i = 0;
while(i < P.length) {
R[i] = P[i];
i++;
}

int j = 0;
while(j < Q.length) {
R[i++] = Q[j++];
}

System.out.println("Elements of Array R:");


for (i = 0; i < R.length; i++) {
System.out.print(R[i] + " ");
}
}
}
Program 6. Write a program to input numbers into a 5×5 integer matrix and print the largest
and smallest number from it.
Answer:
import java.util.*;
class P7
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[][] = new int[5][5];
int i,j,max=0,min=0;
System.out.println("enter 25 numbers:");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
a[i][j]=sc.nextInt();
}
}

for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(i==0 && j==0)
max=min=a[i][j];
if(a[i][j]>max)
max=a[i][j];
if(a[i][j]<min)
min=a[i][j];
}

}
System.out.println("Largest number: "+max);
System.out.println("Smallest number: "+min);
}
}

Program 7. Write a program to input numbers into a 5×5 integer matrix and print the largest
and the smallest number among both the diagonals.
Answer:
import java.util.*;
class diagonaldisplay
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n[][] = new int[5][5];
int i,j,s;
System.out.println("Enter numbers in array:");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
n[i][j]=sc.nextInt();
}
}
System.out.println("Elements of array:");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
System.out.print(n[i][j]+"\t");
}
System.out.println();
}
int ld1 = n[0][0], sd1 = n[0][0];
int ld2 = n[0][0], sd2 = n[0][4];
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(i==j)
{
if(n[i][j]>ld1)
ld1=n[i][j];
if(n[i][j]<sd1)
sd1=n[i][j];
}
if(i+j==4)
{
if(n[i][j]>ld2)
ld2=n[i][j];
if(n[i][j]<sd2)
sd2=n[i][j];
}
}
}
System.out.println("Largest number of diagonal 1 : "+ld1);
System.out.println("Smallest number of diagonal 1 : "+sd1);
System.out.println("Largest number of diagonal 2 : "+ld2);
System.out.println("Smallest number of diagonal 2 : "+sd2);
}
}
Program 8- Write a program to input and sort the weight of ten people. Sort and display them in
descending order using the selection sort technique.
Answer:
import java.util.Scanner;

class SelectionSort
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double weightArr[] = new double[10];
System.out.println("Enter weights of 10 people: ");
for (int i = 0; i < 10; i++) {
weightArr[i] = in.nextDouble();
}

for (int i = 0; i < 9; i++) {


int idx = i;
for (int j = i + 1; j < 10; j++) {
if (weightArr[j] > weightArr[idx])
idx = j;
}

double t = weightArr[i];
weightArr[i] = weightArr[idx];
weightArr[idx] = t;
}

System.out.println("Sorted Weights Array:");


for (int i = 0; i < 10; i++) {
System.out.print(weightArr[i] + " ");
}
}
}

Program 9- Write a program to input 10 numbers into a float type array and arrange the
numbers in ascending order using Bubble Sorting technique.
Answer:
import java.util.Scanner;

class BubbleSortDsc
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = 10;
int arr[] = new int[n];

System.out.println("Enter the elements of the array:");


for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}

//Bubble Sort
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}

System.out.println("Sorted Array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}

Q10. 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 freq_vow_con(): finds the frequency of vowels and consonants in the word and displays
them with an
appropriate message
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
freq_vow_con(), void arrange() and void display(). Define the main() function to create an object
and call the functions accordingly to enable the task.
ANSWER:-

import java.io.*;
import java.util.*;
class Rearrange
{
private String wrd;
private String newwrd;
public Rearrange()
{
wrd =new String();
newwrd = new String();
}
public void readword() throws IOException
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the words");
wrd=sc.next();
}
public void freq_vow_con()
{
wrd=wrd.toUpperCase();
int v=0;
int c=0;
for(int i=0;i<wrd.length();i++)
{
char ch=wrd.charAt(i);
if ( Character.isLetter(ch))
{
switch (ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
v++;
break;
default:
c++;
}
}
}
System.out.println("Frequency of vowels"+v);
System.out.println("Frequency of consonants"+c);
}
public void arrange()
{
String v=new String();
String c=new String();
wrd = wrd.toUpperCase();
for(int i=0;i<wrd.length();i++)
{
char ch=wrd.charAt(i);
if ( Character.isLetter(ch))
{
switch (ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
v+=ch;
break;
default:
c+=ch;
}
}
}
newwrd=v+c;
}
public void display()
{
System.out.println("Original word"+wrd);
System.out.println("Rearranged word:"+newwrd);
}
public static void main(String []args) throws IOException
{
Rearrange obj=new Rearrange();
obj.readword();
obj.freq_vow_con();
obj.arrange();
obj.display();
}
}

Q11. Design a class ArmNum to check if a given number is an Armstrong number or not. (A
number is said to be Armstrong if sum of its digits raised to the power of length of the number
is equal to the number].
Example: 371 = 33+ 73 + 13
1634 = 14 + 64 + 34 + 44
54748= 55 + 45 + 75 + 45 + 85
Thus, 371, 1634 and 54748 are all examples of Armstrong numbers.
Some of the members of the class are given below:
Class name: ArmNum
Data members/instance variables:
n: to store the number
I: to store the length of the number
Methods/Member functions:
ArmNum (intnn): parameterized constructor to initialize the data member n = nn
intsum_pow(inti): returns the sum of each digit raised to the power of the length of the
number using recursive
technique eg., 34 will return 32 + 42 (as the length of the number is 2)
void isArmstrong(): checks whether the given number is an Armstrong number by invoking the
function sum_pow()
and displays the result with an appropriate message.
Specify the class ArmNum giving details of the constructor, intsum_pow(int) and void
isArmstrong( ).
Define a main() function to create an object and call the functions accordingly to enable the
task.
ANSWER:-

import java.io.*;
import java.util.*;
class ArmNum
{
static int n;
static int l;
public ArmNum(int num)
{
n=num;
l=0;
for(int i=n;i!=0;i/=10)
l++;
}
public int sumPow(int i)
{
if(i<10)
return (int)Math.pow(i,1);
return (int)Math.pow(i%10,1)+ sumPow(i/10);
}
public void isArmstrong()
{
if(n==sumPow(n))
System.out.println(n+"is an Armstrong Number");
else
System.out.println(n+"is not an Armstrong Number");
}
public static void main(String []args)throws IOException
{
Scanner sc=new Scanner(System.in);
System.out.print("N=");
int num=sc.nextInt();
ArmNum obj=new ArmNum(num);
obj.isArmstrong();

}
}

Q12. Design a class Perfect to check if a given number is a perfect number or not. [A number is
said to be perfect if sum of the factors of the number excluding itself is equal to the original
number]
Example: 6 = 1+2+3 (where 1,2 and 3 are factors of 6, excluding itself)
Some of the members of the class are given below:
Class name: Perfect
Data members/instance variables:
num: to store the number
Methods/Member functions:
Perfect (intnn): parameterized constructor to initialize the data member num=nn
intsum_of_factors(inti): returns the sum of the factors of the number(num), excluding itself,
using a recursive
technique
Void check(): checks whether the given number is perfect by invoking the function
sum_of_factors() and displays
the result with an appropriate message.
Specify the class Perfect giving details of the constructor(), intsum_of_factors(int) and void
check().
Define a main() function to create an object and call the functions accordingly to enable
ANSWER:-

import java.io.*;
import java.util.*;
class Perfect
{
private int num;
private int f;
public Perfect(int num)
{
this.num=num;
f=1;
}
public int sumOfFactors(int i)
{
if(i==f)
{
return 0;
}
else if(i%f==0)
{
return f++ + sumOfFactors(i);
}
else
{
f++;
return sumOfFactors(i);
}
}
public void check()
{
if (num==sumOfFactors(num))
{
System.out.println(num+"is a perfect number");
}
else
{
System.out.println(num+"Not a perfect number");
}
}
public static void main (String []args) throws IOException
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter the number:");
int n=sc.nextInt();
Perfect obj= new Perfect(n);
obj.check();

}
}

Q13. A class Palin has been defined to check whether a positive number is a Palindrome number
or not.
The number N is palindrome if the original number and its reverse are the same.
Some of the members of the class are given below:
Class name: Palin
Data members/instance variables:
num: integer to store the number
revnum: integer to store the reverse of the number
Methods/Member functions:
Palin(): constructor to initialize data members with legal initial values
void accept(): to accept the number
int reverse(int y): reverses the parameterized argument y' and stores it in revenue using a
recursive technique
void check(): checks whether the number is a Palindrome by invoking the function reverse() and
display the result
with an appropriate message.
Specify the class Palin giving the details of the constructor(), void accept(), int reverse(int) and
void check(). Define
the main() function to create an object and call the functions accordingly to enable the task.
ANSWER:-

import java.io.*;
import java.util.*;
class Palin
{
int num;
int revnum;
Palin()
{
num=0;
revnum=0;
}
void accept() throws IOException
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter the number:");
String a=sc.next();
num=Integer.parseInt(a);
}
int reverse(int y)
{
int len=(y+"").length();
if(len==1)
{
return y;
}
else
{
return (((y%10)*(int)Math.pow(10,len-1))+reverse(y/10));
}
}
void check()
{
revnum=reverse(num);
if(num==revnum)
{
System.out.println("/n Number is palindrome");
}
else
{
System.out.println("/n Number is not palindrome");
}
}
public static void main (String args[]) throws IOException
{
Palin p=new Palin();
p.accept();
p.check();
}
}

Q14. A class SwapSort has been defined to perform string related operations on a word input.
Some of the members of the class are as follows:
Class name: SwapSort
Data members/instance variables:
wrd: to store a word
len: integer to store the length of the word
swapwrd: to store the swapped word
sortwrd: to store the sorted word
Member functions/methods:
SwapSort():default constructor to initialize data members with legal initial values
void readword(): to accept a word in UPPER CASE
void swapchar(): to interchange/swap the first and last characters of the word in 'wrd' and
stores the new word in '\ Swapwrd'
void sortword(): sorts the characters of the original word in alphabetical order and stores it in
'sortwrd'
void display():displays the original word, swapped word and the sorted word
Specify the class SwapSort, giving the details of the constructor(). void readword().,
void swapchar(), voidsortword()
and void display(). Define the main() function to create an object and call the functions
accordingly to enable the task.
ANSWER:-

import java.io.*;
import java.util.*;
class SwapSort
{
String wrd;
int len;
String swapwrd;
String sortwrd;
SwapSort()
{
wrd="";
len=0;
swapwrd="";
sortwrd="";
}
void readword() throws IOException
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter word");
wrd=sc.next();
}
void swapwrd()
{
String w=wrd;
swapwrd=w.charAt(w.length()-1)+w.substring(1,w.length()-1)+w.charAt(0);
}
void sortwrd()
{
String w=wrd;
char[] charArray=w.toCharArray();
int length=charArray.length;
for(int i=0;i<length;i++)
{
for (int j=0;j<length;j++)
{
if(charArray[j]<charArray[i])
{
char temp=charArray[i];
charArray[i]=charArray[j];
charArray[j]=temp;
}
}
}
for(char c:charArray)
{
sortwrd=sortwrd+c;
}
}
void display()
{
System.out.println("Original word"+wrd);
System.out.println("Swapped word"+swapwrd);
System.out.println("Sorted Array"+sortwrd);
}
public static void main(String []args) throws IOException
{
SwapSort obj=new SwapSort();
obj.readword();
obj.swapwrd();
obj.sortwrd();
obj.display();
}
}
Q15. A disarium number is a number in which the sum of the digits to the power of their
respective position is equal to the
number itself. Example: 135 = 11 + 32 + 53
Hence, 135 is a disarium number.
Design a class Disarium to check if a given number is a disarium number or not.
Some of the members of the class are given below:
Class name: Disarium
Data members/instance variables:
intnum: stores the number
int size: stores the size of the number
Methods/Member functions: Disarium (intnn): parameterized constructor to initialize the data
members n= nn and size = 0
void countDigit(): counts the total number of digits and assigns it to size
intsumofDigits (intn, int p): returns the sum of the digits of the number(n) to the power of their
respective
positiors (p) using recursive technique
Void check(): checks whether the number is a disarium number and displays the result with an
appropriate message
Specify the class Disarium giving the details of the constructor),void countDigit(),
intsumofdigits(int,int) and
void check().
Define the main() function to create an object and call the functions accordingly to enable the
task.
ANSWER:-

import java.io.*;
import java.util.*;
class Disarium
{
public static void main (String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int copy=n,d=0,sum=0;
String s=Integer.toString(n);
int len=s.length();
while(copy>0)
{
d=copy%10;
sum=sum+(int)Math.pow(d,len);
len--;
copy=copy/10;
}
if (sum==n)
System.out.println("Is a disarium number");
else
System.out.println("Is not a disarium number");

}
}

You might also like