0% found this document useful (0 votes)
25 views49 pages

Board Proj

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)
25 views49 pages

Board Proj

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

COMPUTER

APPLICATION
PROJECT

SHLOK JOSHI [XC]

1
ACKNOWLDEDGEMEM
NT

I would like to express my special


thanks of gratitude to my teacher
Ms. Shilpa as well as our principal
Dr. (Mrs.)Florence D’Souza who
gave me the golden opportunity to
do this wonderful project, which
also helped me in doing a lot of
Research and I came to know about
so many new things I am really
thankful to them.
Secondly, I would also like to
thank
my parents and friends who
helped me a lot in finalizing this
project within the limited time
frame.

2
Index
Sno. Topic Page no.
name

3
INTRODUCTION
TO JAVA
Overview
Java is a class-based object-oriented simple programming
language. Though we can not consider it to be fully object-
oriented as it supports primitive datatypes. It is a general-
purpose, high-level programming language that helps
programmers and developers to write a code once and run it
anywhere. Java is considered both
a compiled and interpreted language. It is because Java
source code is first compiled to bytecode which is then
interpreted by Java Virtual Machine. Java Virtual Machine
interprets the bytecode and converts it to platform specific
machine code. Hence, Java is also called a platform-
independent programming language.

What is Java Programming?


Java is a programming language that James Gosling developed
at Sun Microsystems_Inc in the year 1995, which later on was
taken into possession by the Oracle Corporation in 2009.

 We can call it a high-level (makes the development of


programs easy and much more user-friendly)
programming language which makes it very convenient
for us to write, compile and debug Java programs.
 Java is a class-based object-oriented programming
language that implements the principle of write once code
anywhere.
 Since Java applications are compiled to byte-code, they
can run on any JVM-supported machine.
 Java codes are very similar to C/C++, which makes them
easier to understand.

4
Why Java Programming Language is Called
JAVA?
Java is the name of a beautiful island in Indonesia. It is also
said that the first coffee was named right here after java
coffee. The name was chosen by James Gosling during the
daytime when he was enjoying a cup of coffee near his office.
Java was initially called by the name: OAK.

However, in the wake of the Oak Technologies, the team had


officially decided to rename it. The options they considered
were Silk, Revolutionary, Dynamic, DNA, and Jolt. Even
though Silk was further selected, they decided to go along with
Java as it was unique, and a lot of people preferred it.

Why Use Java?


As mentioned above, Java is a high-level language and should
be studied if you want to know the basics of programming. Let
us learn why there is a need to learn the language.

1) Object-Oriented

2) Portable

3) Simple

4) Secure

5) Robust

6) Platform Independent

7) Architecture neutral

5
PROGRAM
S
Question 1
Write a program to input a three digit number. Use a method
int Armstrong(int n) to accept the number. The method
returns 1, if the number is Armstrong, otherwise zero(0).

Sample Output: 153 ⇒ 13 + 53 + 33 = 153


Sample Input: 153

It is an Armstrong Number.

ANS:-
import java.util.Scanner;
public class ArmstrongNumber
{
public static void main(String[] args) {

int n,arm=0,rem,c;
Scanner in=new Scanner(System.in);
System.out.println("Enter any Number");
n=in.nextInt();
c=n;
while (n> 0) {
rem= n % 10;
arm = (rem*rem*rem)+arm;
n=n/= 10;
}

if (c==arm)
System.out.println("Armstrong Number");
else
System.out.println("Not Armstrong Number");

6
}

Output
Enter Number: 153
153 is an Armstrong number
Enter Number: 747
747 is not an Armstrong number

VARIABLE DESCRIPTION
Variable Dataype Descripti
on
n int To store the value
of the user input.
arm int To store and
compute the value
of Armstrong
number.
rem int To store the
remainder value.

Question 2

7
Write a class with the name Perimeter using method
overloading that computes the perimeter of a square, a
rectangle and a circle.
Formula: Perimeter of a square = 4 * s
Perimeter of a rectangle = 2 * (l + b)
Perimeter of a circle = 2 * (22/7) * r

Ans:-
import java.util.Scanner;

public class Perimeter


{
public double perimeter(double s) {
double p = 4 * s;
return p;
}

public double perimeter(double l, double b) {


double p = 2 * (l + b);
return p;
}

public double perimeter(int c, double pi, double r) {


double p = c * pi * r;
return p;
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
Perimeter obj = new Perimeter();

System.out.print("Enter side of square: ");


double side = in.nextDouble();
System.out.println("Perimeter of square = " +
obj.perimeter(side));

System.out.print("Enter length of rectangle: ");


double l = in.nextDouble();
System.out.print("Enter breadth of rectangle: ");
double b = in.nextDouble();
System.out.println("Perimeter of rectangle = " +
obj.perimeter(l, b));

System.out.print("Enter radius of circle: ");

8
double r = in.nextDouble();
System.out.println("Perimeter of circle = " +
obj.perimeter(2, 3.14159, r));
}
}

Output
Enter side of square: 3
Perimeter of square = 12.0
Enter length of rectangle: 10
Enter breadth of rectangle: 7
Perimeter of rectangle = 34.0
Enter radius of circle: 6
Perimeter of circle = 37.699079999999995

VARIABLE DESCRIPTION
Variable Dataype Descripti
on
p double To store and compute
perimeters.
r double To store the value of
radius of circle.

l double To store the value of


length of rectangle.

b double To store the value of


breadth of rectangle.
side double To store the value of
side of square.

9
Question 3

Define a class Telephone having the following description:

Class name : Telephone

Data Members Purpose

int prv, pre to store the previous and present meter readings

int call to store the calls made (i.e. pre - prv)

String name to store name of the consumer

double amt to store the amount

double total to store the total amount to be paid

Member
Purpose
functions

Stores the previous reading, present reading and name


void input()
of the consumer

void cal() Calculates the amount and total amount to be paid

Displays the name of the consumer, calls made, amount and total
void
display()
amount to be paid

Write a program to compute the monthly bill to be paid according to the given
conditions and display the output as per the given format.

Calls made Rate

Up to 100 calls No charge

For the next 100 calls 90 paise per call

For the next 200 calls 80 paise per call

More than 400 calls 70 paise per call

10
Calls made Rate

However, every consumer has to pay ₹180 per month as monthly rent for availing the
service.

Output:

Name of the customer Calls made Amount to be paid


.................... .......... .................
.................... .......... .................

Ans:-
import java.util.Scanner;
public class Telephone
{
private int prv;
private int pre;
private int call;
private String name;private double amt;
private double total;
public void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Customer Name: ");
name = in.nextLine();
System.out.print("Enter previous reading: ");
prv = in.nextInt();
System.out.print("Enter present reading: ");
pre = in.nextInt();
}
public void cal() {
call = pre - prv;
if (call <= 100)
amt = 0;

11
else if (call <= 200)
amt = (call - 100) * 0.9;
else if (call <= 400)
amt = (100 * 0.9) + (call - 200) * 0.8;
else
amt = (100 * 0.9) + (200 * 0.8) + ((call - 400) * 0.7);

total = amt + 180;


}
public void display() {
System.out.println("Name of the customer\tCalls made\tAmount
to be paid");
System.out.println(name + "\t" + call + "\t" + total);
}
public static void main(String args[]) {
Telephone obj = new Telephone();
obj.input();
obj.cal();
obj.display();
}
}

Output

Enter Customer Name: Aditya


Enter previous reading: 2
Enter present reading: 10
Name of the customer Calls made Amount to be paid
Aditya 8 180.0

12
VARIABLE DESCRIPTION
Variab Datay Description
le pe
prv int to store the previous
and present meter
readings
pre int to store the previous
and present meter
readings
call int To store the call made.

name String to store name of the


consumer

amt double To store the amount

total double To store the total to be


paid.

Question 4
Bank charges interest for the vehicle loan as given below:

Number of years Rate of interest

Up to 5 years 15%

More than 5 and up to 10 years 12%

Above 10 years 10%

13
Number of years Rate of interest

Write a program to model a class with the specifications given below:

Class name: Loan

Data Members Purpose

int time Time for which loan is sanctioned

double principal Amount sanctioned

double rate Rate of interest

double interest To store the interest

double amt Amount to pay after given time

Member Methods Purpose

void getdata() To accept principal and time

To find interest and amount.


void calculate() Interest = (Principal*Rate*Time)/100
Amount = Principal + Interest

void display() To display interest and amount

Ans:-
import java.util.Scanner;

public class Loan


{
private int time;
private double principal;
private double rate;
private double interest;
private double amt;

14
public void getdata() {
Scanner in = new Scanner(System.in);
System.out.print("Enter principal: ");
principal = in.nextInt();
System.out.print("Enter time: ");
time = in.nextInt();
}

public void calculate() {


if (time <= 5)
rate = 15.0;
else if (time <= 10)
rate = 12.0;
else
rate = 10.0;

interest = (principal * rate * time) / 100.0;


amt = principal + interest;
}

public void display() {


System.out.println("Interest = " + interest);
System.out.println("Amount Payable = " + amt);
}

public static void main(String args[]) {


Loan obj = new Loan();
obj.getdata();
obj.calculate();
obj.display();
}
}

Output
Enter principal: 4000
Enter time: 11
Interest = 4400.0
Amount Payable = 8400.0

15
VARIABLE DESCRIPTION
Variab Datay Description
le pe
p int
to store the principal
amount.

r float
To calculate the rate of
interest.

t int To store the period time.

interest double To the value of


interest.

amt double To store the total


amount.

Question 5
Define a class Bill that calculates the telephone bill of a
consumer with the following description:

16
Data
Purpose
Members

int bno bill number

String name name of consumer

int call no. of calls consumed in a month

double amt bill amount to be paid by the person

Member
Purpose
Methods

constructor to initialize data members


Bill()
with default initial value

parameterised constructor to accept


Bill(...)
billno, name and no. of calls consumed

to calculate the monthly telephone bill for


Calculate()
a consumer as per the table given below

Display() to display the details

17
Units consumed Rate

First 100 calls ₹0.60 / call

Next 100 calls ₹0.80 / call

Next 100 calls ₹1.20 / call

Above 300 calls ₹1.50 / call

Fixed monthly rental applicable to all consumers: ₹125

Create an object in the main() method and invoke the above


functions to perform the desired task

Ans:-
import java.util.Scanner;

public class Bill


{
private int bno;
private String name;
private int call;
private double amt;

public Bill() {
bno = 0;
name = "";
call = 0;
amt = 0.0;
}

public Bill(int bno, String name, int call) {


this.bno = bno;
this.name = name;
this.call = call;
}

public void calculate() {


double charge;
if (call <= 100)
charge = call * 0.6;
else if (call <= 200)

18
charge = 60 + ((call - 100) * 0.8);
else if (call <= 300)
charge = 60 + 80 + ((call - 200) * 1.2);
else
charge = 60 + 80 + 120 + ((call - 300) * 1.5);

amt = charge + 125;


}

public void display() {


System.out.println("Bill No: " + bno);
System.out.println("Name: " + name);
System.out.println("Calls: " + call);
System.out.println("Amount Payable: " + amt);
}

public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter Name: ");
String custName = in.nextLine();
System.out.print("Enter Bill Number: ");
int billNum = in.nextInt();
System.out.print("Enter Calls: ");
int numCalls = in.nextInt();

Bill obj = new Bill(billNum, custName, numCalls);


obj.calculate();
obj.display();
}
}

Output
Enter Name: Aditya
Enter Bill Number: 40302010
Enter Calls: 23
Bill No: 40302010
Name: Aditya
Calls: 23
Amount Payable: 138.8

19
VARIABLE DESCRIPTION
Variab Datay Description
le pe
bno int
to store the bill number.

call int To store number of call


consumed in a month.
name String to store name of the
consumer

amt double To store the bill amount


to be paid by the person.
numCalls int To store the number of
calls made by the
person.

Question 6
Write a program by using a class with the following
specifications:

Class name — Factorial


Data members — private int n

20
Member functions:

1. void input() — to input a number


2. void fact() — to find and print the factorial of the
number

Use a main function to create an object and call


member methods of the class.

Ans:-
import java.util.Scanner;

public class Factorial


{
private int n;

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
n = in.nextInt();
}

public void fact() {


int f = 1;
for (int i = 1; i <= n; i++)
f *= i;
System.out.println("Factorial of " + n
+ " = " + f);
}

public static void main(String args[]) {


Factorial obj = new Factorial();
obj.input();
obj.fact();
}
}

Output
Enter the number: 5
Factorial of 5 = 120

21
VARIABLE DESCRIPTION
Variab Datay Description
le pe
n int
To store the value of
user input.

f int To store the value


factorial.
i int To calculate the value of
factorial.

Question 7
A bank announces new rates for Term Deposit Schemes for their
customers and Senior Citizens as given below:

Rate of
Rate of
Interest
Term Interest
(Senior
(General)
Citizen)

Up to 1
7.5% 8.0%
year

22
Rate of
Rate of
Interest
Term Interest
(Senior
(General)
Citizen)

Up to 2
8.5% 9.0%
years

Up to 3
9.5% 10.0%
years

More
than 3 10.0% 11.0%
years

The 'senior citizen' rates are applicable to the customers whose age is 60
years or more. Write a program to accept the sum (p) in term deposit
scheme, age of the customer and the term. The program displays the
information in the following format:

Amount Intere Amou


Ter Ag
Deposite st nt
m e
d earned Paid

xxx xxx xxx xxx xxx

Ans:-
import java.util.Scanner;

public class BankDeposit


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter sum: ");


double sum = in.nextDouble();

System.out.print("Enter age: ");


int age = in.nextInt();

System.out.print("Enter term: ");


double term = in.nextDouble();

23
double si = 0.0;

if (term <= 1 && age < 60)


si = (sum * 7.5 * term) / 100;
else if (term <= 1 && age >= 60)
si = (sum * 8.0 * term) / 100;
else if (term <= 2 && age < 60)
si = (sum * 8.5 * term) / 100;
else if (term <= 2 && age >= 60)
si = (sum * 9.0 * term) / 100;
else if (term <= 3 && age < 60)
si = (sum * 9.5 * term) / 100;
else if (term <= 3 && age >= 60)
si = (sum * 10.0 * term) / 100;
else if (term > 3 && age < 60)
si = (sum * 10.0 * term) / 100;
else if (term > 3 && age >= 60)
si = (sum * 11.0 * term) / 100;

double amt = sum + si;

System.out.println("Amount Deposited: " + sum);


System.out.println("Term: " + term);
System.out.println("Age: " + age);
System.out.println("Interest Earned: " + si);
System.out.println("Amount Paid: " + amt);
}
}

Output
Enter sum: 20000
Enter age: 19
Enter term: 3
Amount Deposited: 20000.0
Term: 3.0
Age: 19
Interest Earned: 5700.0
Amount Paid: 25700.0

24
VARIABLE DESCRIPTION
Variab Datay Description
le pe
sum double
To store the value of
sum.

term double To store the value of


term.
si double To calculate and store
the value of simple
interest.
age int To store the age of the
user.
amount double To calculate and store
the total amount paid.

Question 8
Write a program using switch case to find the volume of a
cube, a sphere and a cuboid.
For an incorrect choice, an appropriate error message
should be displayed.

1. Volume of a cube = s * s *s
2. Volume of a sphere = (4/3) * π * r * r * r (π = (22/7))
3. Volume of a cuboid = l*b*h

Ans:-
import java.util.Scanner;

public class MenuVolume

25
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.println("1. Volume of Cube");


System.out.println("2. Volume of Sphere");
System.out.println("3. Volume of Cuboid");
System.out.print("Enter your choice: ");
int choice = in.nextInt();

switch(choice) {
case 1:
System.out.print("Enter side of cube: ");
double cs = in.nextDouble();
double cv = Math.pow(cs, 3);
System.out.println("Volume of cube = " + cv);
break;

case 2:
System.out.print("Enter radius of sphere: ");
double r = in.nextDouble();
double sa = (4 / 3.0) * (22 / 7.0) *
Math.pow(r, 3);
System.out.println("Volume of sphere = " +
sa);
break;

case 3:
System.out.print("Enter length of cuboid: ");
double l = in.nextDouble();
System.out.print("Enter breadth of cuboid:
");
double b = in.nextDouble();
System.out.print("Enter height of cuboid: ");
double h = in.nextDouble();
double vol = l * b * h;
System.out.println("Volume of cuboid = " +
vol);
break;

default:
System.out.println("Wrong choice! Please
select from 1 or 2 or 3.");
}
}
}

Output

26
1. Volume of Cube
2. Volume of Sphere
3. Volume of Cuboid
Enter your choice: 1
Enter side of cube:
33
Volume of cube = 35937.0

VARIABLE DESCRIPTION
Variable Dataype Descripti
on
cs double To store the value of
sides of cube.
cv double To calculate and print
the volume of cube.

choice int To input the choice of


user.

r double To store the of


radiuds of sphere.
l double To store the value of
length of cuboid.
b double To store the value of
breadth of cuboid.
h double To store the value of
height of cuboid.
vol double To calculate and store
the volume of cuboid.

27
Question 9
A special two-digit number is such that when the sum of its digits is added to
the product of its digits, the result is equal to the original two-digit number.

Example: Consider the number 59.


Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Sum of the sum of digits and product of digits = 14 + 45 = 59

Write a program to accept a two-digit number. Add the sum of its digits to the
product of its digits. If the value is equal to the number input, then display the
message "Special two—digit number" otherwise, display the message "Not a
special two-digit number".

Ans:-
import java.util.Scanner;

public class SpecialNumber


{
public void checkNumber() {

Scanner in = new Scanner(System.in);

System.out.print("Enter a 2 digit number: ");


int orgNum = in.nextInt();

int num = orgNum;


int count = 0, digitSum = 0, digitProduct = 1;

while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
digitProduct *= digit;
count++;
}

if (count != 2)
System.out.println("Invalid input, please enter a 2-
digitnumber");
else if ((digitSum + digitProduct) == orgNum)
System.out.println("Special 2-digit number");
else
System.out.println("Not a special 2-digit
number");}
}

Output
Enter a 2 digit number: 59

28
Special 2-digit number
Enter a 2 digit number: 85
Not a Special 2-digit number
Enter a 2 digit number: 4
Invalid input, please enter a 2-digit number
Enter a 2 digit number: 512
Invalid input, please enter a 2-digit number

VARIABLE DESCRIPTION
Variable Dataype Descripti
on
orgNum int To store the value of 2
digit number.
digitSum int To store and calculate
the sum of digits.
digitProduct int To store and calculate
the product of digits.
digit double To store the value of
digitProduct.
num int To store and compute
the value of orgNum.

Question 10
Write a menu driven program to perform the following tasks:

(a) Tribonacci numbers are a sequence of numbers similar to Fibonacci numbers,


except that a number is formed by adding the three previous numbers. Write a
program to display the first twenty Tribonacci numbers.
For example;
1, 1, 2, 4, 7, 13, ......................

29
(b) Write a program to display all Sunny numbers in the range from 1 to n.
[Hint: A number is said to be sunny number if square root of (n+1) is an integer.]
For example,
3, 8, 15, 24, 35,......................are sunny numbers.

For an incorrect option, an appropriate error message should be displayed.

Ans:-
import java.util.Scanner;

public class KTribNSunNos


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Tribonacci numbers");
System.out.println("2. Sunny numbers");
System.out.print("Enter your choice: ");
int ch = in.nextInt();

switch (ch) {
case 1:
int a = 1, b = 1, c = 2;

System.out.print(a + " " + b + " " + c);

for (int i = 4; i <= 20; i++) {


int term = a + b + c;
System.out.print(" " + term);
a = b;
b = c;
c = term;
}
break;

case 2:
System.out.print("Enter n: ");
int n = in.nextInt();

double sqRoot, temp;

for (int i = 3; i <= n; i++) {


sqRoot = Math.sqrt(i + 1);
temp = sqRoot - Math.floor(sqRoot);
if (temp == 0)
System.out.print(i + " ");
}
break;

default:

30
System.out.println("Incorrect choice");
break;
}
}
}

Output
Output(i)

1. Tribonacci numbers
2. Sunny numbers
Enter your choice: 1
1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 5768 10609 19513
35890 66012

Output(ii)

1. Tribonacci numbers
2. Sunny numbers
Enter your choice: 2
Enter n: 50
3 8 15 24 35 48

VARIABLE DESCRIPTION
Variable Dataype Descripti
on
ch int To accept user’s
choice
a int To calculate the value
of Tribonacci number.
b int To calculate the value
of Tribonacci number.
c int To calculate the value
of Tribonacci number.
term int To store the sum of
a,b and c
n int To store the value of
number of days.
sqRoot double To calculate square
root and store its
value.
temp double To store the value of
temperature.

31
Question 11

Write the programs to display the following pattern:

15 14 13 12 11
10 9 8 7
6 5 4
3 2
1

Ans:-
public class Pattern
{
public void displayPattern() {
int a = 15;
for (int i = 5; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(a-- + "\t");
}
System.out.println();
}
}
}

Output
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1

VARIABLE DESCRIPTION
Variable Dataype Description
a int To store the total
number of character.
i int Loop variable
j int Loop variable

32
Question 12
Write the programs to display the following pattern:

1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5

Ans:-
public class Patterns
{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {

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


System.out.print(i + " ");

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


System.out.print(k + " ");

System.out.println();
}
}
}

Output

12 3 4 5
2 2 3 4 5
3 3 3 4 5

33
4 4 4 4 5
5 5 5 5 5

VARIABLE DESCRIPTION
Variable Dataype Description
k int Loop variable
i int Loop variable
j int Loop variable

Question 13
Write the programs to display the following pattern:

*
* #
* #*
* #*#
* #*#*
Ans:-
public class Pattern_s
{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
System.out.print("# ");
else
System.out.print("* ");
}
System.out.println();
}
}
}

34
Output
*
* #
* #*
* #*#
* #*#*

VARIABLE DESCRIPTION
Variable Dataype Description
i int Outer Loop variable
j int Inner Loop variable

Question 14
Write a program to store 20 numbers in a Single
Dimensional Array (SDA). Now, display only those numbers
that are perfect squares. n[0] n[1] n[2] n[3] n[4] n[5] ...
n[16] n[17] n[18] n[19] 12 45 49 78 64 77 ... 81 99 45 33
Sample Output: 49, 64, 81

Ans:-
import java.util.Scanner;

public class SDABubbleSort


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter 20 numbers:");

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


arr[i] = in.nextInt();
}

for (int i = 0; i < arr.length / 2 - 1; i++) {

35
for (int j = 0; j < arr.length / 2 - i - 1; j++)
{
if (arr[j] > arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

for (int i = 0; i < arr.length / 2 - 1; i++) {


for (int j = arr.length / 2; j < arr.length - i -
1; j++) {
if (arr[j] < arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

System.out.println("\nSorted Array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

Output
Enter 20 numbers:
1
2
3
4
5
6
7
6
5
4
69
420
540
32

36
20203
3024
2024
32
43
3

Sorted Array:
1 2 3 4 4 5 5 6 6 7 20203 3024 2024 540 420 69 43 32 32 3

VARIABLE DESCRIPTION
Variable Dataype Description
i int To accept and store the
value 20 integers, in
iterative constructs and
single dimensional array
j int To be used in iterative
constructs , conditional
statements and single
dimensional array

Question 15
If arrays M and M + N are as shown below, write a program
in Java to find the array N.
M = {{-1, 0, 2}, M + N = {{-6, 9, 4},
{-3, -1, 6}, {4, 5, 0},
{4, 3, -1}} {1, -2, -3}}
Ans:-
public class SubtractDDA
{
public static void main(String args[]) {

int arrM[][] = {
{-1, 0, 2},
{-3, -1, 6},
{4, 3, -1}
};

int arrSum[][] = {
{-6, 9, 4},
{4, 5, 0},

37
{1, -2, -3}
};

int arrN[][] = new int[3][3];

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
arrN[i][j] = arrSum[i][j] - arrM[i][j];
}
}

System.out.println("Array N:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arrN[i][j]);
System.out.print(' ');
}
System.out.println();
}
}
}

Output
Array N:
-5 9 2
7 6 -6
-3 -5 -2

VARIABLE DESCRIPTION
Variable Dataype Description

arrM int To store the value of Matrix


M
arrN int To store and calculate the
value of Matrix N
arrSum int To store the value of sum
of Matrix M and Matrix N
i int Loop variable
j int Loop variable

38
Question 16
Write a program to input a sentence. Find and display the
following:
(i) Number of words present in the sentence
(ii) Number of letters present in the sentence
Assume that the sentence has neither include any digit nor a
special character.

Ans:-
import java.util.Scanner;

public class WordsNLetters


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();

int wCount = 0, lCount = 0;


int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ')
wCount++;
else
lCount++;
}

wCount++;

System.out.println("No. of words = " + wCount);


System.out.println("No. of letters = " + lCount);
}
}

Output
Enter a sentence:
Happy New Year
No. of letters = 1No. of letters = 12

39
VARIABLE DESCRIPTION
Variable Dataype Description

str String To input a sentence


lcount int To count number of letters
present in the sentence
len int To store the value of
number of characters in
the sentence
i int Loop variable
ch char Checks if character is
whitespace or letter
wcount int To count number of
words present in the
sentence

Question 17
Write a program in Java to store 10 words in a Single
Dimensional Array. Display only those words which are
Palindrome.
Sample Input: MADAM, TEACHER, SCHOOL, ABBA, .........
Sample Output: MADAM
ABBA
..........
..........Enter a word : BLUEJ
Sample Output:
BLUEJ
BLUE
BLU
BL
B

Ans:-
import java.util.Scanner;

public class SDAPalindrome


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String words[] = new String[10];
System.out.println("Enter 10 words:");

40
for (int i = 0; i < words.length; i++) {
words[i] = in.nextLine();
}

System.out.println("\nPalindrome Words:");

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


String str = words[i].toUpperCase();
int strLen = str.length();
boolean isPalin = true;

for (int j = 0; j < strLen / 2; j++) {


if (str.charAt(j) != str.charAt(strLen - 1 -
j)) {
isPalin = false;
break;
}
}

if (isPalin)
System.out.println(words[i]);

}
}
}

Output
Enter 10 words:
MADAM
TEACHER
SCHOOL
ABBA
ENGLISH
HINDI
GEOGRAPHY
MATHEMATICS
COMPUTER
LAPTOP

Palindrome Words:

41
MADAM
ABBA

VARIABLE DESCRIPTION
Variable Dataype Description

str int To convert the sentence in


uppercase and store it.
words int To store words length.
strLen int To convert str to
uppercase.
i int Loop variable
j int Loop variable
isPalin int Checks wether word is a
palinadrome or not

Question 18
Write a program to accept the year of graduation from school as an integer value from
the user. Using the binary search technique on the sorted array of integers given
below, output the message "Record exists" if the value input is located in the array. If
not, output the message "Record does not exist". Sample Input: n[0] n[1] n[2] n[3] n[4]
n[5] n[6] n[7] n[8] n[9] 1982 1987 1993 1996 1999 2003 2006 2007 2009 2010
Ans:-
import java.util.Scanner;

public class GraduationYear


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007,
2009, 2010};

System.out.print("Enter graduation year to search: ");


int year = in.nextInt();

int l = 0, h = n.length - 1, idx = -1;


while (l <= h) {
int m = (l + h) / 2;
if (n[m] == year) {
idx = m;
break;
}
else if (n[m] < year) {
l = m + 1;
}
else {
h = m - 1;

42
}
}

if (idx == -1)
System.out.println("Record does not exist");
else
System.out.println("Record exists");
}
}

Output
Enter graduation year to search: 2000
Record does not exist

VARIABLE DESCRIPTION
Variable Dataype Description

n int To store the value of


graduation year.
year int To input the graduation
year to search
l int To store and evaluate the
length of value n.

h int Loop variable


idx int Loop variable
m int No. of months

Question 19
Write a java program to arrange 20 numbers in descending order using selection sort
Ans:-

import java.util.Scanner;
public class bubble_sort
{
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();

43
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i]=s.nextInt();
}
for (int i = 0; i<n; i++)
{
for(int j=1+1; j<n; j++)
{
if (a[i] < a[j])
{
temp= a[i];
a[i]=a[j];
a[j] = temp;
}
}
}
System.out.print("Descending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}

Output
Enter no. of elements you want in array:5
Enter all the elements:
23
12
34
45
54
Descending Order:54,45,12,23,34

VARIABLE DESCRIPTION
Variable Dataype Description

n int To input number of


variable and store them

a int To store array value.


temp int To store a[j] value.
i int Loop variable
j int Loop variable

44
Question 20
Write a program to accept a list of 20 integers. Sort the first 10 numbers in ascending
order and next the 10 numbers in descending order by using 'Bubble Sort' technique.
Finally, print the complete list of integer
Ans:-
import java.util.Scanner;

public class KboatSDABubbleSort


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter 20 numbers:");

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


arr[i] = in.nextInt();
}

//Sort first half in ascending order


for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = 0; j < arr.length / 2 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

//Sort second half in descending order


for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = arr.length / 2; j < arr.length - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

//Print the final sorted array


System.out.println("\nSorted Array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

Output

45
Enter 20 numbers:
1
2
3
4
5
6
7
6
5
4
69
420
540
32
20203
3024
2024
32
43
3

Sorted Array:
1 2 3 4 4 5 5 6 6 7 20203 3024 2024 540 420 69 43 32 32 3

VARIABLE DESCRIPTION
Variable Dataype Description
i int To accept and store the
value 20 integers, in
iterative constructs and
single dimensional array
j int To be used in iterative
constructs , conditional
statements and single
dimensional array

46
Bibliograph
y
 https://www.knowledgeboat.com/
 https://byjus.com/
 https://www.javatpoint.com/
 https://www.geeksforgeeks.org/

47
Conclusion
Java is an object-oriented programming
language. It is a general-purpose
programming language, mainly
designed to run developed Java code on
all platforms that support Java without
recompilation. As we all know, Java is
one of the most popular and indemand
programming languages to learn and it
was one of the first languages to
standardise high-level threading
utilities. Java project is a must for
aspiring developers. This project helps
developers develop real-world projects
to hone their skills and materialise their
theoretical knowledge into practical
experience. Java has significant

48
advantages both as a commercial
language and as a teaching language.
Java project provides rigorous compile-
time error checking typically associated
with Pascal, allowing instructors to
introduce students to GUI programming,
networking, threads, and other
important concepts used in modern-day
software. Overall, the Java project gives
a complete design for the extended
language.

49

You might also like