Question No.
: 1
Define a class called with the following specifications:
Class name: Eshop
Member variables:
String name: name of the item purchased
double price: Price of the item purchased
Member methods:
void accept(): Accept the name and the price of the item using the methods of
Scanner class.
void calculate(): To calculate the net amount to be paid by a customer, based on
the following criteria:
Price Discount
1000 – 25000 5.0%
25001 – 57000 7.5 %
57001 – 100000 10.0%
More than 100000 15.0 %
void display(): To display the name of the item and the net amount to be paid.
Write the main method to create an object and call the above methods.
import [Link];
public class Eshop
{
String name;
double price;
double disc;
double amount;
public void accept()
{
Scanner in = new Scanner([Link]);
[Link]("Enter item name: ");
name = [Link]();
[Link]("Enter price of item: ");
price = [Link]();
}
public void calculate()
{
double d = 0.0;
if (price < 1000)
d = 0.0;
else if (price <= 25000)
d = 5.0;
else if (price <= 57000)
d = 7.5;
else if (price <= 100000)
d = 10.0;
else
d = 15.0;
disc = price * d / 100.0;
amount = price - disc;
}
public void display()
{
[Link]("Item Name: " + name);
[Link]("Net Amount: " + amount);
}
public static void main(String args[])
{
Eshop obj = new Eshop();
[Link]();
[Link]();
[Link]();
}
}
Question No. : 2
DTDC a courier company charges for the courier based on the weight of the parcel.
Define a class with the following specifications:
Class name: courier
Member variables:
name – name of the customer
weight – weight of the parcel in kilograms
address – address of the recipient
bill – amount to be paid
type – 'D'- domestic, 'I'- international
Member methods:
void accept ( ) — to accept the details using the methods of the Scanner class only.
void calculate ( ) — to calculate the bill as per the following criteria:
Weight in Kgs Rate per Kg
First 5 Kgs Rs.800
Next 5 Kgs Rs.700
Above 10 Kgs Rs.500
An additional amount of Rs.1500 is charged if the type of the courier is I
(International)
void print ( ) — To print the details
void main ( ) — to create an object of the class and invoke the methods
import [Link];
public class courier
{
String name;
double weight;
String address;
double bill;
char type;
public void accept()
{
Scanner in = new Scanner([Link]);
[Link]("Enter customer's name: ");
name = [Link]();
[Link]("Enter recipient's address: ");
address = [Link]();
[Link]("Enter parcel weight (in Kgs): ");
weight = [Link]();
[Link]("Enter courier type");
[Link]("D (Domestic), I (International): ");
type = [Link]().charAt(0);
}
public void calculate()
{
if (weight <= 5)
{
bill = weight * 800;
} else if (weight <= 10)
{
bill = (5 * 800) + ((weight - 5) * 700);
} else
{
bill = (5 * 800) + (5 * 700) + ((weight - 10) * 500);
}
if (type == 'I')
{
bill += 1500;
}
}
public void print() {
[Link]("Customer's Name: " + name);
[Link]("Parcel Weight: " + weight + " Kgs");
[Link]("Recipient's Address: " + address);
[Link]("Type of Courier: " + type);
[Link]("Bill Amount: Rs." + bill);
}
public static void main(String[] args)
{
courier obj = new courier();
[Link]();
[Link]();
[Link]();
}
}
Question No. : 3
Define a class with the following specifications:
Class name: Bank
Member variables:
double p — stores the principal amount
double n — stores the time period in years
double r — stores the rate of interest
double a — stores the amount
Member methods:
void accept () — input values for p and n using Scanner class methods only.
void calculate () — calculate the amount based on the following conditions:
Time in (Years) Rate %
Upto 1⁄2 9
> 1⁄2 to 1 year 10
> 1 to 3 years 11
> 3 years 12
a=p(1+r100)na=p(1+100r)n
void display () — display the details in the given format.
Principal Time Rate Amount
XXX XXX XXX XXX
Write the main method to create an object and call the above methods.
import [Link];
public class Bank
{
double p;
double n;
double r;
double a;
void accept()
{
Scanner in = new Scanner([Link]);
[Link]("Enter principal amount: ");
p = [Link]();
[Link]("Enter time period in years: ");
n = [Link]();
}
void calculate()
{
if (n <= 0.5)
{
r = 9;
}
else if (n <= 1)
{
r = 10;
}
else if (n <= 3)
{
r = 11;
}
else
{
r = 12;
}
a = p * [Link](1 + (r / 100), n);
}
void display()
{
[Link]("Principal\tTime\tRate\tAmount");
[Link](p + "\t" + n + "\t" + r + "\t" + a);
}
public static void main(String args[])
{
Bank b = new Bank();
[Link]();
[Link]();
[Link]();
}
}
Question No. : 4
Write a menu driven program for the following.
Enter an option 1 to display Floyd’s triangle 2. For inverted triangle.
Floyd’s Triangle Inverted triangle
1 6 6 6 6 6 6
2 3 5 5 5 5 5
4 5 6 4 4 4 4
7 8 6 10 3 3 3
……… n rows 2 2
1
import [Link].*;
class Menu
{
public static void main (String args[])
{
Scanner in =new Scanner ([Link]);
int i,j,n,x=1,ch;
[Link](" Enter 1 to display Floyd’s triangle");
[Link](" Enter 2 For inverted triangle.");
[Link]("Enter your choice..");
ch=[Link]();
switch (ch)
{
case 1:[Link]("Enter n value");
n=[Link]();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
[Link](x+" ");
x++;
}
[Link]();
}
break;
case 2:
for(i=6;i>=1;i--)
{
for(j=1;j<=i;j++)
{
[Link](i+" ");
}
[Link]();
}
break;
default:
[Link]("Invalid choice");
}
}
}
Question No. : 5
Write a program to input a string as a sentence find and print the following
(a) Number of letters bot upper and Lower case
(b) Number of digits
(c) Number of special characters
(d) Number of vowels and consonants
import [Link].*;
class Count
{
public static void main (String args[])
{
Scanner in =new Scanner ([Link]);
String s;
int i,u=0,l=0,v=0,sp=0,d=0,c=0;
char ch;
[Link]("Enter String");
s=[Link]();
for(i=0;i<[Link]();i++)
{
ch=[Link](i);
if ((ch>='A'&& ch<='Z') || (ch>='a' && ch<='z'))
{
if (ch>='A' && ch<='Z')
u++;
if (ch>='a' && ch<='z')
l++;
if (ch=='A' ||ch=='E' || ch=='I' || ch=='O' || ch=='U' || ch=='a' ||ch=='e' || ch=='i' ||
ch=='o' || ch=='u')
v++;
else
c++;
}
else if (ch>='0' && ch<='9')
d++;
else
sp++;
}
[Link](" No. of Upper Case Letters "+ u);
[Link]("No. of Lower Case Letters "+ l);
[Link]("No. of Vowels "+ v);
[Link]("No. of Consonants "+ c);
[Link]("No. of Digits "+ d);
[Link]("No. of Special Characters "+ sp);
}
}
Question No. : 6
Write a program to input a square matrix of 4X4 and find and print sum of rows and sum of
columns separately
import [Link].*;
class Squarematrix
{
public static void main (String args[])
{
Scanner in =new Scanner ([Link]);
int a[][]=new int [4][4];
int i,j,s=0;
[Link]("Enter matrix elements");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
a[i][j]=[Link]();
}
}
[Link](" Sum of Each Rows");
for(i=0;i<4;i++)
{
s=0;
for(j=0;j<4;j++)
{
s=s+a[i][j];
}
[Link]("Row " + (i+1)+ " is "+ s);
}
[Link](" Sum of Each Columns");
for(i=0;i<4;i++)
{
s=0;
for(j=0;j<4;j++)
{
s=s+a[j][i];
}
[Link]("Column " + (i+1)+ " is "+ s);
}
}
}
Question No. : 7
Write a program to input a square matrix of 4X4 and find and print sum of DIAGONAL and
product of Diagonal elements separately.
import [Link].*;
class SumProduct
{
public static void main (String args[])
{
Scanner in =new Scanner ([Link]);
int a[][]=new int [4][4];
int i,j,s1=0,s2=0,p1=1,p2=1;
[Link]("Enter matrix elements");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
a[i][j]=[Link]();
}
}
j=3;
[Link](" Sum of Diagonal");
for(i=0;i<4;i++)
{
s1=s1+a[i][i];
s2=s2+a[i][j];
p1=p1*a[i][i];
p2=p2*a[i][j];
j--;
}
[Link](" Sum of Diagonal 1 is " + s1);
[Link](" Sum of Diagonal 2 is " + s2);
[Link](" Product of Diagonal 1 is " + p1);
[Link](" Product of Diagonal 2 is " + p2);
}
}
Question No. : 8
Write a program to initialize the members of our Student Council along with their
post in the Student Council in two different arrays. Search for a name of the
student input bu the user. If found, display name of the student along with his/her
post in the student council. Otherwise display “Sorry Not Found”.
Members : Amartaya Prabhy, Aadhya Uday, Lipyashree N, Sampriti Chaudhury,
Deepak V, Bhumika J, Aditi Yaragunti.
Posts: Head Boy, Head Girl, Sports Captain, CCA Caption, Band Captain, Iris
House Captain, Tulip House Captain
Sample Input: Lipyashree N
Output: Lipyashree N – Sports Captain
Solution:
//Student Council
import [Link].*;
public class Student
{
public static void main(String arg[])
{
String m[]={"Amartaya Prabhy", "Aadhya Uday", "Lipyashree N", "Sampriti
Chaudhury", "Deepak V", "Bhumika J","Aditi Yaragunti"};
String p[]={"Head Boy", "Head Girl", "Sports Captain", "CCA Caption|", "Band
Captain", "Iris House Captain", "Tulip House Captain"};
Scanner in = new Scanner([Link]);
String name;
int i,k=0;
[Link]("Enter the name");
name=[Link]();
for(i=0;i<[Link]; i++)
{
if (m[i].compareTo(name)==0)
{
k=1;
break;
}
}
if (k==1)
[Link](m[i]+" "+p[i]);
else
[Link]("Sorry Not Found");
}
}
Enter the name
Lipyashree N
Lipyashree N Sports Captain
Question No. : 9
Define a class to accept a string and print the characters with the uppercase and
lowercase reversed. But all the other characters should remain the same as before.
Example:
Input : WelCoMe_2022
Output: wELcOmE_2022
//To print the characters
import [Link].*;
public class Welcome
{
public static void main (String args[])
{
Scanner in = new Scanner([Link]);
String str1, str2="";
char ch;
int i,n;
[Link]("Enter string");
str1=[Link]();
n=[Link]();
for(i=0;i<n;i++)
{
ch=[Link](i);
if ([Link](ch))
str2=str2+([Link](ch));
else if ([Link](ch))
str2=str2+([Link](ch));
else
str2=str2+ch;
}
[Link]("Input String is "+str1);
[Link]("Output String is "+str2);
}
}
Enter string
WelCoMe_2022
Input String is WelCoMe_2022
Output String is wELcOmE_2022
Question No. : 10
Define a class to accept a number and check whether it is a SUPERSPY number or
not. A number is called SUPERSPY if the sum of the digits equals the number of
the digits.
Example1:
Input: 1021
output: SUPERSPY number [SUM OF THE DIGITS = 1+0+2+1 = 4,
NUMBER OF DIGITS = 4 ]
Example2:
Input: 125
output: Not an SUPERSPY number [1+2+5 is not equal to 3]
import [Link];
public class KboatSuperSpy
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int sum = 0;
int dc = 0;
int orgNum = num;
while (num > 0)
{
int d = num % 10;
sum += d;
dc++;
num /= 10;
}
if (sum == dc) {
[Link](orgNum + " is a SUPERSPY number");
}
else {
[Link](orgNum + " is not a SUPERSPY number");
}
}
}
Question No. : 11
Define a class to accept a number from user and check if it is an Even palindrome
number or not.
(The number is said to be Even palindrome number when number is palindrome
number (a number is palindrome if it is equal to its reverse) and sum of its digits is
an even number.)
Example: 121 – is a palindrome number
Sum of the digits – 1+2+1 = 4 which is an even number
import [Link];
public class KboatEvenPal
{
public static void main(String[] args)
{
Scanner in = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int org = num;
int rev = 0;
int sum = 0;
while (num > 0)
{
int d = num % 10;
rev = rev * 10 + d;
sum += d;
num /= 10;
}
if (org== rev && sum % 2 == 0)
{
[Link](org + " is an EvenPal number.");
} else {
[Link](org + " is not an EvenPal number.");
}
}
}
Question No. : 12
DIAGONAL array or not. An array is DIAGONAL if the sum of the left diagonal
elements equals the sum of the right diagonal elements. Print the appropriate
message.
Example:
3425
2523
5327
1371
Sum of the left diagonal element = 3 + 5 + 2 + 1 = 11
Sum of the right diagonal element = 5 + 2 + 3 + 1 = 11
import [Link];
public class KboatDiagonalDDA
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int[][] arr = new int[4][4];
[Link]("Enter elements for 4x4 DDA:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
arr[i][j] = [Link]();
}
}
int lSum = 0;
int rSum = 0;
for (int i = 0; i < 4; i++) {
lSum += arr[i][i];
rSum += arr[i][3 - i];
}
if (lSum == rSum) {
[Link]("The array is a DIAGONAL array.");
} else {
[Link]("The array is NOT a DIAGONAL array.");
} }}
Question No. : 13
Define a class pin code and store the given pin codes in a single dimensional array.
Sort these pin codes in ascending order using the Selection Sort technique only.
Display the sorted array.
110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033
public class Pincode
{
public static void main(String args[])
{
int[] arr = {110061, 110001,
110029, 110023,
110055, 110006,
110019, 110033};
for (int i = 0; i < 7; i++)
{
int k = i;
for (int j = i + 1; j < 8; j++)
{
if (arr[j] < arr[k])
k = j;
}
int t = arr[i];
arr[i] = arr[k];
arr[k] = t;
}
[Link]("Sorted Array:");
for (int i = 0; i < 8; i++)
{
[Link](arr[i] + " ");
}
}
}
Question No. : 14
Define a class to accept a string and convert it into uppercase. Count and display
the number of vowels in it.
Input: robotics
Output: ROBOTICS
Number of vowels: 3
import [Link];
public class CountVowels
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
[Link]("Enter the string: ");
String str = [Link]();
str = [Link]();
str += " ";
int count = 0;
int len = [Link]();
for (int i = 0; i < len - 1; i++)
{
char ch = [Link](i);
if(ch == 'A'
|| ch == 'E'
|| ch == 'I'
|| ch == 'O'
|| ch == 'U')
count++;
}
[Link]("String : " + str);
[Link]("Number of vowels : " + count);
}
}
Question No. : 15
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:
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
import [Link];
public class Sparray
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int arr[][] = new int[3][3];
long evenSum = 0, oddSum = 0;
[Link]("Enter the elements of 3 x 3 DDA: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = [Link]();
} }
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (arr[i][j] % 2 == 0)
evenSum += arr[i][j];
else
oddSum += arr[i][j];
}
}
[Link]("Sum of even elements = " + evenSum);
[Link]("Sum of odd elements = " + oddSum);
if (evenSum == oddSum)
[Link]("Special Array");
else
[Link]("Not a Special Array");
}}