0% found this document useful (0 votes)
31 views79 pages

Computer Project

The document contains various Java programs that perform string manipulations, array operations, and matrix calculations. Examples include removing vowels from a string, finding the longest word in a sentence, and checking for palindrome words. Additionally, it covers method overloading and sorting algorithms for arrays, as well as operations on two-dimensional arrays like transposing and checking for symmetry.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views79 pages

Computer Project

The document contains various Java programs that perform string manipulations, array operations, and matrix calculations. Examples include removing vowels from a string, finding the longest word in a sentence, and checking for palindrome words. Additionally, it covers method overloading and sorting algorithms for arrays, as well as operations on two-dimensional arrays like transposing and checking for symmetry.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 79

String Programs

Output:
Enter a string: Computer
Application
The new string after removing
all vowels is: Cmptr pplctns
import java.util.*;
public class RemoveVowels
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

// Accept a string from the user


System.out.print("Enter a string: ");
String str = in.nextLine();

// Remove all vowels from the string


String newStr =
str.replaceAll("[aeiouAEIOU]", "");

// Display the new string


System.out.println("The new string
after removing all vowels is: " + newStr);
}
}
Output:
Enter a string:
He was playing football
The longest word is: football
The length of the longest word is: 8
import java.util.Scanner;
public class LongestWord
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = sc.nextLine();
String longestWord = "";
int maxLength = 0;
for (String word : str.split(" "))
{
if (word.length() > maxLength)
{
longestWord = word;
maxLength = word.length();
}
}
System.out.println("The longest word
is:" + longestWord);
System.out.println("The length of the
longest word is:" + maxLength);
}}
Output:
Enter a sentence:
Victory is rare until success
The framed word is: Virus
import java.util.Scanner;
public class FrameWord
{
public static void main(String[] args)
{
Scanner scanner = new
Scanner(System.in);
// Accept a sentence from the user
System.out.println("Enter a
sentence:");
String sentence = scanner.nextLine();
String word = "";
int p;
p = sentence.length();
word += sentence.charAt(0); // Add
first
character of the sentence
for (int i = 0; i < p-1; i++)
if (sentence.charAt(i) == ' ' &&
sentence.charAt(i + 1) != ' ')
word += sentence.charAt(i + 1);
// Display the word
System.out.println("The framed word is:
" + word);
}}

Output:
Enter a sentence:
MOM AND DAD ARE NOT AT HOME
Palindrome words:
MOM
DAD
import java.util.Scanner;
public class PalindromeWords
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a
sentence:");
String sentence = sc.nextLine();
// Split the sentence into words
String[] words = sentence.split(" ");
System.out.println("Palindrome
words:");
for (int i = 0; i < words.length; i++)
{
String word = words[i];
int len = word.length();
boolean isPalindrome = true;
for (int j = 0; j < len / 2; j++)
{
if (word.charAt(j) !=
word.charAt(len - 1 - j))
{
isPalindrome = false;
break;
}
}
if (isPalindrome)
System.out.println(word);
}}}
Output:
Enter a string:
COMPuter
New word: cpmpvtfr
import java.util.Scanner;
public class ReplaceVowelsNext
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a string:");
String word =
in.nextLine().toLowerCase();
String result = "";
for (char ch : word.toCharArray())
{
if ("aeiou".indexOf(ch) != -1)
result += (char)(ch + 1);
else
result += ch;
}

System.out.println("New word: " +


result);
}
}
Output:
Enter a sentence:
advancements are ever changing
ADVANCEMENTS ARE EVER CHANGING
Total number of words starting
with letter 'A' = 2
import java.util.Scanner;
public class CountWordsStartingA
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a
sentence:");
String str =
in.nextLine().toUpperCase();
String[] words = str.split(" ");
int c = 0;
for (String word : words)
{
if (word.startsWith("A"))
c++;
}
System.out.println(str);
System.out.println("Total number of
words starting with letter 'A' = " + c);
}
}
Output:
Enter a sentence:
MODEM IS AN ELECTRONIC DEVICE
MODEM
DEVICE
Number of words containing
consecutive letters: 2
import java.util.Scanner;
public class ConsecutiveLetters
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a
sentence:");
String sentence =
in.nextLine().toUpperCase();
String[] words = sentence.split(" ");
int c = 0;
for (String word : words)
{
boolean hasConsecutive = false;
for (int i = 0; i < word.length() -
1; i++)
{
if (word.charAt(i + 1) -
word.charAt(i) == 1)
{
hasConsecutive = true;
break;
}
}
if (hasConsecutive)
{
System.out.println(word);
c++;
}
}
System.out.println("Number of words
containing consecutive letters: " + c);
}
}
1-D Array
Programs
Output:
Enter the numbers in ascending
order: 12 31 42 45 64 65 76 81
87 92
Enter the number to be
searched: 87
The search is successful and
the number is present.
import java.util.*;
public class B_Search
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i,k=0,p=0,ns,lb=0,ub=9;
int m[]=new int[10];
for (i = 0; i < 10; i++)
{
System.out.print("Enter the numbers
in ascending order: ");
m[i] = in.nextInt();
}
System.out.print("Enter the number to
be searched: ");
ns = in.nextInt();
while(lb<=ub)
{
p=(lb + ub)/2;
if(m[p]<ns)
lb = p + 1;
if (m[p] > ns)
ub = p - 1;
else
{
k = 1;
break;
}
}
if (k == 1)
System.out.println("The search is
successful and the number is present.");
else
System.out.println("The search is
unsuccessful and the number is not present.");
}
}
Output:
Enter numbers in the cells: 10
4 1 2 3 4 6 3 6 3
The numbers arranged in
ascending order are:
1
2
3
3
3
4
4
6
6
10
import java.util.*;
public class Sort
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i, j, t;
int m[] = new int[10];

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


{
System.out.print("Enter numbers in
the cells: ");
m[i] = in.nextInt();
}

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


{
for (j = 0; j < (9 - i); j++)
{
if (m[j] > m[j + 1])
}
t = m[j];
m[j] = m[j + 1];
m[j + 1] = t;
}
}
}

System.out.println("The numbers
arranged in ascending order are:");
for (i = 0; i < 10; i++)
System.out.println(m[i]);
}
}
Output:
Enter 20 integers one by one:
6 28 7 14 15 13 49 12 10 496 13 70 21 17
18 19 20 100 97 77
Menu Driven Program
1. To display all the perfect numbers
2. To display all the Buzz numbers
Enter your choice:
2
28
7
14
49
70
21
17
97
77
import java.util.*;
class Number
{
public static void main()
{
Scanner in = new Scanner(System.in);
int a[] = new int[20];
int n, s, i, j;
System.out.println("Enter 20 integers one
by one:");
for (i = 0; i < 20; i++)
a[i] = in.nextInt();
System.out.println("Menu Driven Program");
System.out.println("1. To display all the
perfect numbers");
System.out.println("2. To display all the
Buzz numbers");
System.out.println("Enter your choice:");
n = in.nextInt();
switch (n)
{
case 1:
for (i = 0; i < 20; i++)
{
s = 0;
for (j = 1; j < a[i]; j++)
{
if (a[i] % j == 0)
s = s + j;
}
if (s == a[i])
System.out.println(a[i]);
}
break;
case 2:
for (i = 0; i < 20; i++)
{
if (a[i] % 7 == 0 || a[i] % 10
== 7)
System.out.println(a[i]);
}
break;
default:
System.out.println("Invalid
choice!");
}
}
}
Output:
Enter the numbers in the cells
5 2 4 16 1 9 2 64 17 7
The numbers in ascending order
are:
1
2
2
4
5
7
9
16
17
64
import java.util.*;
public class Selection_Sort
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i,j,t,min;
int m[]=new int[10];
for(i=0;i<10;i++)
{
System.out.println("Enter the
numbers in the cells");
m[i]=in.nextInt();
}
for(i=0;i<9;i++)
{
min=i;
for(j=i+1;j<10;j++)
{
if(m[j]<m[min])
min=j;
}
t=m[i];
m[i]=m[min];
m[min]=t;
}
System.out.println("The numbers in
ascending order are:");
for(i=0;i<10;i++)
System.out.println(m[i]);
}
}
2-D Array
Programs
Output:
Enter the numbers of the matrix
12 21 13 14
24 41 51 33
61 11 30 29
59 82 41 76
The transpose of the matrix is:
12 24 61 59
21 41 11 82
13 51 30 41
14 33 29 76
import java.util.*;
public class transpose
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i, j;
int m[][] = new int[4][4];
int n[][] = new int[4][4];
System.out.println("Enter the numbers
of the matrix");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
m[i][j] = in.nextInt();
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
n[i][j] = m[j][i];
}
System.out.println("The transpose of
the matrix is:");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
System.out.print(n[i][j] + "
");
System.out.println();
}
}
}
Output:
Enter the numbers in a 4*4 matrix:
12 21 13 14
24 41 51 33
61 11 30 29
59 82 41 76
The sum of the left diagonal elements =
159
The sum of the right diagonal elements =
135
import java.util.*;
public class diagonals
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i, j, ld, rd;
ld = 0;
rd = 0;
int m[][] = new int[4][4];
System.out.println("Enter the numbers
in a 4*4 matrix:");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
m[i][j] = in.nextInt();
}
for (i = 0; i < 4; i++)
ld = ld + m[i][i];
for (i = 0; i < 4; i++)
rd = rd + m[i][3 - i];
System.out.println("The sum of the left
diagonal elements = " + ld);
System.out.println("The sum of the
right diagonal elements = " + rd);
}
}
Output:
Enter the numbers in a 3*3
matrix:
1 2 3
2 4 5
3 5 6
It is a Symmetric Matrix
import java.util.*;
public class symmetric
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i, j, k = 0;
int m[][] = new int[3][3];
int n[][] = new int[3][3];
System.out.println("Enter the numbers
in a 3*3 matrix:");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
m[i][j] = in.nextInt();
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
if (m[i][j] != m[j][i])
k = 1;
}
if (k == 0)
System.out.println("It is a
Symmetric Matrix");
else
System.out.println("It is not a
Symmetric Matrix");
}
}
Method Overloading
Programs
Output:
Enter number of terms of the
series:
4
Enter the value of x:
2
Sum of first series: 1.166665
Sum of second series:268040729
import java.util.*;
class Over_loading
{
void SumSeries(int n, double x)
{
double sum = 0.0d;
int i;
for (i = 1; i <= n; i++)
{
if (i % 2 == 0)
sum = sum - (double) x / i;
else
sum = sum + (double) x / i;
}
System.out.println("Sum of first
series: " + sum);
}
void SumSeries()
{
int i, p = 1, sum1 = 0;
for (i = 1; i <= 20; i++)
{
p = p * i;
sum1 = sum1 + p;
}
System.out.println("Sum of second
series: " + sum1);
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n;
double x;
System.out.println("Enter number of
terms of the series:");
n = in.nextInt();
System.out.println("Enter the value of
x:");
x = in.nextDouble();
Over_loading ob = new Over_loading();
ob.SumSeries(n, x);
ob.SumSeries();
}
}
Output:
1. obj.num_cal(4, 's');
The square is: 16

2. obj.num_cal(3, 5, 'p');
The product is: 15

3. obj.num_cal("blue",
"green");
Two strings are not equal
class Choice
{
void num_cal(int num, char ch)
{
if (ch == 's')
System.out.println("The square is:
" + (num * num));
else
System.out.println("The cube is: "
+ (num * num * num));
}

void num_cal(int a, int b, char ch)


{
if (ch == 'p')
System.out.println("The product is:
" + (a * b));
else
System.out.println("The sum is: " +
(a + b));
}
void num_cal(String str1, String str2)
{
if (str1.equals(str2))
System.out.println("Two strings are
equal");
else
System.out.println("Two string are
not equal");
}
}
Output:
Frequency of digit 5 in
2565685 = 3
Sum of even digits in 29865 =
16
import java.util.*;
class Number
{
void Number(int num, int d)
{
int f = 0, digit;
int num1 = num;
while (num > 0)
{
digit = num % 10;
if (digit == d)
f = f + 1;
num = num / 10;
}
System.out.println("Frequency of
digit " + d + " in " + num1 + " = " + f);
}

void Number(int n)
{
int d1, sum = 0;
int n1 = n;
while (n > 0)
{
d1 = n % 10;
if (d1 % 2 == 0)
sum = sum + d1;
n = n / 10;
}
System.out.println("Sum of even
digits in " + n1 + " = " + sum);
}
}
Constructor/
Encapsulation
Programs
Output:
Enter Employee's Name, Employee
No. and Basic Salary :
John
E106
50000
Name : John
Employee Number : E106
Gross Salary : Rs72500.0
Net Salary : Rs66500.0
import java.util.*;
public class a4
{
String name, empno;
int basic;
double da, hra, pf, gs, net;
a4 (String n, String en, int bs)
{
name = n;
empno = en;
basic = bs;
}
void compute()
{
da = basic*30.0/100.0;
hra = basic*15.0/100.0;
pf = basic*12.0/100.0;
gs = basic+da+hra;
net = gs-pf;
}
void display()
{
System.out.println("Name : " + name);
System.out.println("Employee Number :
" + empno);
System.out.println("Gross Salary :
Rs" + gs);
System.out.println("Net Salary : Rs"
+ net);
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String nm, enm;
int bsal;
System.out.println("Enter Employee's
Name, Employee No. and Basic Salary :");
nm = in.nextLine();
enm = in.next();
bsal = in.nextInt();
a4 ob = new a4(nm, enm, bsal);
ob.compute();
ob.display();
}
}
Output:
Enter Name, Age, Marks in
three subjects one by one:
Alice
20
85
90
78
Name : Alice
Age : 20 years
Marks in three subjects : 85
90 78
The highest marks in three
subjects : 90
The average marks : 84.33
import java.util.*;
public class a5
{
String name;
int age, m1, m2, m3, max;
double avg;
a5(String n, int d, int a, int b, int c)
{
name = n;
age = d;
m1 = a;
m2 = b;
m3 = c;
}
void compute()
{
avg = (m1 + m2 + m3) / 3.0;
if (m1 >= m2 && m1 >= m3)
max = m1;
else if (m2 >= m1 && m2 >= m3)
max = m2;
else
max = m3;
}
void display()
{
System.out.println("Name : " + name);
System.out.println("Age : " + age + "
years");
System.out.println("Marks in three
subjects : " + m1 + " " + m2 + " " + m3);
System.out.println("The highest marks
in three subjects : " + max);
System.out.println("The average marks :
" + avg);
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String nm;
int ag, a1, b1, c1;
System.out.println("Enter Name, Age,
Marks in three subjects one by one:");
nm = in.nextLine();
ag = in.nextInt();
a1 = in.nextInt();
b1 = in.nextInt();
c1 = in.nextInt();

a5 ob = new a5(nm, ag, a1, b1, c1);


ob.compute();
ob.display();
}
}
Output:
Enter year, title and rating
2023
Avengers: Endgame
4.7
Avengers: Endgame Super Hit
import java.util.*;
class MovieMagic
{
int year;
String title;
float rating;
Scanner in = new Scanner(System.in);
MovieMagic()
{
year = 0;
rating = 0.0F;
title = "";
}
void accept()
{
System.out.println("Enter year, title
and rating");
year = in.nextInt();
in.nextLine(); // Consume leftover
newline
title = in.nextLine();
rating = in.nextFloat();
}
void display()
{
if (rating <= 2.0)
System.out.println(title + "\t" +
"Flop");
else if (rating >= 2.1 && rating <=
3.4)
System.out.println(title + "\t" +
"Semi-Hit");
else if (rating >= 3.5 && rating <=
4.5)
System.out.println(title + "\t" +
"Hit");
else if (rating >= 4.6 && rating <=
5.0)
System.out.println(title + "\t" +
"Super Hit");
}
public static void main(String[] args)
{
MovieMagic ob = new MovieMagic();
ob.accept();
ob.display();
}
}

You might also like