Computer Project Part 2 (10-15)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

Computer

Project
2022-23
Name: xxx
Grade: xx
Roll No: xx
ACKNOWLEDGEMENT

I would like to thank Ms. for her valuable

guidance and help. She clarified all my queries about the

project without which I would not have been able to

complete the project. I would also like to thank my family

for providing all the necessary materials and my friends for

helping me with this project.


INDEX
S.No. Programs Pg.No.
1. For Loop Pattern Program using switch-case
2. For Loop Series program
3. Number Programs (Special Number & Harshad
Number )
4. Function (FruitJuice – using Instance variable)
5. Function (Parcel)
6. Nested functions (Sum of factorial of even digits)
7. Function Overloading-1
8. Function Overloading-2
9. Constructor (Finding the roots of a quadratic
equation)
10. String and Character functions - 1
11. String and Character functions - 2
12. String and Character functions - 3
13. String and Character functions - 4
14. Finding the count of Palindrome words
15. Bubble sort
16. Sum of array elements
17. Linear Search - 1
18. Linear Search - 2
19. Binary Search - 1
20. Binary Search – 2
Question 10:

String and Character functions - 1


Write a program in Java to input two strings of same length and form a new word in
such a way that, the first character of the first word is followed by the first character of
the second word and so on. If the strings are not of same length, then print the message
“Invalid Input”.

Sample Input: String 1: BALL

String 2: WORD

Output: BWAOLRLD

Divyasha Satapathy 1
Program 10:

import java.util.Scanner;

class Program_10

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter two strings of equal length:");

String s1=sc.next();

String s2=sc.next();

String res="";

if(s1.length()==s2.length())

for(int i=0;i<s1.length();i++)

res=res+s1.charAt(i)+s2.charAt(i);

System.out.println(res);

else

System.out.println("Invalid Input");

Divyasha Satapathy 2
}

Divyasha Satapathy 3
Variable Description Table:

Variable Name Data type Purpose


s1 String To store the first string entered by the
user
S2 String To store the second string entered by the
user
res String To store the result
i int To iterate the for loop to form the result

Output:

Divyasha Satapathy 4
Divyasha Satapathy 5
Question 11:

String and Character functions - 2


Write a program to assign a full path and file name as given below. Using library
functions, extract and display the file path, file name and file extension separately as
shown.
Sample Input: C:\Users\admin\pictures\flower.jpg
Output path: C:\Users\admin\pictures\
File name : flower
Extension: jpg

Divyasha Satapathy 6
Program 11:

import java.util.Scanner;

class Program_11

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter a full path and file name:");

String str=sc.nextLine();

String output_path=str.substring(0,(str.lastIndexOf('\\')+1));

String file_name=str.substring(str.lastIndexOf('\\')
+1,str.lastIndexOf('.'));

String extension=str.substring(str.lastIndexOf('.')+1,str.length());

System.out.println("Output path: "+output_path);

System.out.println("File name: "+file_name);

System.out.println("Extension: "+extension);

Divyasha Satapathy 7
Variable Description Table:

Variable Name Data type Purpose


str String To store the full path and file name
entered by the user
output_path String To store the output path
file_name String To store the file name
extension String To store the extension

Output:

Divyasha Satapathy 8
Question 12:

String and Character functions - 3


Write a program to accept a String in uppercase and replace all the vowels with ‘@’
present in the String.

Sample Input : “TATA STEEL IS IN JAMSHEDPUR”


Output: T@T@ ST@@L @S @N J@MSH@DP@R

Divyasha Satapathy 9
Program 12:

import java.util.Scanner;

class Program_12

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter a string in uppercase:");

String str=sc.nextLine();

String res="";

for(int i=0;i<str.length();i++)

char ch=str.charAt(i);

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

res=res+"@";

else

res=res+ch;

System.out.println(res);

Divyasha Satapathy 10
}

Divyasha Satapathy 11
Variable Description Table:

Variable Name Data type Purpose


str String To store the string entered by the user
res String To store the result
i int To iterate the for loop to form the result
ch char To store the character at each index of
the string

Output:

Divyasha Satapathy 12
Question 13:

String and Character functions - 4


Write a program to accept a word in lower case and display the new word after
removing all the repeated letters.

Sample Input: applications

Output: aplictons

Divyasha Satapathy 13
Program 13:

import java.util.Scanner;

class Program_13

public static void main()

Scanner sc=new Scanner(System.in);

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

String word=sc.next();

int i=0;

String res="";

while(i<word.length())

char ch=word.charAt(i);

if(word.indexOf(ch)!=word.lastIndexOf(ch))

if(i==word.lastIndexOf(ch))

else

Divyasha Satapathy 14
{

res=res+ch;

else

res=res+ch;

i++;

System.out.println(res);

Divyasha Satapathy 15
Variable Description Table:

Variable Name Data type Purpose


word String To store the word entered by the user
res String To store the result
i int To iterate the while loop to form the result
ch char To store the character at each index of
the word

Output:

Divyasha Satapathy 16
Question 14:

Finding the count of Palindrome words

Write a program to declare an array to accept and store five words. Display the number
of palindrome words.

Sample Input: MADAM

TEACH

RACECAR

EXAM

MALAYALAM

Sample Output: Number of palindrome words : 3

Divyasha Satapathy 17
Program 14:

import java.util.Scanner;

class Program_14

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter 5 words:");

String arr[]=new String[5];

String rev="";

int count=0;

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

arr[i]=sc.next();

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

for(int k=arr[j].length()-1;k>=0;k--)

rev=rev+arr[j].charAt(k);

Divyasha Satapathy 18
if(arr[j].equals(rev)==true)

count=count+1;

rev="";

System.out.println("Number of palindrome words: "+count);

Divyasha Satapathy 19
Variable Description Table:

Variable Name Data type Purpose


arr[] String To store the words entered by the user
rev String To store the reverse of the word
count int To store the number of palindrome words
i int To iterate the for loop to store 5 words in
the array
j int To iterate the outer for loop for each
element
k int To iterate the inner for loop to calculate
the reverse of each word and to count the
number of palindrome words

Output:

Divyasha Satapathy 20
Question 15:

Bubble sort

Write a program in Java to input five city names in an array. Using bubble sort arrange
them in descending order and display the same.
Sample input: AGRA
MUMBAI
CHENNAI
DELHI
PUNE
Sample output:
PUNE
MUMBAI
DELHI
CHENNAI
AGRA

Divyasha Satapathy 21
Program 15:

import java.util.Scanner;

class Program_15

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter 5 cities:");

String arr[]=new String[5];

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

arr[i]=sc.next();

String temp="";

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

for(int k=0;k<4;k++)

if(arr[k].compareTo(arr[k+1])<0)

temp=arr[k];

arr[k]=arr[k+1];

Divyasha Satapathy 22
arr[k+1]=temp;

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

System.out.println(arr[l]);

Divyasha Satapathy 23
Variable Description Table:

Variable Name Data type Purpose


arr[] String To store the 5 cities entered by the user
i int To iterate the for loop to store the 5 cities
entered by the user in an array
temp String To store the city name present in an array
at a particular index
j int To iterate the outer for loop for each
element of the array
k int To iterate the inner for loop to sort the
cities in the array in descending order
l int To iterate the for loop to print the city
present at each index of the array

Output:

Divyasha Satapathy 24
Question 16:

Sum of array elements

Write a program to create two separate arrays A and B. Accept 6 numbers for each
array A and B. Find Sum1 and Sum2 as follows and print Sum1 and Sum2:

Sum1=Elements in odd subscript of array A + Elements in even subscript of B.


Sum2= Elements in odd subscript of array B + Elements in even subscript of A.

Divyasha Satapathy 25
import java.util.Scanner;

class Program_16

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter 6 numbers for array A:");

double array_A[]=new double[6];

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

array_A[i]=sc.nextDouble();

Divyasha Satapathy 26
}

System.out.println("Enter 6 numbers for array B:");

double array_B[]=new double[6];

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

array_B[j]=sc.nextDouble();

double Sum1=0.0;

double Sum2=0.0;

for(int k=0,l=1;k<6&&l<6;k=k+2,l=l+2)

Sum1=Sum1+array_A[l]+array_B[k];

Sum2=Sum2+array_B[l]+array_A[k];

System.out.println("Sum1= "+Sum1);

System.out.println("Sum2= "+Sum2);

Divyasha Satapathy 27
Variable Description Table:

Variable Name Data type Purpose


array_A double To store the 6 numbers for array A
entered by the user
i int To iterate the for loop to store the 6
numbers entered by the user in array A
array_B double To store the 6 numbers for array B
entered by the user
j int To iterate the for loop to store the 6
numbers entered by the user in array B
k int To serve for the index of the elements in
the even subscript in each array
l int To serve for the index of the elements in

Divyasha Satapathy 28
the odd subscript in each array

Output:

Divyasha Satapathy 29
Question 17:

Linear Search - 1
Write a program to initialize an array of 10 names and initialize another array with their
respective telephone numbers. Using linear search technique, search for a name given
by the user in the list. If found, display “Search successful” and print the name along
with the telephone number, otherwise display “Search Unsuccessful. Name not
enlisted”.

Divyasha Satapathy 30
Program 17:
import java.util.Scanner;

class Program_17

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter 10 names:");

String name[]=new String[10];

boolean flag=false;

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

name[i]=sc.next();

System.out.println("Enter their respective telephone numbers:");

long number[]=new long[10];

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

Divyasha Satapathy 31
number[j]=sc.nextLong();

System.out.println("Enter a name to be searched:");

String search=sc.next();

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

if(search.equals(name[k]))

System.out.println("Search successful");

System.out.println(name[k]);

System.out.println(number[k]);

flag=true;

break;

if(flag==false)

System.out.println("Search Unsuccessful.Name not enlisted.");

Divyasha Satapathy 32
Variable Description Table:

Variable Name Data type Purpose


name[] String To store the 10 names entered by the
user
flag boolean To show whether search is successful or
unsuccessful
i int To iterate the for loop to store 10 names
in the array-name[]
number[] long To store the 10 respective telephone
numbers entered by the user
j int To iterate the for loop to store 10
telephone numbers in the array-number[]
search String To store the name to be searched in the
list entered by the user
k int To iterate the for loop to search the name
in the list

Output:

Divyasha Satapathy 33
Divyasha Satapathy 34
Question 18:

Linear Search - 2
Write a program to accept name and monthly salary of 5 employees and store them in
two separate arrays. Display the names of the employees who earn more than
Rs.50000 per month, using linear search.

Divyasha Satapathy 35
Program 18:

import java.util.Scanner;

class Program_18

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter names of 5 employees:");

String name[]=new String[5];

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

name[i]=sc.next();

System.out.println("Enter salaries of the 5 employees:");

double salary[]=new double[5];

Divyasha Satapathy 36
for(int j=0;j<5;j++)

salary[j]=sc.nextDouble();

System.out.println("Employees who earn more than Rs.50000 per month:");

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

if(salary[k]>50000)

System.out.println(name[k]);

Divyasha Satapathy 37
Variable Description Table:

Variable Name Data type Purpose


age int To store user’s age
gender String To store user’s gender
income int To store user’s taxable income
tax double To store user’s tax
Output:

Divyasha Satapathy 38

You might also like