0% found this document useful (0 votes)
28 views30 pages

Index: Topic

Uploaded by

Sarbajit Das
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)
28 views30 pages

Index: Topic

Uploaded by

Sarbajit Das
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/ 30

INDEX

SL Topic Pg
NO
No
1 Program to define a class RecurringPatterns and define methods in it, which will print 1-6
the following patterns using switch case.
a)The method takes an integer argument n and prints the following
pattern, shown for n=4.
b) The method takes an integer argument and prints the following
pattern, shown for n=4.
2 Program to Define a class called Library with the following description: Instance 7-10
variables/data members: Int acc_num – stores the accession number of the book
String title – stores the title of the book stores the name of the author Member
Methods: (i) void input() – To input and store the accession number, title and author.
(ii)void compute – To accept the number of days late, calculate and display and fine
charged at the rate of Rs.2 per day. (iii) void display() To display the details in the
following format: Accession Number Title Author Write a main method to create an
object of the class and call the above member method
3 Write a program to input the age, gender (male or female) and Taxable Income of a 11-14
person. If the age is more than 65 years or the gender is female, display “wrong
category*. If the age is less than or equal to 65 years and the gender is male, compute
and display the Income Tax payable as per the table given above.
4 Write a program to accept a string. Convert the string to uppercase. Count and output 15-17
the number of double letter sequences that exist in the string. Sample Input: “SHE
WAS FEEDING THE LITTLE RABBIT WITH AN APPLE Sample Output: 4
5 In Piglatin a word such as KING becomes INGKAY, TROUBLE becomes OUBLETRAY as 18-20
so on. The first vowel of the original word becomes the starting of the translation and
proceeding letter being shifted towards the end and followed by AY. Word that begins
with a vowel is left unchanged. Write a class to accept a word and convert in to
Piglatin word.
6 Define a class Pay in java with following specification: Data Members String: name, 21-24
address & city double :salary, da, hra,pf, gs, ns Member functions Pay(String n, String
add, String cy, Float s)-Parameterized constructor to initialize the data members. void
outputData()-to display initial values void calculate() to calculate following details: da
15% of salary hra 10% of salary pf 12% of salary gs salary + da + hra ns = gs - pf void
display() to display the complete information

7 Write a program to accept a word and display the new sword after removing the 25-27
vowels present in the word. Example: COMPUTER APPLICATION --> CMPTR PPLCTNS
8 Write a program in Java to input the values of x and n and print the sum of the 28-29
following series: S = 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + ……… to n terms
Program no – 1 1
Program Definition - Define a class RecurringPatterns and define
methods in it, which will print the following patterns using switch case. a)
The method takes an integer argument n and prints the following pattern,
shown for n=4.

a
aa
aaa
aaaa
aaa
aa
a
b) The method takes an integer argument and prints the following pattern,
shown for n=4.

121

12321

1234321

12321

121

Algorithm –

Steps in the Algorithm:

Initialization:

o Prompt the user to choose between two patterns.


o Read the user input (either 1 or 2).
Pattern 1 2

o Read the value n from the user.


o Print the upper part of the diamond by printing spaces followed
by 'a' characters.
o Print the lower part of the diamond similarly.

Pattern 2:

o Read the value n1 from the user.


o Print the upper half of the numeric pyramid by printing spaces
followed by ascending and then descending numbers.
o Print the lower half of the numeric pyramid similarly.

Invalid input:

If the input is not 1 or 2, display a message "Wrong input".


o
Source code :

import java.util.*;
public class pattern51

{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Press 1 for pattern 1");
System.out.println("Press 2 for pattern 2");
int ch = in.nextInt();
switch (ch){
case 1:

System.out.println("Enter the value of n");


int n = in.nextInt();
int s = n-1;
int l = n+s;
for(int i = 1 ;i<=n;i++) 3
{
for(int j = n ;j>=i;j--)
{
System.out.print(" ");
}
for(int k = 1 ;k<=i;k++)
{
System.out.print("a ");
}
System.out.println();
}
for(int i = n-1 ;i>=1;i--)
{
for(int j = n ;j>=i;j--)
{
System.out.print(" ");
}
for(int k = 1 ;k<=i;k++)
{
System.out.print("a ");
}
System.out.println();
}
break;
case 2:
{
System.out.println("Enter the value of n");
int n1 = in.nextInt();
for (int i = 1; i <= n1; i++) {

for (int j = 1; j <= n1 - i; j++) {


System.out.print(" "); 4
}

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


System.out.print(j);
}

for (int j = i - 1; j >= 1; j--) {


System.out.print(j);
}

System.out.println();
}

for (int i = n1 - 1; i >= 1; i--) {

for (int j = 1; j <= n1 - i; j++) {


System.out.print(" ");
}

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


System.out.print(j);
}

for (int j = i - 1; j >= 1; j--) {


System.out.print(j);
} 5
System.out.println();
}
}
break;
default:
{System.out.println("Wrong input");}
}

}}

Input and output:

Variable Description Table

Variable Name Data Type Purpose


ch int To store the specific
input for the swtich
case
n int Used in taking the line
no input
i int Used in for loop
j int Used in for loop
k int Used in for loop

Program no - 2 7
Program Definition - Define a class called Library with the following
description: Instance variables/data members: Int acc_num – stores the
accession number of the book String title – stores the title of the book
stores the name of the author Member Methods: (i) void input() – To input
and store the accession number, title and author. (ii)void compute – To
accept the number of days late, calculate and display and fine charged at
the rate of Rs.2 per day. (iii) void display() To display the details in the
following format: Accession Number Title Author Write a main method to
create an object of the class and call the above member methods.
Algorithm –

Initialize Variables:

 Declare int no, days, fine; String name, title.

Define Methods:

 input(n, a, t):
o Assign n to no, a to name, and t to title.
 compute(n):
o Set days = n and calculate fine = days * 2.
 display():
o Print book details and the fine.

In main method:

 Create library ob and Scanner in.


 Prompt user for:
o n (accession number), t (title), a (author), and days (late days).
 Call input(n, a, t) to store data.
 Call compute(days) to calculate the fine.
 Call display() to show the book details and fine.

End Program.

Source code -
import java.util.*; 8
public class library
{
int no, days,fine;
String name = " ";
String title = " ";
void input(int n,String a,String t)
{
name = a;
title = t;
no = n;
}
void compute(int n)
{
days = n;
fine = days*2;
}
void display()
{
System.out.println("Accession no Title Author ");
System.out.println(" "+no+" "+title+" " +name+" ");
System.out.println("The fine imported = "+fine);
}
public static void main()
{
library ob = new library();
Scanner in = new Scanner (System.in);
System.out.println("Enter the accesion no");
int n = in.nextInt();
System.out.println("Enter the Title of the book");
String t = in.next();
System.out.println("Enter the Author name");
String a = in.next(); 9
System.out.println("Enter the days late");
int days = in.nextInt();
ob.input(n,a,t);
ob.compute(days);
ob.display();
}
}

Input and output


Variable Description Table
Variable Name Data Type Purpose
No,days,fine int To store the
Respective values of
the variables
Name String To Store the name of
the author
title String To Store the title of
the book
n,days int Variables used in main
method , used in after
calling the methods
a,t String Used input of name
and title of the book,

Method Description Table


Method name Method type Purpose
Input() void To input and store the accession
number, title and author.
Compute() void To accept the number of days late,
calculate and display and fine charged
at the rate of Rs.2 per day.
Display() void void display() To display the details in
the following format: Accession
Number Title Author
Program no -3 11
Program Definition - Given below is a hypothetical table showing rates of
Income Tax for male citizens below the age of 65 years: Taxable Income
(TI) in Income Tax in Does not exceed 1,60,000 Nil Is greater than 1,60,000
and less than or equal to 5,00,000 ( TI – 1,60,000 ) * 10% Is greater than
5,00,000 and less than or equal to 8,00,000 [ (TI - 5,00,000 ) *20% ] +
34,000 Is greater than 8,00,000 [ (TI - 8,00,000 ) *30% ] + 94,000 Write a
program to input the age, gender (male or female) and Taxable Income of
a person. If the age is more than 65 years or the gender is female, display
“wrong category*. If the age is less than or equal to 65 years and the
gender is male, compute and display the Income Tax payable as per the
table given above.
Algorithm –

Input:

 Prompt the user to input their gender (M for Male, F for Female).
 If the gender is M, proceed with further inputs:
o Prompt the user to input their age.
o If the age is less than 65, prompt the user to enter their income.

Tax Calculation:

 If the income is ≤ 160,000:


o Print: "Income tax payable = NIL".
 If the income is between 160,001 and 500,000:
o Calculate tax: tax = (income - 160,000) * 0.1 and display the tax
payable.
 If the income is between 500,001 and 800,000:
o Calculate tax: tax = (income - 160,000) * 0.2 + 34,000 and
display the tax payable.
 If the income is greater than 800,000:
o Calculate tax: tax = (income - 160,000) * 0.3 + 94,000 and
display the tax payable.
Invalid Input Handling: 12

 If the gender is not M, display: "Wrong Category".


 If the age is 65 or above, no tax calculation is done.

 End of Program.

Source Code –
import java.util.*;
public class tax
{
public static void main()
{
Scanner in = new Scanner (System.in);
System.out.println("Enter the Gender");
System.out.println("M for Male or F for female");
char ch = in.next().charAt(0);

if(ch=='M')
{
System.out.println("Enter the Age");
int age = in.nextInt();

if(age<65)
{
System.out.println("Enter the Income");
int income = in.nextInt();
double tax ;
if(income<=160000)
{
System.out.println("Income tax payable = NIL");
}
else if (income>160000&&income<=500000)
{ 13
tax = (income-160000)*(0.1);
System.out.println("Income tax payable = "+tax);
}
else if (income>500000&&income<=800000)
{
tax = ((income-160000)*(0.2))+34000;
System.out.println("Income tax payable = "+tax);
}
else if (income>800000)
{
tax = ((income-160000)*(0.3))+94000;
System.out.println("Income tax payable = "+tax);
}
}
else
{
System.out.println("Wrong Category");
}
}
else
{
System.out.println("Wrong Category");
}

Input and Output 14


Variable Description Table
Variable Name Data type Purpose
ch Char To take the input of
male or female
age int To store the value of
the age
income int To store the value of
the income
tax String To store the value of
the tax

Program no 4 15
Program Definition - Write a program to accept a string. Convert the string
to uppercase. Count and output the number of double letter sequences
that exist in the string. Sample Input: “SHE WAS FEEDING THE LITTLE
RABBIT WITH AN APPLE Sample Output: 4
Algorithm –

Start.

Input:

o Prompt the user to enter a string (input).

Convert Input to Uppercase:

o Convert the string input to uppercase to make the comparison


case-insensitive.

Initialize Count:

o Initialize a variable count to 0. This will store the number of


double-letter sequences.

Loop Through the String:

o For each index i from 0 to length of string - 2:


 Compare the character at index i with the character at
index i + 1:
 If they are the same, increment count by 1.

Output:

o Print the value of count, which represents the number of


double-letter sequences.

End.
Source Code- 16
import java.util.*;
public class DoubleLetterSequences {
public static void main() {
Scanner in = new Scanner(System.in)
System.out.print("Enter the string: ");
String input = in.nextLine();
String upperCaseString = input.toUpperCase();
int count = 0;
for (int i = 0; i < upperCaseString.length() - 1; i++) {
if (upperCaseString.charAt(i) ==upperCaseString.charAt(i + 1)) {
count++;
}
}
System.out.println("Number of double letter sequences: " + count);
}
}

Input and Output


Variable Description Table
Variable name Data type Purpose
input String To take the input String
upperCaseString String Converted String to
uppercase
count int No of double Letter
Sequence
i int Used in loop
Program no 5 18
Problem Definition :
In Piglatin a word such as KING becomes INGKAY, TROUBLE becomes
OUBLETRAY as so on. The first vowel of the original word becomes the
starting of the translation and proceeding letter being shifted towards the
end and followed by AY. Word that begins with a vowel is left unchanged.
Write a class to accept a word and convert in to Piglatin word.
Algorithm –
Start
Input: Read a word from the user.
Convert to Lowercase: Convert the word to lowercase for uniformity.
Loop Through the Word:

 For each character in the word:


o Check if the character is a vowel.
o If a vowel is found, move all characters before it to the end of
the string and append "ay".

Output: Print the word in Pig Latin.


End.
Source Code :
import java.util.*;
public class Piglatin
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the word");
String ch = in.next();
String ci = " ";
ch = ch.toLowerCase();
int l = ch.length();
for(int i = 0 ;i<l;i++)
{ 19
int c = ch.charAt(i);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
{
ci=ch.substring(i)+ch.substring(0,i)+'a'+'y';
break;
}
}
System.out.println("The word in Piglatin = "+ci);
}
}

Input and Output


Variable Description table
Variable name Data Type Purpose
ch String Used to store the input
String
ci String Lower case string
stored
I int Length of the string
c int Used in for loop

Program no -6 21
Program Definition –
Define a class Pay in java with following specification: Data Members
String: name, address & city double :salary, da, hra,pf, gs, ns Member
functions Pay(String n, String add, String cy, Float s)-Parameterized
constructor to initialize the data members. void outputData()-to display
initial values void calculate() to calculate following details: da 15% of salary
hra 10% of salary pf 12% of salary gs salary + da + hra ns = gs - pf void
display() to display the complete information
Algorithm
Source Code :
Input:

 Prompt for employee's name, salary, city, and address.

Initial Output:

 Display the initial details (name, salary, address, city).

Calculations:

 Compute DA as 15% of salary.


 Compute HRA as 10% of salary.
 Compute PF as 12% of salary.
 Compute gross salary as salary + DA + HRA.
 Compute net salary as gross salary - PF.

Final Output:

 Display the calculated DA, HRA, PF, gross salary, and net salary.

End

import java.util.*;
public class Pay
{
String name,address,city;
double salary, da, hra , pf, grossSal , netSal; 22
Pay(String n ,float s,String cy,String add)
{
name = n;
salary = s;
address = add;
city = cy;
}
void outputData()
{
System.out.println("Initial Data");
System.out.println("Name of the Employee "+name);
System.out.println("Adress of the employee "+address);
System.out.println("City of the employee "+city);
System.out.println("Salary of the employee "+salary);
}
void calculate()
{
da = (0.15*salary);
hra = (0.10*salary);
pf = (0.12*salary);
grossSal = (salary + da + hra);
netSal = (grossSal+pf);
}
void display()
{
System.out.println("Final Data");
System.out.println("Name of the employee = "+name);
System.out.println("Adress of the employee "+address);
System.out.println("City of the employee "+city);
System.out.println("Salary of the employee = "+salary);
System.out.println("dearness allowance of the employee = "+da);
System.out.println("house rent allowance the employee = "+hra); 23
System.out.println("Provident fund of the employee = "+pf);
System.out.println("Gross salary of the employee = "+grossSal);
System.out.println("Net salary of the employee = "+netSal);
}
public static void main()
{
Scanner in = new Scanner (System.in);
System.out.println("Enter the Name of the employee ");
String n = in.nextLine();
System.out.println("Enter the Salary ");
float s = in.nextFloat();
System.out.println("Enter the City of the employee");
String cy = in.next();
System.out.println("Enter the Address of the employee");
String add = in.next();
Pay ob = new Pay(n,s,cy,add);
ob.outputData();
ob.calculate();
ob.display();
}
}

Input and output


Variable Description Table
Variable Name Data type Purpose
Name, address & city String To store its respective
strings
:salary, da, hra,pf, gs, ns double To store its respective
values

Method Description table


Method name Data type Purpose
Pay(String n, String add, -Parameterized constructor
String cy, Float s) to initialize the data
members
outputdata() void to display initial values
void calculate() void To calculate
void display() void to display the complete
information

Program no 7 25
Program definition;
Write a program to accept a word and display the new sword after
removing the vowels present in the word. Example: COMPUTER
APPLICATION --> CMPTR PPLCTNS
Algorithm-

Start:.

Input:

o Prompt the user to enter a sentence.


o Read the sentence as input.

Convert to Lowercase:

o Convert the entire sentence to lowercase to treat uppercase


and lowercase vowels equally.

Initialize StringBuilder:

o Create an empty StringBuilder to store the result (the sentence


without vowels).

Iterate Over Each Character:

o Loop through each character of the sentence.


o For each character:
 Check if the character is a vowel (a, e, i, o, u).
 If the character is not a vowel, append it to the
StringBuilder.

Output:

o After the loop finishes, output the modified sentence stored in


the StringBuilder (i.e., the sentence without vowels).

End:

Source code: 26
import java.util.*;
public class vow
{
public static void main()
{
Scanner in = new Scanner (System.in);
System.out.println("Enter the Sentence");
String ch = in.nextLine();
int l = ch.length();
ch = ch.toLowerCase();
String ci = " ";
for(int i = 0 ;i<l;i++)
{
char c = ch.charAt(i);
if(c!='a'&&c!='e'&&c!='i'&&c!='o'&&c!='u')
{
ci = ci+c;
}
}
System.out.println("The Sentence without Vowel = "+ci);
}
}
Input and output
Variable Description Table
Variable name Data type Purpose
Ch String Input string
Ci String Updated string
I int Used in loop
c char Characters of the string
are stored here
Program no 8 28
Program Definition - Write a program in Java to input the values of x and n
and print the sum of the following series: S = 1 + (x+2)/2! + (2x+3)/3! +
(3x+4)/4! + ……… to n terms
Algorithm:
Start:
Input: Read values x and n from the user.
Initialize:

 Set sum = 1 (first term).


 Set fact = 1 (factorial of the first term).

Loop: For each i from 2 to n:

 Update fact to fact * i to get the factorial for the current term.
 Calculate the current term using the formula ((i - 1) * x + i) / fact.
 Add the term to sum.

Output: Display the sum of the series.


End:.
Source Code:
import java.util.*;
public class SeriesSum {
public static void main() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of x: ");
int x = in.nextInt();
System.out.print("Enter the value of n: ");
int n = in.nextInt();
double sum = 1;
for (int i = 2; i <= n; i++) {
long fact = 1;
for (int j = 1; j <= i; j++) {
fact *= j;
}
double term = ((i - 1) * x + i) / (double) fact; 29
sum += term;
}
System.out.println("The sum of the series is: " + sum);
}
}

Input and output

Variable Description Table


Variable Name Data type Purpose
X,n int Variable used to take
the input
fact long Factorials stored
Sum,term double Used to store respective
values
I,j int Used in loop as local
variables

You might also like