0% found this document useful (0 votes)
22 views33 pages

Project Computer

The document contains multiple Java programs demonstrating various functionalities, including encoding words in Pig Latin, sorting arrays using bubble and selection sort, calculating series sums through method overloading, and modifying strings by replacing vowels. Each program is accompanied by variable descriptions to clarify their purpose. Overall, the document serves as a tutorial for basic Java programming concepts and techniques.

Uploaded by

krishugupta68
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)
22 views33 pages

Project Computer

The document contains multiple Java programs demonstrating various functionalities, including encoding words in Pig Latin, sorting arrays using bubble and selection sort, calculating series sums through method overloading, and modifying strings by replacing vowels. Each program is accompanied by variable descriptions to clarify their purpose. Overall, the document serves as a tutorial for basic Java programming concepts and techniques.

Uploaded by

krishugupta68
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/ 33

1. Write a program that encodes a word in Piglatin.

To translate word into Piglatin word,


convert the word into uppercase and then place the first vowel of the original alphabets.

import java.util.*;

// Define a class named piglatin

class piglatin

public static void main()

Scanner sc = new Scanner(System.in);

int i;

// Prompt the user to enter a word

System.out.println("Enter a word:");

String c = sc.next(); // Read the user input

String ns = c.toUpperCase(); // Convert the input to uppercase

System.out.println("The word in capital letter will be=" + ns);

String pgltn = ""; // Initialize a variable to store the piglatin form

// Iterate through each character of the uppercase word

for (i = 0; i < ns.length(); i++)

char x = ns.charAt(i); // Get the current character

// Check if the character is a vowel (A, E, I, O, U)

if (x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U')

// Construct the piglatin form by rearranging the characters and adding "AY"

pgltn = ns.substring(i) + ns.substring(0, i) + "AY";

break; // Exit the loop since piglatin form is constructed

} else

// If the character is not a vowel, append "AY" to the end of the word

pgltn = ns + "AY";

}
}

// Display the original word and its piglatin form

System.out.println(ns + " in the piglatin form will be: " + pgltn);

Variab Description
le

sc An instance of the Scanner class used to read input from the user via the command
line.

c A String variable that stores the original word input by the user.

ns A String variable that stores the uppercase version of the original word.

pgltn A String variable that stores the piglatin form of the word.

x A char variable that holds the current character being processed during the
iteration through the characters of the uppercase word.

i An int variable used as a loop control variable in the for loop to iterate through the
characters of the uppercase word.
2. Write a program to input 10 integer elements in an array and sort them in Descending order
using bubble sort technique.

import java.util.*;

// Define a class named project_2

class project_2

public static void main()

Scanner sc = new Scanner(System.in);

int n = 10; // Set the number of elements in the array

int arr[] = new int[n]; // Declare an array to store the elements

System.out.println("Enter the elements of the array:");

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

arr[i] = sc.nextInt(); // Read and store elements in the array

// Bubble Sort algorithm to sort the array in descending order

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

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

if (arr[j] < arr[j + 1]) { // Compare adjacent elements

// Swap the elements if they are in the wrong order

int t = arr[j];

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

arr[j + 1] = t;

// Display the sorted array in descending order

System.out.println("Sorted Array:");
for (int i = 0; i < n; i++)

System.out.print(arr[i] + " "); // Print each element separated by space

Variab Description
le

sc An instance of the Scanner class used to read input from the user via the
command line.

n An int variable that represents the number of elements in the array.

arr An int array that stores the elements to be sorted.

i An int variable used as a loop control variable in the outer loop to


iterate through the array elements during input and sorting.

j An int variable used as a loop control variable in the inner loop to


iterate through the array elements during sorting.

t An int variable used to temporarily hold the value of an array element


during the swapping process in the Bubble Sort algorithm.
3. Design a class to overload a function series() as follows:

I. double series(double n):it calculates and returns sum of the following series upto n terms:
sum=1/1+1/2+1/3+…….+1/n.
II. double series(double a, double n):it calculates and returns sum of the following series upto n
terms:
sum=1/a2+4/a5+7/a8+10/a11+………n terms.

// Define a class named Series


public class Series
{
// Method to calculate the sum of the first series
double series(double n)
{
double sum = 0;
for (int i = 1; i <= n; i++)
{
double term = 1.0 / i; // Calculate the current term of the series
sum += term; // Add the current term to the sum
}
return sum; // Return the sum of the series
}
// Method to calculate the sum of the second series
double series(double a, double n)
{
double sum = 0;
int x = 1;
for (int i = 1; i <= n; i++)
{
int e = x + 1;
double term = x / Math.pow(a, e); // Calculate the current term of the series
sum += term; // Add the current term to the sum
x += 3; // Increment x by 3
}
return sum; // Return the sum of the series
}
// Main method
public static void main()
{
Series obj = new Series(); // Create an object of the Series class
// Calculate and print the sum of the first and second series
System.out.println("First series sum = " + obj.series(5));
System.out.println("Second series sum = " + obj.series(3, 8));
}
}
Variab Description
le

n A double parameter representing the limit for the number of terms in the first
series.

a A double parameter representing the base value for calculations in the second
series.

sum A double variable used to accumulate the sum of series terms.

x An int variable used in the second series calculation as part of the formula for
terms.

i An int variable used as a loop control variable to iterate through the terms in
both series.

e An int variable calculated based on x in the second series calculation to


determine the exponent.

term A double variable that stores the value of the current term in both series
calculations.

obj An object of the Series class, created in the main method, used to call the series
methods and calculate sums.
4.Write a program to accept a word and convert into lowercase if it is in uppercase ,and displaythe
new word by replacing only the vowels with the character following it.

import java.util.*;

// Define a class named project_4

public class project_4

public static void main()

Scanner sc = new Scanner(System.in);

// Prompt the user to enter a word

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

String str = sc.nextLine(); // Read the user input

str = str.toLowerCase(); // Convert the input to lowercase

String newStr = ""; // Initialize a variable to store the modified string

int len = str.length(); // Get the length of the input string

// Iterate through each character of the input string

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

char ch = str.charAt(i); // Get the current character

// Check if the character is a vowel (a, e, i, o, u)

if (str.charAt(i) == 'a' ||

str.charAt(i) == 'e' ||

str.charAt(i) == 'i' ||

str.charAt(i) == 'o' ||

str.charAt(i) == 'u')

char nextChar = (char) (ch + 1); // Get the next character in the ASCII sequence

newStr = newStr + nextChar; // Append the next character to the modified string

} else

newStr = newStr + ch; // Append the original character to the modified string
}

// Print the modified string

System.out.println(newStr);

Variabl Description
e

sc An instance of the Scanner class used to read input from the user via the
command line.

str A String variable that stores the original word input by the user.

newStr A String variable that stores the modified string with certain vowel characters
replaced.

len An int variable representing the length of the input string.

i An int variable used as a loop control variable to iterate through the


characters of the input string.

ch A char variable that holds the current character being processed during the
iteration.

nextChar A char variable that holds the next character in the ASCII sequence, used to
replace vowels.
5.Write a program to input and store the weight of ten people. Sort and display them in descending
orderusing selection sort technique.

import java.util.*;

// Define a class named project_5

class project_5

public static void main()

Scanner sc = new Scanner(System.in);

double weightArr[] = new double[10]; // Declare an array to store weights

// Prompt the user to enter weights of 10 people

System.out.println("Enter weights of 10 people: ");

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

weightArr[i] = sc.nextDouble(); // Read and store weights in the array

// Implementation of Selection Sort algorithm to sort the weights array in descending order

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

int idx = i;

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

if (weightArr[j] > weightArr[idx])

idx = j; // Find the index of the maximum weight in the unsorted portion

double t = weightArr[i];

weightArr[i] = weightArr[idx];

weightArr[idx] = t; // Swap the maximum weight with the current position

}
// Display the sorted weights array in descending order

System.out.println("Sorted Weights Array:");

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

System.out.print(weightArr[i] + " "); // Print each weight separated by space

Variabl Description
e

sc An instance of the Scanner class used to read input from the user via the
command line.

weightArr A double array that stores the weights of 10 people.

i An int variable used as a loop control variable to iterate through the array of
weights.

j An int variable used as a loop control variable to iterate through the remaining
unsorted portion.

idx An int variable that stores the index of the maximum weight in the remaining
unsorted portion.

t A double variable used to temporarily hold the value of an array element during
swapping.

You might also like