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

Computer Class 10 Project(full)

The document outlines a project for Class X Computer Applications at Holy Cross School for the academic year 2024-2025, which includes writing twenty programs in BlueJ. Each program addresses different tasks such as printing initials, checking for happy words, determining anagrams, sorting words by length, and generating various patterns and series. The document provides sample inputs and outputs, along with Java code implementations for each program.

Uploaded by

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

Computer Class 10 Project(full)

The document outlines a project for Class X Computer Applications at Holy Cross School for the academic year 2024-2025, which includes writing twenty programs in BlueJ. Each program addresses different tasks such as printing initials, checking for happy words, determining anagrams, sorting words by length, and generating various patterns and series. The document provides sample inputs and outputs, along with Java code implementations for each program.

Uploaded by

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

CLASS X COMPUTER APPLICATION [2024 – 2025]

HOLY CROSS SCHOOL Page 1


PROJECT WORK FOR CLASS X – [2024 – 2025]
 Project Topic – Twenty programs in BlueJ
 Write the code of the following programs along with the variable descriptions.
___________________________________________________________________________
1. Write a program to accept a name of a person and print the initial.
Sample Input: Netaji Subhas Chandra Bose
Sample Output: N.S.C. Bose

import java.util.Scanner;
public class Initials{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a name: ");
String name=scanner.nextLine();
String initials=name.substring(0,1);
int l=name.length();
String lastname=name.substring(name.lastIndexOf(' '));
name=name.substring(0,name.lastIndexOf(' ')+1);
for(int i=0;i<name.length()-1;i++){
if(name.charAt(i)==' '){
initials+="."+Character.toUpperCase(name.charAt(i+1));
}
}
initials+=lastname;
System.out.println(initials);
}
}

2. Write a program in java to accept a word and check and print whether the word is a Happy
word
or not.
A Happy Word is defined as:
Take a word and calculate the word’s value based on position of the letters in the English
alphabet.
On the basis of word’s value, find the sum of the squares of its digits. Repeat the process with
the
resultant number until the number becomes single digit. If the number ends with 1, then the
word is
called a ‘Happy Word’.
Sample Input: VAT
Place value of V=22, A=1, T=20
Solution: 22120 => 22 + 22 + 12 + 22 + 02 = 13 => 12 + 32 = 10 => 12 + 02 = 1
Sample output: A Happy Word

import java.util.*;
public class HappyWord{
public static void main(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word.");
String s=sc.next().toUpperCase();
int sum1=0;
int l=s.length();
String conc="";
int sum2=0;
String
arr[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","
U","V","W","X","Y","Z"};
for(int i=0;i<l;i++){
char c=s.charAt(i);
for(int j=0;j<26;j++){
if(arr[j].equals(String.valueOf(c))){
conc+=String.valueOf(j+1);
}
}
}
sum1=Integer.valueOf(conc);
while (sum1 > 9) {
sum2=0;
while (sum1!=0) {
sum2+=(sum1 % 10)*(sum1 % 10);
sum1/=10;
}
sum1=sum2;
}
if(sum2==1)
System.out.println("Happy Word");
else
System.out.println("Not Happy Word");
}
}

3. Write a program to accept any two words and check whether they are Anagram
words or not.
Anagrams : If a word that is made with the combination of letters present in the original
word then
they are called Anagrams.
Example : The first word is FLOW and the second word is WOLF which is formed with
the
combinations of the letters present in the original word. Thus FLOW and WOLF are
Anagrams.

import java.util.Scanner;
public class Anagram{
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
System.out.println("Enter the 2 words to check");
System.out.print("Enter first word: ");
String str1=in.next().toUpperCase();
System.out.print("Enter second word: ");
String str2=in.next().toUpperCase();
boolean flag=false;
if(!(str1.length()==str2.length())){
System.out.println("Not Anagram");
System.exit(0);
}
for(int i=0;i<str1.length();i++){
char c1=str1.charAt(i);
for(int j=0;j<str2.length();j++){
char c2=str2.charAt(j);
if(c1==c2)
flag=true;
}
}
if(flag)
System.out.println("Anagram");
else
System.out.println("Not Anagram");
}
}

4. Write a program to input a sentence and arrange the words in descending order
according to their length and print the result.
Sample Input: THIS IS A COMPUTER PROJECT
Sample Output: COMPUTER PROJECT THIS IS A

import java.util.Scanner;
public class SortWordsByLength {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a sentence:");
String sentence = scanner.nextLine();
String[] words = sentence.split(" ");
for (int i = 0; i < words.length - 1; i++) {
for (int j = i + 1; j < words.length; j++) {
if (words[i].length() < words[j].length()) {
String temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
}
String result = String.join(" ", words);
System.out.println("Sorted sentence by word length (descending):");
System.out.println(result);
scanner.close();
}
}

5. Write a menu driven program to print the following patterns:

1.54321 2. *
5432 * &
543 * &*
54 * &*&
5 * &*&*

import java.util.Scanner;
public class Pattern9 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\nChoose a pattern to print:");
System.out.println("1. Pattern 1");
System.out.println("2. Pattern 2");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();

switch (choice) {
case 1:
printPattern1();
break;
case 2:
printPattern2();
break;
case 3:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 3);
scanner.close();
}
private static void printPattern1() {
System.out.println("\nPattern 1:");
for (int i = 5; i >= 1; i--) {
for (int j = 5; j >= 6 - i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
private static void printPattern2() {
System.out.println("\nPattern 2:");
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 1) {
System.out.print("* ");
} else {
System.out.print("& ");
}
}
System.out.println();
}
}
}

6. Write a menu driven program to print the following pattern:


a. B b. A
BL ab
BLU ABC
BLUE abcd
BLUEJ ABCDE

import java.util.Scanner;
public class Pattern10
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\nChoose a pattern to print:");
System.out.println("1. Pattern A");
System.out.println("2. Pattern B");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
printPatternA();
break;
case 2:
printPatternB();
break;
case 3:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 3);

scanner.close();
}
private static void printPatternA() {
System.out.println("\nPattern A:");
String[] letters = {"B", "L", "U", "E", "J"};
for (int i = 0; i < letters.length; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(letters[j] + " ");
}
System.out.println();
}
}
private static void printPatternB() {
System.out.println("\nPattern B:");
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (i % 2 == 1) {
System.out.print((char) ('A' + j - 1) + " ");
} else {
System.out.print((char) ('a' + j - 1) + " ");
}
}
System.out.println();
}
}
}

7. Write a menu driven program to find out the sum of the following series:
i) S = 1 + 3/ 2! + 5/3! + 7/4! + ...................... upto n
ii) S = 1 + (1*2) + (1*2*3) + .......................+ (1*2*3*............*n)

import java.util.Scanner;
public class SeriesSumMenu {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\nChoose a series to find the sum:");
System.out.println("1. S = 1 + 3/2! + 5/3! + 7/4! + ... up to n terms");
System.out.println("2. S = 1 + (1*2) + (1*2*3) + ... + (1*2*3*...*n)");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the number of terms (n): ");
int n1 = scanner.nextInt();
double sum1 = calculateSeries1(n1);
System.out.println("Sum of the series: " + sum1);
break;
case 2:
System.out.print("Enter the number of terms (n): ");
int n2 = scanner.nextInt();
long sum2 = calculateSeries2(n2);
System.out.println("Sum of the series: " + sum2);
break;
case 3:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 3);
scanner.close();
}
private static double calculateSeries1(int n) {
double sum = 1.0;
int numerator = 3;
int factorial = 1;

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


factorial *= i;
sum += (double) numerator / factorial;
numerator += 2;
}
return sum;
}
private static long calculateSeries2(int n) {
long sum = 1;
long product = 1;
for (int i = 2; i <= n; i++) {
product *= i;
sum += product;
}
return sum;
}
}

8. Write a menu driven program to find out the sum of the following series:
i) Sum = x/2 - x/5 + x/8 - x/11 + ........................ + x/20
ii) Sum = 1 + 1 + 2 + 3 + 5 + 8 + 13+ 21 + ............. upto n terms

import java.util.Scanner;
public class SeriesSumMenu2{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\nChoose a series to find the sum:");
System.out.println("1. Sum = x/2 - x/5 + x/8 - x/11 + ... + x/20");
System.out.println("2. Sum = 1 + 1 + 2 + 3 + 5 + 8 + 13 + ... up to n terms
(Fibonacci sequence)");
System.out.println("3. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the value of x: ");
double x = scanner.nextDouble();
double sum1 = calculateSeries1(x);
System.out.println("Sum of the series: " + sum1);
break;
case 2:
System.out.print("Enter the number of terms (n): ");
int n = scanner.nextInt();
int sum2 = calculateSeries2(n);
System.out.println("Sum of the series: " + sum2);
break;
case 3:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 3);
scanner.close();
}
private static double calculateSeries1(double x) {
double sum = 0.0;
int denominator = 2;
boolean isPositive = true;
while (denominator <= 20) {
if (isPositive) {
sum += x / denominator;
} else {
sum -= x / denominator;
}
denominator += 3;
isPositive = !isPositive;
}
return sum;
}
private static int calculateSeries2(int n) {
if (n <= 0) return 0;
int a = 1, b = 1;
int sum = a + b;
for (int i = 3; i <= n; i++) {
int next = a + b;
sum += next;
a = b;
b = next;
} return sum;
}
}

9. Write a menu driven program to perform the following tasks:


case 1: Accept a number and check whether it is Circular Prime number or not.
Circular Prime : A circular prime is a prime number with the property that the number generated
at
each intermediate step when cyclically permuting its digits will be prime.
Example : 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime.
case 2: Write program to accept a number and check whether it is a Kaprekar number or not.
Kaprekar Number : In number theory, a Kaprekar number is non-negative integer, the
representation of whose square can be split into two parts that add up to the original number
again.
Example : 45 is a Kaprekar number. (45)2 = 2025 and 20 + 25 = 45

import java.util.Scanner;
public class NumberPropertiesMenu {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\nChoose an option:");
System.out.println("1. Check if a number is a Circular Prime");
System.out.println("2. Check if a number is a Kaprekar Number");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.print("Enter a number: ");
int num1 = scanner.nextInt();
if (isCircularPrime(num1)) {
System.out.println(num1 + " is a Circular Prime.");
} else {
System.out.println(num1 + " is not a Circular Prime.");
}
break;
case 2:
System.out.print("Enter a number: ");
int num2 = scanner.nextInt();
if (isKaprekar(num2)) {
System.out.println(num2 + " is a Kaprekar Number.");
} else {
System.out.println(num2 + " is not a Kaprekar Number.");
}
break;
case 3:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 3);

scanner.close();
}
private static boolean isCircularPrime(int num) {
int digits = (int) Math.log10(num) + 1;
int rotation = num;
for (int i = 0; i < digits; i++) {
if (!isPrime(rotation)) {
return false;
}
rotation = rotateNumber(rotation, digits);
}
return true;
}
private static int rotateNumber(int num, int digits) {
int lastDigit = num % 10;
num = num / 10;
return (lastDigit * (int) Math.pow(10, digits - 1)) + num;
}
private static boolean isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
private static boolean isKaprekar(int num) {
if (num == 1) return true;
int square = num * num;
String squareStr = Integer.toString(square);
int len = squareStr.length();
for (int i = 1; i < len; i++) {
int leftPart = Integer.parseInt(squareStr.substring(0, i));
int rightPart = Integer.parseInt(squareStr.substring(i));
if (rightPart != 0 && leftPart + rightPart == num) {
return true;
}
}
return false;
}
}

10. Write a menu driven program to perform the following task:


case 1: Accept a number and check and print whether it is a Dudeney number or not.
A Dudeney number is a positive integer that is a perfect cube such that the sum of its digits is
equal
to the cube root of the number.
Sample Input: 512 [Sum of the digits=5+1+2=8 Cube root of 512= 8]
Sample Output: It is a Dudeney number.
case 2: Accept a number and check and print whether it is an Abundant number or not.
An Abundant number is a number for which the sum of its proper factors(excluding the number
itself) is greater than the original number.
Sample Input: 12 [Factors of 12 are 1, 2, 3, 4, 6(excluding 12); Sum of the
factors=1+2+3+4+6=16;
As 16>12, so 12 is an Abundant number.]
Sample Output: It is an Abundant number.

import java.util.Scanner;
public class NumberPropertiesMenu2{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\nChoose an option:");
System.out.println("1. Check if a number is a Dudeney Number");
System.out.println("2. Check if a number is an Abundant Number");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter a number: ");
int num1 = scanner.nextInt();
if (isDudeneyNumber(num1)) {
System.out.println(num1 + " is a Dudeney Number.");
} else {
System.out.println(num1 + " is not a Dudeney Number.");
}
break;
case 2:
System.out.print("Enter a number: ");
int num2 = scanner.nextInt();
if (isAbundantNumber(num2)) {
System.out.println(num2 + " is an Abundant Number.");
} else {
System.out.println(num2 + " is not an Abundant Number.");
}
break;
case 3:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 3);
scanner.close();
}
private static boolean isDudeneyNumber(int num) {
int cubeRoot = (int) Math.round(Math.cbrt(num));
if (cubeRoot * cubeRoot * cubeRoot != num) {
return false;
}
int sumOfDigits = 0;
int temp = num;
while (temp > 0) {
sumOfDigits += temp % 10;
temp /= 10;
}
return sumOfDigits == cubeRoot;
}
private static boolean isAbundantNumber(int num) {
int sumOfFactors = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sumOfFactors += i;
}
}
return sumOfFactors > num;
}
}

11. Define a class with the following specification:


Class name: E_commerce
Instance variable/Data Members :
String name: Name of the item purchased
double price: Price of the item purchased
Member methods:
void accept(): Accept the name and the price of the item using the methods of Scanner class.
void calculate(): To calculate the net amount to be paid by a customer, based on the following
criteria:

Price (Rs.) Discount


1000 - 25000 5.0%
25001 - 57000 7.5%
57001 - 100000 10.0%
More than 100000 15.0%

void display(): To display the name of the item and the net amount to be paid.
Write the main method to create an object of the class and call the above methods to perform
the
desired task..

import java.util.Scanner;
public class E_commerce {
private String name;
private double price;
public void accept() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name of the item: ");
name = scanner.nextLine();
System.out.print("Enter the price of the item: ");
price = scanner.nextDouble();
}
public double calculate() {
double discount = 0.0;
if (price >= 1000 && price <= 25000) {
discount = 5.0;
} else if (price >= 25001 && price <= 57000) {
discount = 7.5;
} else if (price >= 57001 && price <= 100000) {
discount = 10.0;
} else if (price > 100000) {
discount = 15.0;
}
double discountAmount = price * (discount / 100);
double netAmount = price - discountAmount;
return netAmount;
}
public void display() {
double netAmount = calculate();
System.out.println("Item Name: " + name);
System.out.println("Net Amount to be paid: Rs. " + netAmount);
}
public static void main(String[] args) {
E_commerce item = new E_commerce();
item.accept();
item.display();
}
}

12. Define a class Bill that calculates the telephone bill of a consumer with the following
description:
Instance variables/data members:
int bno - to store the bill number
String name - to store the name of the consumer
int call - to store the number of calls consumed in a month
double amt - to store the amount to be paid by the consumer
Member methods:
i) void input() - Accept and store the bill number, name of the consumer and number of
calls consumed
ii) void calculate() - Calculate the monthly telephone bill as per the following conditions.
Unit Consumed Rate
First 100 calls Rs. 0.60/ call
For next 100 calls Rs. 0.80/ call
For next 100 calls Rs. 1.20/ call
Above 300 calls Rs. 1.50/ call
iii) void display() - To display the details
Write a main method to create an object of the class and call the above member methods to
perform the desired task..

import java.util.Scanner;
public class Bill {
private int bno;
private String name;
private int calls;
private double amt;
public void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter bill number: ");
bno = scanner.nextInt();
scanner.nextLine(); // consume newline
System.out.print("Enter consumer name: ");
name = scanner.nextLine();
System.out.print("Enter number of calls made: ");
calls = scanner.nextInt();
}
public void calculate() {
if (calls <= 100) {
amt = calls * 0.60;
} else if (calls <= 200) {
amt = (100 * 0.60) + ((calls - 100) * 0.80);
} else if (calls <= 300) {
amt = (100 * 0.60) + (100 * 0.80) + ((calls - 200) * 1.20);
} else {
amt = (100 * 0.60) + (100 * 0.80) + (100 * 1.20) + ((calls - 300) * 1.50);
}
}
public void display() {
System.out.println("\nBill Details:");
System.out.println("Bill Number: " + bno);
System.out.println("Consumer Name: " + name);
System.out.println("Number of Calls: " + calls);
System.out.println("Amount to be paid: Rs. " + amt);
}
public static void main(String[] args) {
Bill bill = new Bill();
bill.input();
bill.calculate();
bill.display();
}
}

13. 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 a sphere using
the
formula. V=4/3*22/7*r*r*r
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*r*r*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

public class VolumeCalculator {


public double volume(double r) {
return (4.0 / 3.0) * (22.0 / 7.0) * r * r * r;
}
public double volume(double h, double r) {
return (22.0 / 7.0) * r * r * h;
}
public double volume(double l, double b, double h) {
return l * b * h;
}
public static void main(String[] args) {
VolumeCalculator vc = new VolumeCalculator();
double radius = 7.0;
System.out.println("Volume of the sphere: " + vc.volume(radius));
double height = 10.0;
System.out.println("Volume of the cylinder: " + vc.volume(height, radius));
double length = 5.0;
double breadth = 3.0;
System.out.println("Volume of the cuboid: " + vc.volume(length, breadth,
height));
}
}

14. Design a class to overload the function display() as follows:


i) void display(String str, char ch) : To print the new string by replacing all the vowels present in
str
with the provided character ch
ii) void display(int n): To print the first and last digit of the number n, where n is a three digit
number
iii) void display(char c): To print the ASCII value of the provided character c

public class DisplayOverload {


public void display(String str, char ch) {
String newStr = str.replaceAll("[AEIOUaeiou]", String.valueOf(ch));
System.out.println("Modified String: " + newStr);
}
public void display(int n) {
if (n >= 100 && n <= 999) {
int firstDigit = n / 100;
int lastDigit = n % 10;
System.out.println("First digit: " + firstDigit + ", Last digit: " + lastDigit);
} else {
System.out.println("Error: Please enter a three-digit number.");
}
}
public void display(char c) {
int asciiValue = (int) c;
System.out.println("ASCII value of '" + c + "': " + asciiValue);
}
public static void main(String[] args) {
DisplayOverload obj = new DisplayOverload();
obj.display("Hello World", '*');
obj.display(123);
obj.display('A');
}
}

15. Write a program to accept 10 integers and store them in a single dimensional array. Arrange
them in ascending order by using selection sort technique. Print the sorted array.

import java.util.Scanner;
public class SelectionSortArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[10];
System.out.println("Enter 10 integers:");
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
System.out.println("Sorted array in ascending order:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}

16. Write a program to accept any 10 integers and store them in dimensional array. Now print
all the
prime numbers present in the array.
[The number which is not divisible by any number other than 1 and the number itself, is called a
Prime number]
Sample Input : {12, 33, -45, 123, 90, 11, 31, 121, 81, 73}
Sample Output: 11, 31, 73

import java.util.Scanner;
public class PrimeNumbersInArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[10];
System.out.println("Enter 10 integers:");
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
System.out.println("Prime numbers in the array:");
for (int num : arr) {
if (isPrime(num)) {
System.out.print(num + " ");
}
}
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}

17. Write a program to accept the name and contact numbers of 20 students. Store the names
and
contacts in two different Single dimensional arrays. Now accept a name from the user and
check
whether it is present in the array or not. If it is present, print the name along with the respective
contact number. Otherwise print the message “Search unsuccessful”. [Use Binary search
technique].

import java.util.Arrays;
import java.util.Scanner;
public class StudentContactSearch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] names = new String[20];
String[] contacts = new String[20];
System.out.println("Enter the names and contact numbers of 20 students:");
for (int i = 0; i < names.length; i++) {
System.out.print("Enter name of student " + (i + 1) + ": ");
names[i] = scanner.nextLine();
System.out.print("Enter contact number of student " + (i + 1) + ": ");
contacts[i] = scanner.nextLine();
}
String[] sortedNames = names.clone();
Arrays.sort(sortedNames);
System.out.print("Enter the name to search: ");
String searchName = scanner.nextLine();
int index = binarySearch(sortedNames, searchName);
if (index != -1) {
System.out.println("Name: " + searchName + ", Contact Number: " +
contacts[index]);
} else {
System.out.println("Search unsuccessful");
}
}
public static int binarySearch(String[] array, String key) {
int low = 0;
int high = array.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
int comparison = key.compareTo(array[mid]);
if (comparison == 0) {
return mid;
} else if (comparison < 0) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
}

18. Write a program to accept the name of any 10 cities and their STD Codes. Store the name of
the
cities and STD in two different single dimensional arrays. Arrange the name of the cities in
alphabetical order along with their STD codes by using Bubble sort technique. Now print the
sorted
list of the cities along with their corresponding STD Codes.

import java.util.Scanner;
public class CitySTDSorter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] cities = new String[10];
int[] stdCodes = new int[10];
System.out.println("Enter the names and STD codes of 10 cities:");
for (int i = 0; i < 10; i++) {
System.out.print("Enter name of city " + (i + 1) + ": ");
cities[i] = scanner.nextLine();
System.out.print("Enter STD code of city " + (i + 1) + ": ");
stdCodes[i] = scanner.nextInt();
scanner.nextLine();
}
for (int i = 0; i < cities.length - 1; i++) {
for (int j = 0; j < cities.length - i - 1; j++) {
if (cities[j].compareTo(cities[j + 1]) > 0) {
String tempCity = cities[j];
cities[j] = cities[j + 1];
cities[j + 1] = tempCity;
int tempCode = stdCodes[j];
stdCodes[j] = stdCodes[j + 1];
stdCodes[j + 1] = tempCode;
}
}
}
System.out.println("\nSorted list of cities and their STD codes:");
for (int i = 0; i < cities.length; i++) {
System.out.println("City: " + cities[i] + ", STD Code: " + stdCodes[i]);
}
}
}

19.Write a program to create a double dimensional array N[3][3]. Allow the user to input and
store
the integer elements into the matrix.
Now print the original matrix and its Transposed matrix. Transpose of a matrix is obtained by
changing rows to columns and columns to rows.
Original matrix: 5 8 2 Transposed matrix: 5 4 2
463861
219239

import java.util.Scanner;
public class MatrixTranspose {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[][] matrix = new int[3][3];
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("Element [" + i + "][" + j + "]: ");
matrix[i][j] = scanner.nextInt();
}
}
System.out.println("\nOriginal Matrix:");
printMatrix(matrix);
System.out.println("\nTransposed Matrix:");
printTranspose(matrix);
}
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static void printTranspose(int[][] matrix) {
for (int i = 0; i < matrix[0].length; i++) {
for (int j = 0; j < matrix.length; j++) {
System.out.print(matrix[j][i] + " ");
}
System.out.println();
}
}
}

20. Write a program to create a double dimensional array M[4][4]. Allow the user to input and
store
the integer elements in to the matrix.
Now calculate and print the sum of the elements of both the diagonals(left and right) of the
matrix
and the difference between these two sums separately.
Sample Input: 5 1 3 9
8234
3067
4591
Sample Output: The sum of the left diagonal elements: 14
The sum of the right diagonal elements: 16
The difference is: 2

import java.util.Scanner;
public class DiagonalSumDifference {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[][] matrix = new int[4][4];
System.out.println("Enter the elements of the 4x4 matrix:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print("Element [" + i + "][" + j + "]: ");
matrix[i][j] = scanner.nextInt();
}
}
int leftDiagonalSum = 0;
int rightDiagonalSum = 0;

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


leftDiagonalSum += matrix[i][i];
rightDiagonalSum += matrix[i][3 - i];
}
System.out.println("\nThe sum of the left diagonal elements: " +
leftDiagonalSum);
System.out.println("The sum of the right diagonal elements: " +
rightDiagonalSum);
System.out.println("The difference is: " + Math.abs(leftDiagonalSum -
rightDiagonalSum));
}
}

You might also like