0% found this document useful (0 votes)
13 views81 pages

Combinepdf

The document is a project report titled 'Programs in JAVA' submitted by a student for the ICSE 2023-24 Computer Applications paper at Excel Central School. It includes a table of contents listing various Java programs covering topics like conditional statements, iteration, constructors, and array manipulations. Each program is designed to demonstrate different programming concepts and techniques in Java.

Uploaded by

chandran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views81 pages

Combinepdf

The document is a project report titled 'Programs in JAVA' submitted by a student for the ICSE 2023-24 Computer Applications paper at Excel Central School. It includes a table of contents listing various Java programs covering topics like conditional statements, iteration, constructors, and array manipulations. Each program is designed to demonstrate different programming concepts and techniques in Java.

Uploaded by

chandran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 81

PROGRAMS IN JAVA

Computer Applications

Project Report

Submitted by

Grade: X

Indian Certificate of Secondary Education


(ICSE) 2023-24

EXCEL CENTRAL SCHOOL


THIRUVATTAR-629 177
Kanyakumari District
EXCEL CENTRAL
SCHOOL
THIRUVATTAR-
629177
Kanyakumari District

CertifiCate

The project report entitled Programs in JAVA submitted by

…………………………………….. to meet the internal assessment of the

Indian Certificate of Secondary Education (ICSE) Examination 2024

Computer Applications paper at EXCEL CENTRAL SCHOOL, Thiruvattar,

Kanyakumari District – 629177 has been examined.

Internal Examiner External Examiner

Date :
TABLE OF CONTENTS

Ex. No. Title of the Program Page No.

1 Electricity Bill using Conditional Statement

2 Abundant or Deficient or Perfect Number using


Iteration
3 Menu driven (Choice based selection) using Iterative
Constructs
4 Tribonacci Series using Iteration

5 Displaying Sum using Overloaded Methods

6 Purchase using Constructor

7 Area of Shapes - Constructor

8 Volume of Sphere, Cylinder and Cone using


Overloaded Methods
9 Sum of Digits – Static Methods

10 Count ODD and EVEN Numbers in an SDA

11 Searching a number in an Array using Linear Search

12 Searching a number in an Array using Binary Search

13 Piglatin using String methods

14 Searching String using Linear Search


Ex. No. Title of the Program Page No.

15 Frequency of a Character in a String

16 Palindromic String

17 Counting letters, digits, whitespaces using Character


class methods
18 Implementing Scope of Data members and Member
methods
19 Manipulating String

20 Student Marks using Single Dimensional Arrays

21 String Program to Count number of Digits, Alphabets


and Special characters)
22 2D Array - Transpose of the matrix

23 Selection sort - Ascending order

24 2D Array - Sum, Min and Max

25 Bubble sort - Descending order


1. Define a class for Electricity Board that initializes the data members using
constructors.

Data members: month, name of the consumer, consumer number, previous


meter reading, present meter reading and net bill.

( units= present meter reading – previous meter reading)

No of
units Charge
first 100 units Rs. 1.20 per unit
Rs. 1.75 for every
next 120 units additional unit
Rs. 2.25 for every
otherwise additional unit

Calculate net bill with a meter rent of Rs. 100 is charged from each
consumer. A tax of 6% is charged on the sum of the rent and the bill amount.
The total amount is calculated by adding tax to the sum of the
amount and the rent. Display the details of the consumer.

2. If the sum of the proper divisors of a number is greater than the number
itself, then the number is called ABUNDANT OR EXCESSIVE NUMBER. If the
sum of the proper divisors of a number is less than the number itself, then
the number is called DEFICIENT OR DEFECTIVE NUMBER. If the sum of the
proper divisors of a number is equal to that number itself, then the number is
called
PERFECT NUMBER.
The proper divisors of 12 are 1, 2, 3, 4, and 6. Because the sum of its proper
divisors (1 + 2 + 3

+ 4 + 6 = 16) is greater than 12, 12 is an abundant number. Numbers like 8,


whose proper divisors have a sum (1+2+4=7) that is less than the number
itself, are called deficient. The proper divisors of 6 are 1, 2 and 3 and have
the sum 6. So 6 is a Perfect Number. Write a program to check whether the
given number is a Perfect or Deficient or Abundant number.

3. Write a menu driven program for the following

i) To display the sum of series (Get n values from the user)

Sum= 1 + 12 + 123 + 1234 + ..... + n terms.


3+0.5
ii) To Print the value of Z where = where x ranges from – 10 to 10 with

an increment of 2 and Y remains constant at 5.5.

4. Write a program to display the following series


A tribonacci series is one in which the sum next term is the sum of
previous three terms eg. 0 1 2 3 6 11 20………….
5. Design a class to overload a function sum( ) as follows:
(a) void sum (int x, int n) – To display the sum of two integers.
(b) void sum (char p, char x) – To display the sum of two characters
(ASCII)
(c) void sum ( float m, float n) – To display the sum of two real numbers

6. Design a class name ShowRoom with the following


description: Instance variables / Data members:
String name - To store the name of the customer
long mobno - To store the mobile number of
the customer double cost - To store the cost of
the items purchased double dis - To store the
discount amount
double amount - To store the amount to be paid
after discount Member methods:
ShowRoom( ) - default constructor to initialize
data members void input( ) - To input customer
name, mobile number, cost
void calculate( ) - To calculate discount on the cost of purchased items,
based on following criteria
Cost Discount (in Percentage)
Less than or equal to ` 10000 5%
More than `10000 and less than or equal to
` 20000 10%
More than `20000 and less than or equal to
` 35000 15%
More than ` 35000 20%
void display( ) - To display customer name , mobile number , amount to be
paid after discount.
Write a main method to create an object of the class and call the above
member methods.

7. Define a class named Shapes, having


members as: Data members: length,
breadth
Member Functions:
i) A parameterized constructor to initialize the sides of a square
ii) A parameterized constructor to initialize the sides of a rectangle
iii)Compute the area and display it.
Create the main() method to show the implementation of the above
methods.
8. Design a class to overload a function volume() as follows:
(i) double volume (double R) – with radius (R) as an argument, returns the
volume of sphere using the formula. V = 4/3 × 22/7 × R3
(ii)double volume (double H, double R) – with height(H) and radius(R) as
the arguments, returns the volume of a cylinder using the formula. V =
22/7 × R2 × H
(iii) double volume (double L, double B, double H) – with length(L),
breadth(B) and Height(H) as the arguments, returns the volume of a
cuboid using the formula. V = L × B × H

9. Create a class called Number which contains the following class functions
(i)static int sumOfDigits(int n) which returns the sum of the digits in the
integer n
(ii) static void call() to accept two integers using Scanner object and
find the sum of the digits of each integer.

10. Write a program to get n numbers in an array and count number of odd
and even numbers in the array.

11. Write a program to store n numbers. Get a number as search element and
locate its position, if present in the list using Linear Search else display an
appropriate message

12. Write a program to search for an integer value input by the user in the
sorted list given below using binary search technique. If found display ''Search
Successful'' and print the element, otherwise display ''Search Unsuccessful''.

{31, 36, 45, 50, 60, 75, 86, 90}

13.Write a program that encodes a word into Piglatin. To translate a word into a
Piglatin word, convert the word into upper case and then place the first vowel
of the original word as the start of the new word along with the remaining
alphabets. The alphabets present before the vowel
being shifted towards the end following by “YZ”.
Sample input : Flowers Sample Output: OWERSFLYZ
Sample input : Olympics Sample Output : OLYMPICSYZ

14. Write a program to initialize the seven Wonders of the World along with their
locations in two different arrays. Search for a name of the country input by the
user. If found, display the name of the country along with its Wonder,
otherwise display "Sorry Not Found
Seven wonders - CHICHEN ITZA, CHRIST THE RDEEEMER, TAJMAHAL,
GREAT WALL OF CHINA, MACHU PICCHU, PETRA, COLOSSEUM
Locations - MEXICO, BRAZIL, INDIA, CHINA, PERU,
JORDAN, ITALY Examples: Country Name: INDIA
Output: INDIA-TAJMAHAL
Country Name: USA Output: Sorry Not Found!
15. Write a program to find the frequency of each character in a given string.

16. Write a program to input a string . Check and display message Palindrome if
the string is palindrome.

17. Write a java program to count the frequency of letters, digits, lowercase,
uppercase and whitespaces in a string. [Use Character class methods]

18. Define a class Applicant with following


description: Private Members:
• A data member ANO (Admission Number) of type long
• A data member Name of type string
• A data member Agg (Aggregate Marks) of type float
• A data member Grade of type char

• A member function GradeMe() to find the Grade as per the Aggregate


Marks obtained by a student. Equivalent Aggregate Marks range and
the respective Grades are shown as follows:
Aggregate Marks Grade
>=80 A
Less than 80 and >=65 B
Less than 65 and >=50 C
Less than 50 D

Public members:

• A function ENTER() to allow user to enter values for ANo, Name, Agg &
call function GradeMe() to find the Grade.
• A function RESULT() to allow user to view the content of all the data
members.

Also create a main() method to create an object and show its implementation
by calling the above methods.

19. Write a program to convert all capital letters to small letters and vice-
versa in a given string. For example,
Given He Went To The Market.
String:
Output: hE WENTwENTtOTOtHETHEmARKETMARKET..
20. Write a program to accept name and total marks of N number of
students in two single subscript array name[ ] and totalmarks[ ].
Calculate and print:
(i)The average of the total marks obtained by N
number of students. [average = (sum of total marks
of all the students)/N]
(ii) Deviation of each student’s total marks
with the average. [deviation = total marks of
a student – average]

21.Write a program that takes a matrix of order 3 x 3 as input from the


user and prints the transpose of the matrix.
Example:

A= Transpose of A =

22. Define a class to accept a String and print the number of digits,
alphabets and special characters in the string.
Example: S = "KAPILDEV@83"
Output: Number of digits - 2
Number of Alphabets - 8
Number of Special characters – 1

23.Write a Java program to input a double array of size n. Arrange


these numbers in ascending order using the selection sort
technique.

24. Write a Java program to accept an integer array of size 3 x


3 and perform the following, a). Compute and print the sum
of all elements of the array
b). Print all the odd numbers of the array
c). Print the sum of elements of each row
of the array d). Sum of main diagonal of
the array
e). Display minimum and maximum number from the array.
25.Write a Java program to input ten names in an array. Arrange these names in
descending
order of alphabets, using the bubble sort technique.
*******************************
Program 1:Electricity Board
class ElectricityBoard {//class creation
String month;
String CName; long CNumber;
int previous; int present; int units;
ElectricityBoard (String month, String CName, long CNumber, int
previous, int present) {
month = month;
CName = Name;
CNumber = CNumber;
previous =previous;
present = present;
netBill = 0.0;
}

void process() {
units = present - previous; double charge;
double netBill;

if (units <= 100) {


charge = units * 1.20;
} else if (units <= 220) {
charge = 100 * 1.20 + (units - 100) * 1.75;
} else {
charge = 100 * 1.20 + 120 * 1.75 + (units - 220) * 2.25;
}

netBill = charge + 100 + (0.06 * (charge + 100));


}

void displayDetails() {
System.out.println("Month: " + month);
System.out.println("ConsumerName: " + CName);
System.out.println("CconsumerNumber: " + CNumber);
System.out.println("Net Bill: " + netBill);
}

public static void main(String[] args) {


ElectricityBoard obj = new ElectricityBoard ("March", "joe",
9791692200, 1000,300);
obj.process();
obj.displayDetails();
}
}
Variable Description:-
SN Variable Datatype Purpose

1 month int Number of


months
2 CName String Name of
customer

3 netBill int Total Bill

OUTPUT:
Program 2:Abundant Number
import java.util.Scanner;

class Number { //class creation


void process() {
Scanner sc= new Scanner(System.in);

System.out.print("Enter a number : ");


int num = sc.nextInt();

int sum = 1;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
sum += i;
if (i != num / i) {
sum += num / i;
}
}
}

if (sum > number) {


System.out.println( num + " is an Abundant
number."); } else if (sum < number) {
System.out.println( num + " is a Deficient
number."); } else {
System.out.println( num + " is a Perfect number.");
}

}
public static void main()
{ Number obj = new Number ();
obj.process();
}
}
Variable Description:-
SN Variable Datatype Purpose

1 sum int Sum of


numbers

2 num int Input from user


3 i int Used in
looping

OUTPUT:

Program 3:Menu Driven


import java.util.Scanner;
class program { //class creation
void process() {
Scanner sc = new Scanner(System.in);

int choice;
do {
System.out.println("Menu:");
System.out.println("1. Display the sum of
series"); System.out.println("2. Print the
value of Z"); System.out.println("3. Exit");

System.out.print("Enter your choice (1,


2, or 3): "); choice = scanner.nextInt();

switch (choice) {
case 1:
int y = 55;
for (int x = -10; x <= 10; x += 2)
{ double z = Math.pow(x, 3) + 0.5 * x /
y; System.out.println("For x = " + x +
", Z = " + z);} break;
case 2:
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();

int sum = 0;
int term = 1;

for (int i = 1; i <= n; i++) {


sum += term;
term = term * 10 + (i + 1);
}

System.out.println("Sum of the series: " + sum);


break;
case 3:
System.out.println("Exiting the program.
Goodbye!"); break;
default:
System.out.println("Invalid choice. Please enter 1, 2, or
3.");
}
}
public static void main()
{
Program obj= new Program();
obj.process();
}
}
}
Variable Description:-

SN Variable Datatype Purpose

1 sum int Sum of number


2 term int No of
characters

3 z int output

OUTPUT:

Program 4:Tribonacci series


import java.util.Scanner;
class TribonacciSeries {
void process() {
Scanner sc = new Scanner (System.in);

System.out.print("Enter the number of terms in the


Tribonacci series: "); int n = sc.nextInt();

int firstTerm = 0, secondTerm = 1, thirdTerm = 1;

System.out.println("Tribonacci Series:");

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

System.out.print(firstTerm + " ");

int nextTerm = firstTerm + secondTerm + thirdTerm;


firstTerm = secondTerm;
secondTerm = thirdTerm;
thirdTerm = nextTerm;
}
public static void main()
{
TribonacciSeries obj=new TribonacciSeries();
}
}

Variable Description:-

SN Variable Datatype Purpose

1 firstLetter int Term 1

2 secondLetter int Term 2

3 ThirdLetter int Term 3


Program 5:Overload
class OverloadedSum { //class creation

void sum(int x, int y) {


System.out.println("Sum of integers: " + (x + y));
}

void sum(char p, char q) {


int sum = (int) p + (int) q;
System.out.println("Sum of characters (ASCII): " + sum);
}

void sum(float m, float n) {


System.out.println("Sum of real numbers: " + (m + n));
}

public static void main(String[] args) {


OverloadedSum obj = new OverloadedSum();

obj.sum(5, 10);
obj.sum('A', 'B');
obj.sum(3.5f, 7.2f);
}
}
Variable Description:-
SN Variable Datatype Purpose

1 x int Int For


calculation
2 y int Int For
calculation
3 p char Character for
calculation
Program 6:ShowRoom
import java.util.Scanner;
class ShowRoom { //class creation
String name;
long mobno;
double cost;
double dis;
double amount;
/ Default
constructor
ShowRoom() {
name = "
"; mobno =
0; cost =
0.0; dis =
0.0;
amount =
0.0;
}
void input() {
Scanner sc = new Scanner (System.in);

System.out.print("Enter customer name: ");


name = sc.nextLine();

System.out.print("Enter mobile number: ");


mobno = sc.nextLong();
System.out.print("Enter cost of items
purchased: ");
cost = sc.nextDouble();
}

void calculate() {
if (cost <=
10000) { dis
= cost * 0.05;
} else if (cost <=
20000) { dis =
cost * 0.10;
} else if (cost <=
35000) { dis =
cost * 0.15;
} else {
dis = cost * 0.20;
}

amount = cost - dis;


}

/ Method to display customer details and amount


after discount void display() {

System.out.println("Customer Name: " + name);


System.out.println("Mobile Number: " + mobno);
System.out.println("Amount to be paid after discount: Rs" + amount);
}
public static void main(String[] args) {
ShowRoom customer = new ShowRoom();

customer.input();

customer.calculate();

customer.display();
}
}

Variable Description:-

SN Variable Datatype Purpose

1 amount int Final amout

2 mobno float Number of user

3 name String Name of user


Program 7:Shapes
import java.util.Scanner;
class Shapes {
void process() {
Scanner scanner = new Scanner(System.in);

// For a square
System.out.print("Enter the side of the
square: "); double sideOfSquare =
sc.nextDouble();
Shapes square = new
Shapes(sideOfSquare);
System.out.println("Square:");
square.computeArea();

// For a rectangle
System.out.print("Enter the length of the
rectangle: "); double lengthOfRectangle =
sc.nextDouble(); System.out.print("Enter the
breadth of the rectangle: "); double
breadthOfRectangle = sc.nextDouble();
Shapes rectangle = new Shapes(lengthOfRectangle, breadthOfRectangle);
System.out.println("Rectangle:");
rectangle.computeArea();

}
public static void main()
{
Shapes obj = new Shapes();
obj.process();

}
Variable Description:-

SN Variable Datatype Purpose

1 sideOfSquare int Side

2 lengthOfRectangle int length

3 breadthOfRectangle int breadth

Program 8:Overload Volume


class VolumeCalculator {
/ Method to calculate volume of
a sphere double volume(double
R) {
return (4.0 / 3.0) * 3.14 * Math.pow(R, 3);
}

/ Method to calculate volume of a


cylinder double volume(double H,
double R) {
3.14* Math.pow(R, 2) * H;
}

/ Method to calculate volume of a cuboid


/ double volume(double L, double B,
double H) {
/ return L * B * H;
/ }
/
/ / public static void main(String[] args) {
/ VolumeCalculator calculator = new VolumeCalculator();
/
/ / // Example usage
/ double sphereVolume =
calculator.volume(3.0);
System.out.println("Volume of Sphere: " +
sphereVolume);
/
/ double cylinderVolume = calculator.volume(5.0,
2.0); System.out.println("Volume of Cylinder: " +
cylinderVolume);
/
/ double cuboidVolume = calculator.volume(4.0,
3.0, 6.0); System.out.println("Volume of Cuboid:
/ " + cuboidVolume);
}
/ } }
Variable Description:-

SN Variable Datatype Purpose

1 sphereVolume double Volume - Sphere

2 cuboidVolume double Volume-cuboid

3 cylinderVolume double Volume-Cyliner


Program 9:Sum of Digits
import java.util.scanner;
class Number {
/ Function to calculate the sum of digits in
an integer int sumOfDigits(int n) {
int sum =
0; while (n
> 0) {
sum += n %
10; n /= 10;
}
return sum;
}

/ Function to accept two integers and find the sum of


digits for each void call() {
Scanner sc = new Scanner(System.in);

System.out.print("Enter the first integer: ");


int num1 = sc.nextInt();
int sum1 = sumOfDigits(num1);
System.out.println("Sum of digits for the first integer: " + sum1);

System.out.print("Enter the second integer: ");


int num2 = sc.nextInt();
int sum2 = sumOfDigits(num2);
System.out.println("Sum of digits for the second integer: " + sum2);

}
public static void main()
{
Number obj=new Nuimber();
obj.SumbofDigits(5)
obj.call();
}
}
Variable Description:-

SN Variable Datatype Purpose

1 n int input

2 sum int Sum of digits

3 num1 int Term 1

Program 10:Odd or Even in Array


import java.util.scanner;
public class OddEven {
void process() {
Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of elements


in the array: "); int n = sc.nextInt();

int[] numbers = new int[n];

/ Input numbers into the array


System.out.println("Enter the elements of
the array:"); for (int i = 0; i < n; i++) {
numbers[i] = sc.nextInt();
}
int even = 0, odd = 0;
for (int num : numbers) {
if (num % 2 == 0) {
even++;
} else {
odd++;
}
}

System.out.println("Number of even numbers: " + even);


System.out.println("Number of odd numbers: " + odd);

}
public static void main()
{
ArrayOddEvenCount obj = new
ArrayOddEvenCount(); obj.process();
}
}
Variable Description:-

SN Variable Datatype Purpose

1 n int input
2 even int Count even
numbers
3 odd int Count odd
numbers
Program 11:Linear Search
import java.util.Scanner;
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements
in the array: ");
int n = sc.nextInt(); lan=0;
int[]A= new int[n];
System.out.println("Enter the elements of the
array:");
for (int i = 0; i < n; i++)
{
A [i] = sc.nextInt();
}

System.out.print("Enter the number to


search: "); int k= sc.nextInt();
For(int j=0; a.length; i++)
{
If(A[i]==K)
{
lan=1
int pos=I;
break;
}}
if (lan==1)
System.out.println(“found”);
Else
System.out.println(“not found”);
Variable Description:-
SN Variable Datatype Purpose

1 n int Size of array


2 lan int Position of
element

3 i int For looping


Program 12:Binary Search
import java.util.Scanner;

public class BinarySearch {


void process() {
int[] sortedList = {31, 36, 45, 50, 60, 75, 86, 90};

/ Sorting the array (Binary search requires a


sorted array) Arrays.sort(sortedList);

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number to


search: "); int searchElement =
scanner.nextInt();

// Perform binary search


int position = binarySearch(sortedList, searchElement);

if (position != -1) {
System.out.println("Search Successful! Element " + searchElement + " found
at position
" + (position +
1)); }

System.out.println("Search Unsuccessful! Element " + searchElement + " not


found in the list.");
}
}

// Binary search function


static int binarySearch(int[] arr, int key) {
int low = 0;
int high = arr.length - 1;

while (low <= high) {


int mid = (low + high) / 2;

if (arr[mid] == key) {
return mid;
} else if (arr[mid] <
key) { low = mid +
1;
} else {
high = mid - 1;
}
}

return -1; // Return -1 if the key is not found


}
public static void main()
{
BinarySearch obj = new BinarySearch();
obj.process();
}
}

Variable Description:-

SN Variable Datatype Purpose

1 position int Position of


element

2 low int Lower term

3 mid int Middle term


Program 13:PigLatin
import java.util.Scanner;

class PigLatinEncoder
{
void process() {

Scanner sc = new Scanner(System.in);


System.out.print("Enter word: ");
String word = sc.nextline();
int len = word.length();

word=word.toUpperCase();
String piglatin=" ";
int flag=0;

for(int i = 0; i < len; i++)


{
char x = word.charAt(i);
if(x=='A' || x=='E' || x=='I' || x=='O' || x=='U')
{
piglatin=word.substring(i) + word.substring(0,i) + "YZ";
flag=1;
break;
}
}

if(flag == 0)
{
piglatin = word + "YZ";
}
System.out.println(word + " in Piglatin format is " + piglatin);
}
public static void main()
{
PigLatinEncoder obj = new PigLatinEncoder();
obj.process();
}
}
Variable Description:-

SN Variable Datatype Purpose

1 x int Exception
letters

2 piglatin String output

3 len int Length of array


Program 14:Seven Wonders of the World
import java.util.Scanner;

public class SevenWondersSearch


{//class creation void process()
{//variable initialization
String[] wonders = {"CHICHEN ITZA", "CHRIST THE REDEEMER",
"TAJMAHAL", "GREAT WALL OF CHINA", "MACHU PICCHU", "PETRA",
"COLOSSEUM"};
String[] locations = {"MEXICO", "BRAZIL", "INDIA", "CHINA", "PERU",
"JORDAN", "ITALY"};

Scanner sc = new Scanner(System.in); //scanner

System.out.print("Enter the country name: ");


String countryName
=sc.nextLine().toUpperCase();
int index = searchCountryIndex(countryName, locations);
if (index != -1) {
System.out.println(countryName + "-" +
wonders[index]); } else {
System.out.println("Sorry Not Found!");
}

int searchCountryIndex(String countryName,


String[] locations) { for (int i = 0; i <
locations.length; i++) {
if (locations[i].equals(countryName)) {
return i;

}
}
return -1;
}
public static void main()//main class
{
SevenWonderSearch obj = new SevenWonderSearch();
obj.process();
obj.searchCountryIndex(Brazil,5)
}
}
Variable Description:-

SN Variable Datatype Purpose

1 location String location

2 Country name String name

3 -_ - -
Program 15:Frequency of each Character
class Frequency { //class creation

void process() {
//initialization
String K = "Hello World";
char ch = 'e';
int frequency = 0;
//process
for(int i = 0; i < K.length(); i++) {
if(ch == K.charAt(i)) {
++frequency;
}
}
//output
System.out.println("Frequency of " + ch + " = " + frequency);
}
public static void main()
{//main class
Frequency obj = new Frequency();
obj.process();
}
}

Variable Description:-

SN Variable Datatype Purpose

1 K String input
2 frequency int Frequency to
be added
3 ch char Frequency to
be checked
Program 16:Palindrome of String
import java.util.Scanner;//import scanner
class Check//class creation
{//initialization
String sent;
int len;
Check( )
{
sent="";
len=0;
}
void acceptword( )
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word");
sent=sc.next( );
len=sent.length( );
}
boolean palindrome( )
{
for(int i=0, j=len-1; i&lt;=j; i++, j--)
{
if(wrd.charAt(i)!=sent.charAt(j))
return false;
}
return true;
}
public void display( )
{
System.out.print(wrd);
if(palindrome( ))
System.out.println("is a Palindrome word");
else
System.out.println("is not a Palindrome word");
}
public static void main(String ar[ ])//main class
{
Check obj=new Check( )
obj.acceptword( );
obj.display( );
}
}

Variable Description:-

SN Variable Datatype Purpose

1 sent String Get from user


2 length int Length of
string

3 i int For looping


Program 17:Character class
import java.util.Scanner;//import scanner

class CharacterCount {//class creation


void process() {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string: ");


String inputString = scanner.nextLine();
//input
int letterCount = 0;
int digitCount = 0;
int lowercaseCount = 0;
int uppercaseCount = 0;
int whitespaceCount = 0;
//process
for (char ch : inputString.toCharArray()) {
if (Character.isLetter(ch)) {
letterCount++;
if (Character.isLowerCase(ch)) {
lowercaseCount++;
} else {
uppercaseCount++;
}
} else if
(Character.isDigit(ch)) {
digitCount++;
} else if
(Character.isWhitespace(ch))
{ whitespaceCount++;
}
}

System.out.println("Letter count: " + letterCount);


System.out.println("Digit count: " + digitCount);
System.out.println("Lowercase count: " + lowercaseCount);
System.out.println("Uppercase count: " + uppercaseCount);
System.out.println("Whitespace count: " + whitespaceCount);
}//main method
public static void main()
{
CharacterCount obj = new CharacterCount();
obj.process();

Variable Description:-

SN Variable Datatype Purpose

1 uppercaseCount int Count number


of upper case
2 lowercaseCount int Count number
of Lower case
3 DigitCount int Count umber
of Digits
Program 18:Student Grades
import java.util.Scanner;//importing scanner

class Applicant {//class creation


long ANO; //variables
String Name;
float Agg;
char Grade;

void GradeMe() {//process


if (Agg >= 80) {
Grade = 'A';
} else if (Agg >= 65)
Grade = 'B';
} else if (Agg >=
50) { Grade =
'C';
} else {
Grade = 'D';
}
}

void ENTER() {//output


Scanner scanner = new Scanner(System.in);

System.out.print("Enter Admission Number


(ANO): "); ANO = scanner.nextLong();

scanner.nextLine();
System.out.print("Enter Name: ");
Name = scanner.nextLine();

System.out.print("Enter Aggregate Marks: ");


Agg = scanner.nextFloat();

GradeMe();
}

void RESULT() {//output


System.out.println("Admission Number (ANO): " + ANO);
System.out.println("Name: " + Name);
System.out.println("Aggregate Marks: " + Agg);
System.out.println("Grade: " + Grade);
}
public static void main()
{
Applicant obj = new Applicant();
obj.GradeMe();
pbj.ENTER();
obj.RESULT();
}
}

Variable Description:-

SN Variable Datatype Purpose

1 Grade char Grade of


student
2 Name String Name of
Student

3 Char char Character


Program 19:Converting capital and small
letters
import java.util.Scanner;//import scanner

class CaseConverter {//class creation


void process() {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string: ");


String inputString = scanner.nextLine();

String convertedString = convertCase(inputString);

System.out.println("Converted String: " + convertedString);


}

String convertCase(String str) {//process

StringBuilder result = new StringBuilder();

for (char ch : str.toCharArray()) {


if (Character.isUpperCase(ch)) {
result.append(Character.toLowerCase(ch));
} else if (Character.isLowerCase(ch))
{ result.append(Character.toUpper
Case(ch));
} else {
result.append(ch);
}
}d
return result.toString();
}
public static void main()//main method
{
CaseConverter obj = new CaseConverter();//object creation
obj.process();
obj.convertCase(Hello);
}
}
Variable Description:-

SN Variable Datatype Purpose

1 inputString String From user

2 str String output

3 - -- ---
Program 20:Array-Marks
import java.util.Scanner;

class StudentMarks {
void process() {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of


students (N): "); int N = scanner.nextInt();

/ Declare arrays to store names and


total marks String[] name = new
String[N];
double[] totalMarks = new double[N];

/ Input names and total marks


for (int i = 0; i < N; i++) {
System.out.print("Enter name of student " + (i
+ 1) + ": "); name[i] = scanner.next();

System.out.print("Enter total marks of student " +


(i + 1) + ": "); totalMarks[i] =
scanner.nextDouble();
}

/ Calculate average of
total marks double sum =
0;
for (double marks :
totalMarks) { sum +=
marks;
}
double average = sum / N;

/ Calculate deviation for each student and


display the result System.out.println("\
nResults:");
for (int i = 0; i < N; i++) {
double deviation = totalMarks[i] - average;
System.out.println("Student " + name[i] + " - Total Marks: " +
totalMarks[i] + ",
Deviation: " + deviation);
}

/ Display the average of total marks


System.out.println("\nAverage of Total Marks: " +
average);

}
public static void main()
{
StudentMarks obj = new StudentMarks();
obj.process();
}
}

Variable Description:-
SN Variable Datatype Purpose

1 Name String Name of user

2 TotalMarks int Sum of marks

3 N int Input from user


Program 21:Transposing of Matrix
import java.util.Scanner;

public class MatrixTranspose {


void process() {
Scanner scanner = new Scanner(System.in);

int[][] matrix = new int[3][3];

// Input matrix from the user


System.out.println("Enter the elements of the
3x3 matrix:"); for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("Enter element at position (" + (i + 1) + ","
+ (j + 1) + "): "); matrix[i][j] = scanner.nextInt();
}
}

// Print the original matrix

System.out.println("\nOriginal Matrix:");
printMatrix(matrix);

/ Calculate and print the transpose of


the matrix int[][] transposeMatrix =
transpose(matrix);
System.out.println("\nTranspose of the
Matrix:"); printMatrix(transposeMatrix);

scanner.close();
}

/ Function to calculate the transpose


of a matrix static int[][]
transpose(int[][] matrix) {
int rows = matrix.length;
int columns = matrix[0].length;

int[][] transposeMatrix = new int[columns][rows];


for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
transposeMatrix[j][i] = matrix[i][j];
}
}

return transposeMatrix;
}

// Function to print a matrix


static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
public static void main()
{
MatrixTranspose obj = new MatrixTranspose();
obj.process();
obj.transpose();
obj.printMatrix();
}
Program 22:Selection Sort
import java.util.Scanner;

class SelectionSort {
void process() {
Scanner scanner = new
Scanner(System.in);

System.out.print("Enter the size of the array: ");


int n = scanner.nextInt();

double[] doubleArray = new double[n];

// Input double array


System.out.println("Enter the elements of
the array:"); for (int i = 0; i < n; i++) {
doubleArray[i] = scanner.nextDouble();
}

// Perform selection sort


for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (doubleArray[j] < doubleArray[minIndex]) {
minIndex = j;
}
}
// Swap elements
double temp = doubleArray[minIndex];
doubleArray[minIndex] = doubleArray[i];
doubleArray[i] = temp;
}

// Display the sorted array


System.out.println("\nSorted array in
ascending order:"); for (double value :
doubleArray) {
System.out.print(value + " ");
}
}
public static void main()
{
SelectionSort obj = new SelectionSort();
obj.process();
}

Variable Description:-

SN Variable Datatype Purpose

1 n int Input temporary

2 i int For looping

3 temp int
Program 23:3x3 Array
import java.util.Scanner;

class ArrayOperations {
void process() {
Scanner scanner = new Scanner(System.in);

int[][] array = new int[3][3];

// Input array from the user


System.out.println("Enter the elements of the 3x3
array:"); for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("Enter element at position (" + (i + 1) + ","
+ (j + 1) + "): "); array[i][j] = scanner.nextInt();
}
}

/ a) Compute and print the sum of all


elements int totalSum = 0;
for (int[] row :
array) { for (int
value : row)
{ totalSum +=
value;
}
}
System.out.println("Sum of all elements: " + totalSum);

/ b) Print all the odd numbers


System.out.println("Odd numbers:");
for (int[] row : array) {
for (int value : row) {
if (value % 2 != 0) {
System.out.print(value + " ");
}
}
}
System.out.println();

/ c) Print the sum of elements of each


row System.out.println("Sum of elements
in each row:"); for (int i = 0; i < 3; i++) {
int rowSum = 0;
for (int j = 0; j < 3;
j++) { rowSum
+= array[i][j];
}
System.out.println("Row " + (i + 1) + ": " + rowSum);
}

/ d) Sum of main diagonal

int diagonalSum = 0;

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


diagonalSum += array[i][i];
}
System.out.println("Sum of main diagonal: " + diagonalSum);

/ e) Display minimum and maximum number


from the array int min = array[0][0];
int max = array[0]
[0]; for (int[] row :
array) { for (int
value : row) {
if (value < min) {
min = value;
}
if (value > max) {
max = value;
}
}
}
System.out.println("Minimum number: " + min);
System.out.println("Maximum number: " + max);

}
public static void main()
{
IntegerArrayOperations obj = new
IntegerArrayOperations(); obj.process();
}
}

Variable Description:-

SN Variable Datatype Purpose

1 totalSum int Sum of total

2 rowSum int Sum of row

3 diagnolSum int Sum of diagnols


Program 25:Bubble Sort
import java.util.Scanner;

public class StringBubbleSort {


void process() {
Scanner scanner = new
Scanner(System.in);

/ Input names into an array


String[] names = new
String[10];
System.out.println("Enter ten
names:"); for (int i = 0; i < 10;
i++) {
names[i] = scanner.nextLine();

/ Perform Bubble Sort for


descending order for (int i = 0; i <
names.length - 1; i++) {
for (int j = 0; j < names.length - 1 - i;
j++) { if
(names[j].compareTo(names[j +
1]) < 0) {
/ Swap names[j] and names[j + 1]
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
}

/ Display the sorted names


System.out.println("\nNames in
descending order:"); for (String name :
names) {
System.out.println(name);
}
}
public static void main()
{
StringBubbleSort obj = new StringBubbleSort();
obj.process();
}
}

Variable Description:-

SN Variable Datatype Purpose

1 j int looping

2 i int looping

3 names String Name of user

You might also like