Index: Topic
Index: Topic
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 –
Initialization:
Pattern 2:
Invalid input:
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();
}
}}
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:
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:
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:
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:
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");
}
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:
Initialize Count:
Output:
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);
}
}
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:
Initial Output:
Calculations:
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();
}
}
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:
Convert to Lowercase:
Initialize StringBuilder:
Output:
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:
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.