Projectpgy (With Comment)
Projectpgy (With Comment)
Arrange all the alphabets of the string in such a way that all the lower case
characters are followed by the upper case characters.
import java.util.Scanner;
class casesorter
{
void main()
{
Scanner sc=new Scanner(System.in); /*to input*/
String s,low="",up="";
char c;
int i,l;
System.out.println("Enter your string "); /* to enter string*/
s=sc.nextLine();
l=s.length(); /*to enter length*/
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(Character.isUpperCase(c))
up=up+c;
if(Character.isLowerCase(c))
low=low+c;
}
System.out.println(" the new sting formed is:" +"\t" + low+up);
}
}
OUTPUT 1:-
Enter a string
AAAaaa
aaaAAA
Enter a string
mySTUDent
myentSTUD
Enter a string
MyTEACher
yherMTEAC
Program 2: Write a program in bluej to accept a string,frame a word by taking
the first character of each word and check whether the new word is a palindrome or
not.
import java.util.Scanner;
class checkpalindrome
{
void main()
{
Scanner Sc=new Scanner(System.in); /*to input*/
String s,t="",p="";
char c,d,e;
int i,l,a,b;
System.out.println("enter a string"); /* to enter string*/
s=Sc.nextLine();
s=" "+s;
l=s.length();
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(c==' ') , /*checking if c is equal to' '*/nh
{
d=s.charAt(i+1);
t=t+d;
}
a=t.length();
for(b=0;b<a;b++)
{
e=t.charAt(b);
p=e+p;
}
if(t.equalsIgnoreCase(p))
System.out.println("The new word such formed is palindrome");
else
System.out.println("The new word such formed is not palindrome");
}
}
OUTPUT 2:-
Enter a string
mad a mad a mad
it is a palindrome string
Enter a string
mad a mad a d
it is not a palindrome string
Enter a string
why u why
it is a palindrome string
Program 3 : Write a program in bluej to accept a string, which includes some special
characters like punctuation mark viz. apostrophe(), full stop(.).comma(,),
semicolon(:) etc. and display the string in reversed order without using punctuation
mark.
import java.util.Scanner;
class specialscharacters
{
void main()
{
Scanner sc=new Scanner(System.in); /*to input*/
String s,str="",t="",newst="";
char c,d;
int i,l,k;
System.out.println(" Enter your string"); /* to enter string*/
s=sc.nextLine();
l=s.length(); /* to obtain length*/
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(Character.isLetter(c) || (Character.isWhitespace(c)))
str=str+c;
}
str=str+' ';
k=str.length();
for(i=0;i<k;i++)
{
d=str.charAt(i);
if(Character.isLetter(d))
t=t+d;
if(Character.isWhitespace(d))
{
newst= " "+t+" "+newst+" ";
t="";
}
}
System.out.println( " the reversed new string is : " + newst);
}
}
OUTPUT 3:-
Enter a string
'emotions', controlled, 'controlled' and 'directed to work' show the greatness of swami
Vivekananda.
Vivekananda swami of greatness the show work to directed and controlled
controlled emotions
Program 4 : Write a program in bluej to input natural numbers between 1 to 1000 and
then output in words.
import java.util.*;
class naturalnumbers
{
void main()
{
Scanner Sc=new Scanner(System.in); /*to input*/
int n;
System.out.println("Enter a natural number"); /*to enter number*/
n=Sc.nextInt(); /* to extract new integer*/
int r,m=n;
String str="",st="";
if(n>=1 && n<=1000)
{
while(n!=0)
{
r=n%10;
if(r==0)
str="zero";
if(r==1)
str="one";
if(r==2)
str="two";
if(r==3)
str="three";
if(r==4)
str="four";
if(r==5)
str="five";
if(r==6)
str="six";
if(r==7)
str="seven";
if(r==8)
str="eight";
if(r==9)
str="nine";
st=' '+str+st;
n=n/10;
}
System.out.println("Number "+m+" in letters="+st);
}
else
System.out.println("Number "+m+" not in range");
}
}
OUTPUT 4:-
import java.util.*;
class descendingarray
{
void main()
{
Scanner scanner = new Scanner(System.in); /*to input*/
int[] arr = new int[11];
int i, j, temp;
System.out.println("Enter 10 integers:"); /*to enter 10 integers*/
for (i = 0; i < 10; i++)
{
arr[i] = scanner.nextInt();
}
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9 - i; j++)
{
if (arr[j] < arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("Enter another number:"); /* to display to enter another number*/
int newNumber = scanner.nextInt();
int pos = 10;
for (i = 0; i < 10; i++)
{
if (newNumber > arr[i])
{
pos = i;
break;
}
}
Enter 10 integers
3
4
5
6
7
90
10
8
2
3
Enter another no.
100
Displaying the arrray in descending order:-
100
90
10
8
7
6
5
4
3
3
2
Program 6 : Write a program in bluej to store n integers (n>0) in a S.D.A.
and store only palindrome numbers in another array. Display the new array.
import java.util.*;
class palindromearray
{
void main(int n)
{
Scanner Sc=new Scanner(System.in); /*to input*/
int i,j=0;
int b[]=new int[n];
int a[]=new int[n];
System.out.println("Enter your numbers"); /*to enter numbers*/
for(i=0;i<n;i++)
{
a[i]=Sc.nextInt();
}
for(i=0;i<n;i++)
{
int r,p=0,m=a[i],temp=a[i];
while(temp!=0)
{
r=temp%10;
temp=temp/10;
p=p*10+r;
}
if(p==m) /* checking if p is equal to m*/
{
b[j]=m;
j++;
}
}
System.out.println("Displaying the array containing only palindrome
number:-");
for(i=0;i<j;i++)
{
System.out.println(b[i]);
}
}
}
OUTPUT 6:-
Enter numbers
101
109
890
787
7
777
9876
11
000
909
Displaying the array containing only palindrome number:-
101
787
7
777
11
0
909
Program 7 : Write a method that takes a number in decimal number system
as argument and returns its binary equivalent.
import java.util.* ;
class DecimalToBinary
{
void main()
{
Enter a number
100
The binary equivalent of the number 100 = 1100100
Enter a number
678
The binary equivalent of the number 678 = 1010100110
Enter a number
9009
The binary equivalent of the number 9009 = 10001100110001
Program 8 : Write a method in bluej that takes a binary equivalent number
as argument and returns its decimal equivalent.
import java.util.*;
class binarytodecimal
{
void main()
{
class perfect
{
int n;
perfect()
{
n=0;
}
perfect(int a)
{
n=a;
}
void perfect_sq()
{
int c=0;
int num=(int)Math.sqrt(n)+1;
System.out.println("The next 5 perfect squares which are greater than the
inputed number "+n+":-");
while(c<5)
{
int perfectsquare=num*num; /* to do square*/
System.out.println(perfectsquare); /*to print the perfect square*/
c++;
num++;
}
}
void sum_of()
{
int a,i;
boolean found=false;
System.out.println("Displaying all the combitions of consecutive integers whose
sum is equal to the inputed number:-");
for(a=1;a<n;a++)
{
int s=0;
String sequence="";
for(i=a;i<n;i++)
{
s=i+s;
sequence=sequence+i+" ";
if(s==n) /*checking if s is equal to n*/
{
System.out.println(sequence);
found =true;
break;
}
else if(s>n)
break;
}
}
}
void main(int a)
{
perfect p1=new perfect(a);
p1.perfect_sq();
p1.sum_of();
}
}
OUTPUT 9:-
The next 5 perfect squares are which are greater than the inputed number 15:-
16
25
36
49
64
Displaying all the combitions of consecutive integers whose sum in equal to the
inputed number:-
12345
456
78
The next 5 perfect squares are which are greater than the inputed number 20:-
25
36
49
64
81
Displaying all the combitions of consecutive integers whose sum in equal to the
inputed number:-
23456
The next 5 perfect squares are which are greater than the inputed number 16:-
25
36
49
64
81
Displaying all the combitions of consecutive integers whose sum in equal to the
inputed number:-
Program 10 : In "Piglatin" a word such as KING is replaced by INGKAY, while
TROUBLE becomes OUBLETRAY and so on. The first vowel of the original word
becomes the start of the translation, any preceding letters being shifted towards the
end and followed by AY. Words that begin with a vowel or which do not contain any
vowel are left unchanged. Design a class Piglatin using the description of the data
members and member functions given below:
Specify the class Piglatin giving the details of the constructor, void readstring(),
void convert() and void consonant(). Also define the main function to create an
object and call methods accordingly to enable the task.
import java.util.Scanner;
class piglatin
{
String txt;
int len;
piglatin()
{
txt="";
len=0;
}
void readstring()
{
Scanner sc=new Scanner(System.in); /*to input*/
System.out.println("Enter your string in upper case"); /*to display to enter string in upper case*/
txt=sc.nextLine(); /*to input the string*/
txt=txt.toUpperCase();
len=txt.length();
}
void convert()
{
int i,k=0; char c,d=txt.charAt(0);String t="",p="";
for (i=1;i<len;i++)
{
c=txt.charAt(i);
if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
k++;
}
if(d=='A' || d=='E' || d=='I' || d=='O' || d=='U')
{
for( i=0;i<len;i++)
{
c=txt.charAt(i);
if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
{
p=txt.substring(i);
break;
}
else
t=t+c;
}
System.out.println("The pigltin form of the word is : " + p + t+ "AY");
}
}
void consonant()
{
int z=0;
char c;
int i;
len=txt.length();
for(i=0;i<len;i++)
{
c=txt.charAt(i);
if(c!='A' || c!='E' || c!='I' || c!='O' || c!='U')
z++;
}
System.out.println("The number of consonants in the string are : " + z);
}
void main()
{
piglatin p1=new piglatin();
p1.readstring();
p1.convert();
p1.consonant();
}
}
OUTPUT 10:-
Enter a string in Upper Case
KING
The pig latin form = INGKAQ
Number of consonants = 1
Enter a string in Upper Case
TROUBLE
The pig latin form = OUBLETRAQ
Number of consonants = 3
Enter a string in Upper Case
AMEN
The pig latin form = AMEN
Number of consonants = 2
Enter a string in Upper Case
HELLO
The pig latin form = ELLOHAQ
Number of consonants = 2
Program 11 : Write a program in bluej to input 100 numbers through the keyboard
using scanner class and display only those numbers which are palindrome. your
program must validate for integer numbers.
import java.util.*;
class palindromenumbers
{
void main()
{
Scanner Sc=new Scanner(System.in); /*to input*/
int a[]=new int[100];
int b[]=new int[100];
int i,j=0;
System.out.println("enter 100 integers"); /* to enter 100 numbers*/
for(i=0;i<100;i++)
{
a[i]=Sc.nextInt();
}
for(i=0;i<100;i++)
{
int r,p=0,m=a[i],temp=a[i];
while(temp!=0)
{
r=temp%10;
temp=temp/10;
p=p*10+r;
}
if(p==m) /*checking if c is equal to m*/
{
b[j]=m;
j++;
}
}
for(i=0;i<j;i++)
{
System.out.println(b[i]);
}
}
}
OUTPUT 11:-
enter 100 integers
1
2
3
4
5
6
7
8
9
0
1
2
1
3
1
4
5
2
3
53
465
769
856
77
88
99
11
22
3
3334
5
5
6
6
7
576
776
87
665
56
56
4545
555
5655
77
8
89
6766
54
55
555
666
121
232
43435
75
447
38454
4645
87454
9565
73643
8466
0687
9676
6986
607
9676
8647
84
8565
766
5554
765
647
86
968
99
999
88
7777
66565
76767
77
4847
75
8465
8464
585
8746565
4756
5665
6
56456
6456
98
9445
8545
454
45
1
2
3
4
5
6
7
8
9
0
1
2
1
3
1
4
5
2
3
77
88
99
11
22
3
5
5
6
6
7
555
77
8
55
555
666
121
232
99
999
88
7777
76767
77
585
5665
6
454
Program 12 : Write a method in bluej that takes two integers as argument
and returns its HCF using division method.
import java.util.*;
class HCF
{
int HCF(int a,int b)
{
int r,i;
System.out.print("The highest common factor=");
while(b!=0)
{
int temp=b;
b=a%b;
a=temp;
}
return a; /*returning the value of a*/
}
void main()
{
Scanner Sc=new Scanner(System.in); /*to input*/
int x,y;
System.out.println("Enter two numbers"); /* to display to enter 2 numbers*/
x=Sc.nextInt();
y=Sc.nextInt();
System.out.println(HCF(x,y));
}
}
OUTPUT 12:-
Enter two no.
36
24
The highest common factor=12
Enter two no.
45
30
The highest common factor=15
Enter two no.
350
100
The highest common factor=50
Program 13 : Write a program to input the date dd/mm/yyyy format and check
whether given date is valid or not. If date is valid display in mm/dd/yyyy format.
import java.util.Scanner;
class DateValidator
{
void main()
{
Scanner sc = new Scanner(System.in); /*to input*/
System.out.print("Enter the date in dd/mm/yyyy format: "); /* to enter the date as per given format*/
String date = sc.nextLine(); /*to enter string*/
if (date.length() != 10 || date.charAt(2) != '/' || date.charAt(5) != '/')
{
System.out.println("Invalid format. Please enter in dd/mm/yyyy format.");
return;
}
String dayStr = date.substring(0, 2);
String monthStr = date.substring(3, 5);
String yearStr = date.substring(6);
for (int i = 0; i < dayStr.length(); i++)
{
if (dayStr.charAt(i) < '0' || dayStr.charAt(i) > '9')
{
System.out.println("Invalid day.");
return;
}
}
for (int i = 0; i < monthStr.length(); i++)
{
if (monthStr.charAt(i) < '0' || monthStr.charAt(i) > '9')
{
System.out.println("Invalid month.");
return;
}
}
for (int i = 0; i < yearStr.length(); i++)
{
if (yearStr.charAt(i) < '0' || yearStr.charAt(i) > '9')
{
System.out.println("Invalid year.");
return;
}
}
int day = Integer.parseInt(dayStr);
int month = Integer.parseInt(monthStr);
int year = Integer.parseInt(yearStr);
if (year < 1)
{
System.out.println("Invalid year.");
return;
}
if (month < 1 || month > 12)
{
System.out.println("Invalid month.");
return;
}
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
daysInMonth[1] = 29;
}
if (day < 1 || day > daysInMonth[month - 1])
{
System.out.println("Invalid day.");
return;
}
System.out.println("Valid date!");
System.out.println("Date in mm/dd/yyyy format: " + month +"/" + day + "/" +
year );
}
}
OUTPUT 13
Enter the date in dd/mm/yyyy format: 22/02/2024
Valid date!
Date in mm/dd/yyyy format: 2/22/2024
Enter the date in dd/mm/yyyy format: 30/02/2078
Invalid day.
Enter the date in dd/mm/yyyy format: 29/02/2000
Valid date!
Date in mm/dd/yyyy format: 2/29/2000
Program 14 : Write a program in blue j to delete a number from a SDA.
import java.util.*;
class delete
{
void main()
{
Scanner Sc = new Scanner(System.in); /*to input*/
System.out.println("Enter how many numbers you want to input:");
int n = Sc.nextInt();
int a[] = new int[n];
int c[] = new int[n - 1];
int i, e = -1, f, j, s = 0, g, h, z;
System.out.println("Enter numbers:"); /* to display for entering numbers*/
for (i = 0; i < n; i++)
{
a[i] = Sc.nextInt();
}
System.out.println("Enter a number to deleted");
int b = Sc.nextInt();
for (i = 0; i < n; i++)
{
if (b == a[i])
{
e = i;
break;
}
}
if (e == -1)
{
System.out.println("Number not found in the array.");
return;
}
f = e - 1;
j = e - 1;
while (f >= 0) /* to check if f is greater than or equal to 0 */
{
c[s] = a[f];
f--;
s++;
}
g = s;
h = s;
z = g + 1;
while (z < n)
{
c[h] = a[z];
h++;
z++;
}
System.out.println("Displaying the new array after deleting the given number:");
for (i = j; i >= 0; i--)
{
System.out.println(c[i]);
}
for (i = j + 1; i < n - 1; i++)
{
System.out.println(c[i]);
}
}
}
OUTPUT 14:-
Enter how many numbers you want to input:
5
Enter numbers:
12
15
78
90
89
Enter a number to deleted
16
Number not found in the array.
Enter how many numbers you want to input:
5
Enter numbers:
89
17
16
15
14
Enter a number to deleted
14
Displaying the new array after deleting the given number:
89
17
16
15
Enter how many numbers you want to input:
7
Enter numbers:
90
89
78
67
56
45
34
Enter a number to deleted
45
Displaying the new array after deleting the given number:
90
89
78
67
56
Program 15 : Write a program in blue j to input a string and display the ASCII code
of each character.
import java.util.*;
class ascii
{
void main()
{
Scanner Sc=new Scanner(System.in); /*to input*/
String s;
char c;
int i,l,d;
System.out.println("enter a string"); /* to display to enter a string*/
s=Sc.nextLine();
l=s.length();
for(i=0;i<l;i++)
{
c=s.charAt(i); /*to extract characters*/
d=(int)c;
System.out.println("ASCII CODE OF "+c+" = "+d);
}
}
}
OUTPUT 15 :-
enter a string
raman
ASCII CODE OF r = 114
ASCII CODE OF a = 97
ASCII CODE OF m = 109
ASCII CODE OF a = 97
ASCII CODE OF n = 110
enter a string
junior
ASCII CODE OF j = 106
ASCII CODE OF u = 117
ASCII CODE OF n = 110
ASCII CODE OF i = 105
ASCII CODE OF o = 111
ASCII CODE OF r = 114
enter a string
sqrt
ASCII CODE OF s = 115
ASCII CODE OF q = 113
ASCII CODE OF r = 114
ASCII CODE OF t = 116
Program 16 : A company announces and increment of their employees on seniority
basis as per the given condition: -
Age Increment
Write a program to find new basic by using the following class specifications:
Class : Increment
Data members:
String name. double basic, int age
Member methods:
Void getdata(); to accept name, basic and age
Void calc() to find increment and update basic
Void display(): to display age and basic in the following format:
Age Basic
-------- ---------
Write main method to create object and call all the methods defined above.
import java.util.Scanner;
class RecurringPatterns
{
// Method to print pattern (a)
void patternA(int n)
{
int i,j;
for (i = 1; i <= n; i++)
{
// Print leading spaces
for ( j = 1; j <= n - i; j++)
{
System.out.print(" ");
}
// Print 'a'
for ( j = 1; j <= i; j++)
{
System.out.print("a ");
}
System.out.println();
}
for (i = n - 1; i >= 1; i--)
{
// Print leading spaces
for (j = 1; j <= n - i; j++)
{
System.out.print(" ");
}
// Print 'a'
for ( j = 1; j <= i; j++)
{
System.out.print("a ");
}
System.out.println();
}
}
// Method to print pattern (b)
void patternB(int n)
{
int i,j;
for ( i = 1; i <= n; i++)
{
// Print leading spaces
for ( j = i; j < n; j++)
{
System.out.print(" ");
}
// Print increasing numbers
for (j = 1; j <= i; j++)
{
System.out.print(j);
}
// Print decreasing numbers
for ( j = i - 1; j >= 1; j--)
{
System.out.print(j);
}
// Move to the next line
System.out.println();
}
// Lower part of the pyramid
for ( i = n - 1; i >= 1; i--)
{
// Print leading spaces
for (j = n; j > i; j--)
{
System.out.print(" ");
}
// Print increasing numbers
for ( j = 1; j <= i; j++)
{
System.out.print(j);
}
// Print decreasing numbers
for ( j = i - 1; j >= 1; j--)
{
System.out.print(j);
}
// Move to the next line
System.out.println();
}
}
// Method to print pattern (c)
void patternC(int n)
{
// Top part
int i,j;
for (i = 0; i < n; i++)
{
for ( j = 0; j < n - i; j++)
{
System.out.print((char) ('a' + j));
}
for (j = 0; j < 2 * i; j++)
{
System.out.print(" ");
}
for ( j = n - i - 1; j >= 0; j--)
{
System.out.print((char) ('a' + j));
}
System.out.println();
}
// Bottom part
for ( i = n - 2; i >= 0; i--)
{
for ( j = 0; j < n - i; j++)
{
System.out.print((char) ('a' + j));
}
for ( j = 0; j < 2 * i; j++)
{
System.out.print(" ");
}
for ( j = n - i - 1; j >= 0; j--)
{
System.out.print((char) ('a' + j));
}
System.out.println();
}
}
OUTPUT 17:-
Pattern A:
a
aa
aaa
aaaa
aaa
aa
a
Pattern B:
1
121
12321
1234321
12321
121
1
Pattern C:
abcdcba
abc cba
ab ba
a a
ab ba
abc cba
abcddcba
Program 18 : write a program in blue -j where there are two single subscripted
variable A and B. Array A contains integer numbers in ascending order and array b
contains in descending order.Write a program in bluej to merge these array into the
third array C in such a way that resultant array will contain the number in ascending
order. Without using either of the sorting technique.
import java.util.Scanner;
class MergeArrays
{
void main()
{
Scanner sc = new Scanner(System.in); /*to input*/
System.out.println("Enter size of array A:");
int sizeA = sc.nextInt();
System.out.println("Enter size of array B:");
int sizeB = sc.nextInt();
int[] A = new int[sizeA];
int[] B = new int[sizeB];
int z=sizeA+sizeB;
int[] C = new int[z];
System.out.println("Enter elements of array A in ascending order:");
for (int i = 0; i < sizeA; i++)
{
A[i] = sc.nextInt();
}
System.out.println("Enter elements of array B in descending order:");
for (int i = 0; i < sizeB; i++)
{
B[i] = sc.nextInt();
}
int i = 0, j = sizeB - 1, k = 0;
while (i < sizeA && j >= 0)
{
if (A[i] <= B[j]) /*checking if A[i]<=B[j]*/
{
C[k++] = A[i++];
} else
{
C[k++] = B[j--];
}
}
while (i < sizeA)
{
C[k++] = A[i++];
}
while (j >= 0) /*checking if j>=0*/
{
C[k++] = B[j--];
}
System.out.println("Merged array C in ascending order:");
for ( i=0;i<z;i++)
{
System.out.println(C[i]);
}
}
}
OUTPUT 18 :-
import java.util.Scanner;
class RemoveCommonCharacters
{
String removeCommon(String str)
{
String result = "";
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if (str.indexOf(ch) == str.lastIndexOf(ch))
{
result += ch;
}
}
return result;
}
void main()
{
Scanner sc = new Scanner(System.in); /*to input*/
RemoveCommonCharacters rcc = new RemoveCommonCharacters();
System.out.print("Enter a string: "); /* to display to enter a string*/
String input = sc.nextLine(); /*to input a string*/
String output = rcc.removeCommon(input);
System.out.println("String after removing common characters: " + output);
}
}
OUTPUT 19:-
import java.util.*;
class IMEI
{
void main()
{
Scanner Sc = new Scanner(System.in); /*to input*/
System.out.println("Enter a 15-digit IMEI number:");
long b = Sc.nextLong(); String l="";
l=Long.toString(b);
if (l.length() != 15)
{
System.out.println("Invalid input. IMEI number must be 15 digits.");
return;
}
int d = 0;
d += (e / 10) + (e % 10);
} else
{
d += x;
}
}
if (d % 10 == 0) /*dividing d by 10*/
{
System.out.println("The IMEI number is valid.");
} else
{
System.out.println("The IMEI number is invalid.");
}
}
}
OUTPUT 20:-
Enter a 15-digit IMEI number:
490154203237
Invalid input. IMEI number must be 15 digits.
Enter a 15-digit IMEI number:
490154203237518
The IMEI number is valid.