0% found this document useful (0 votes)
12 views17 pages

Java Programs for Array and String Operations

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

Java Programs for Array and String Operations

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

Q10.

Define a class to declare an integer array of size n and accept the elements into
the array. Search for an element input by the user using linear search technique, display
the element if it is found, otherwise display the message "NO SUCH ELEMENT".

Program :

import [Link];

class LinearSearchArray

public static void main(String[] args)

Scanner sc = new Scanner([Link]);

[Link]("Enter number of elements: "); //Accept size of array

int n = [Link]();

int[] arr = new int[n]; //Declare array

[Link]("Enter " + n + " elements:");

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

arr[i] = [Link]();

[Link]("Enter element to search: "); //Accept element to search

int searchElement = [Link]();

//Perform Linear Search

boolean found = false; // flag to check if element is found

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

if (arr[i] == searchElement)

[Link]("Element found: " + arr[i]);

1
found = true;

break; // exit loop once element is found

if (!found) //If not found, display message

[Link]("NO SUCH ELEMENT");

Q19. Write a program to accept a word and convert it into uppercase, and check
whether it is Palindrome or not.
Input Word: MOM Output: Palindrome word
Input Word: BAT Output: Not Palindrome Word

Program :

import [Link];

class PalindromeCheck
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a word: "); //Accept a word
String word = [Link]();
word = [Link](); // Convert to uppercase
String reverse = ""; //Reverse the word
for (int i = [Link]() - 1; i >= 0; i--)
{
reverse = reverse + [Link](i);
}

2
if ([Link](reverse)) //Check palindrome
{
[Link]("Palindrome word");
}
else
{
[Link]("Not Palindrome Word");
}
}
}

Q1) Define a class Employee having the following description:

Instance variables:

int panto store personal account number

String name to store name

double taxIncome to store annual taxable income

double tax to store tax that is calculated

Member functions:

input ()Store the pan number, name, taxable income

calc()-Calculate tax for an employee

display()-Output details of an employee

Write a program to compute the tax according to the given conditions and display the
output as per given format.

Total Annual Taxable Income

Upto Rs, 1,00,000 No tax

From 1,00,001 to 1,50,000 10% of the income exceeding Rs. 1,00,000

From 1,50,001 to 2,50,000 Rs. 5000 + 20% of the income exceeding Rs1,50,000

Above Rs. 2,50,000 Rs. 25,000+30% of the income exceeding Rs. 2,50,000

3
Program :

import [Link];

class Employee

int pan;

String name;

double taxIncome;

double tax;

void input() // Function to take input

Scanner sc = new Scanner([Link]);

[Link]("Enter PAN number: ");

pan = [Link]();

[Link]("Enter Name: ");

name = [Link]();

[Link]("Enter Taxable Income: ");

taxIncome = [Link]();

void calc() // Function to calculate tax

if (taxIncome <= 100000)

tax = 0;

4
}

else if (taxIncome <= 150000)

tax = 0.1 * (taxIncome - 100000);

else if (taxIncome <= 250000)

tax = 5000 + 0.2 * (taxIncome - 150000);

Else

tax = 25000 + 0.3 * (taxIncome - 250000);

void display() // Function to display employee details

[Link]("\nPan Number\tName\tTax-Income\tTax");

[Link](pan + "\t\t" + name + "\t" + taxIncome + "\t" + tax);

public static void main(String[] args) // Main function

Employee emp = new Employee();

[Link]();

[Link]();

[Link]();

5
18. Define a class to accept values into a 3×3 array and check if it is a special array.
An array is a special array if the sum of the even elements sum of the odd
elements. Example: int a[]={{4,5, 6}, { 5,3, 2), (4, 2, 5}}; Sum of even elements =
4+6+2+4+2=18 Sum of odd elements=5+5+3+5=18 Then display It is a Special
Array else display Appropriate message.

Program :

import [Link];

class SpecialArray

int a[][] = new int[3][3];

int sumEven = 0, sumOdd = 0;

void input()

Scanner sc = new Scanner([Link]);

[Link]("Enter 9 elements for 3x3 array:");

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

for (int j = 0; j < 3; j++)

a[i][j] = [Link]();

void calculate() // Function to calculate sum of even and odd numbers

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

6
for (int j = 0; j < 3; j++) {

if (a[i][j] % 2 == 0)

sumEven += a[i][j];

else

sumOdd += a[i][j];

void display() // Function to display result

[Link]("Sum of even elements = " + sumEven);

[Link]("Sum of odd elements = " + sumOdd);

if (sumEven == sumOdd)

[Link]("It is a Special Array");

else

[Link]("Not a Special Array");

public static void main(String[] args) // Main function

SpecialArray obj = new SpecialArray();

[Link]();

[Link]();

[Link]();

7
20. Write a program to accept a word and convert it into lowercase if it is in
uppercase, and display the new word by replacing only the vowels with the
character following it. Input Word: CAT Output: cbt
PROGRAMME :

import [Link];

class ReplaceVowels
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

[Link]("Enter a word: ");


String word = [Link]();

word = [Link](); //Convert to lowercase

String newWord = ""; //Replace vowels with next character

for (int i = 0; i < [Link](); i++)


{
char ch = [Link](i);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')


{
ch = (char)(ch + 1); // replace with next character
}

newWord += ch;
}

[Link]("Output: " + newWord); //Display new word


}
}

8
Program :

import [Link];

class ShowRoom

String name;

long mobno;

double cost, dis, amount;

ShowRoom() // Default constructor

name = "";

mobno = 0;

cost = 0;

dis = 0;

amount = 0;

void input() // Function to input data

9
Scanner sc = new Scanner([Link]);

[Link]("Enter Customer Name: ");

name = [Link]();

[Link]("Enter Mobile Number: ");

mobno = [Link]();

[Link]("Enter Cost of Items: ");

cost = [Link]();

void calculate() // Function to calculate discount and amount

if (cost <= 10000)

dis = 0.05 * cost;

else if (cost <= 20000)

dis = 0.10 * cost;

else if (cost <= 35000)

dis = 0.15 * cost;

else

dis = 0.20 * cost;

amount = cost - dis;

void display() // Function to display details

[Link]("\n--- Customer Bill ---");

[Link]("Customer Name : " + name);

[Link]("Mobile Number : " + mobno);

[Link]("Total Cost : " + cost);

[Link]("Discount : " + dis);

10
[Link]("Amount to Pay : " + amount);

public static void main(String[] args) // Main method

ShowRoom obj = new ShowRoom();

[Link]();

[Link]();

[Link]();

14. Write a program to accept three number from the user and sort them in
descending order using Bubble sort.

Programme :

import [Link];

class DescendingBubbleSort

public static void main(String[] args)

Scanner sc = new Scanner([Link]);

int arr[] = new int[3];

[Link]("Enter 3 numbers: "); // Input

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

arr[i] = [Link]();

11
// Bubble Sort (Descending)

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

for (int j = 0; j < 2 - i; j++)

if (arr[j] < arr[j + 1]) { // swap for descending order

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

//Output

[Link]("Numbers in Descending Order:");

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

[Link](arr[i] + " ");

15. Write a program to accept 5 names from the user and print them in descending
order using Selection sort

Program :

import [Link];

12
class SortNames

public static void main(String[] args)

Scanner sc = new Scanner([Link]);

String names[] = new String[5];

[Link]("Enter 5 names:"); // Input 5 names

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

names[i] = [Link]();

for (int i = 0; i < 5; i++) // Selection sort (descending)

for (int j = i + 1; j < 5; j++)

if (names[i].compareTo(names[j]) < 0) // check for descending

{ String temp = names[i];

names[i] = names[j];

names[j] = temp;

[Link]("\nNames in Descending Order:"); // Output

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

[Link](names[i]);

13
}

Q16 Solution :

import [Link];

class DiagonalSums3x3

public static void main(String[] args)

Scanner sc = new Scanner([Link]);

int a[][] = new int[3][3];

[Link]("Enter 9 elements (3x3):");

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

for (int j = 0; j < 3; j++)

a[i][j] = [Link]();

14
}

int sumLeft = 0; // principal diagonal

int sumRight = 0; // other diagonal

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

sumLeft += a[i][i]; // (0,0), (1,1), (2,2)

sumRight += a[i][3 - 1 - i]; // (0,2), (1,1), (2,0)

[Link]("Sum of Left Diagonal = " + sumLeft);

[Link]("Sum of Right Diagonal = " + sumRight);

Q17 Solution :

import [Link];

class RowColumnSums3x4

public static void main(String[] args)

Scanner sc = new Scanner([Link]);

int rows = 3, cols = 4;

int a[][] = new int[rows][cols];

[Link]("Enter " + (rows * cols) + " elements (3 rows and 4 columns):");

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

for (int j = 0; j < cols; j++)

15
{

a[i][j] = [Link]();

[Link]("Sum of each row:"); // Sum of each row

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

int sumRow = 0;

for (int j = 0; j < cols; j++)

sumRow += a[i][j];

[Link]("Row " + (i + 1) + " sum = " + sumRow);

[Link]("Sum of each column:"); // Sum of each column

for (int j = 0; j < cols; j++)

int sumCol = 0;

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

sumCol += a[i][j];

[Link]("Column " + (j + 1) + " sum = " + sumCol);

16
17

You might also like