0% found this document useful (0 votes)
9 views47 pages

Computer Project

The document contains multiple Java programming tasks, including converting decimal numbers to other bases, determining future dates, checking for Emirp numbers, creating abbreviated names, checking for palindrome numbers, and implementing binary search. Each task includes a class definition, methods, and sample outputs. Additionally, variable descriptions are provided for clarity on data types and purposes.

Uploaded by

joshimanas098
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views47 pages

Computer Project

The document contains multiple Java programming tasks, including converting decimal numbers to other bases, determining future dates, checking for Emirp numbers, creating abbreviated names, checking for palindrome numbers, and implementing binary search. Each task includes a class definition, methods, and sample outputs. Additionally, variable descriptions are provided for clarity on data types and purposes.

Uploaded by

joshimanas098
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

QUESTION. 01

Write a program to convert a decimal number to other base and


vice versa according to user's choice.
A.​Decimal to Others
[Link] to Binary
[Link] to Octal
[Link] to Hexadecimal

B. Other to Decimal
[Link] to Decimal
[Link] to Decimal
[Link] to Decimal
2

import [Link].*;
class Conversion
{
public static void main ( )
{
Scanner sc = new Scanner([Link]); [Link]("Conversion
Menu");
[Link]("1. Decimal to Binary");
[Link]("2. Decimal to Octal");
[Link]("3. Decimal to Hexadecimal");
[Link]("4. Binary to Decimal");
[Link]("5. Octal to Decimal");
[Link]("6. Hexadecimal to Decimal");
[Link]("Enter your choice: ");
int choice = [Link]();
switch (choice)
{
case 1:
[Link]("Enter Decimal number: ");
int dec1 = [Link]();
[Link]("Binary: " + [Link](dec1));
break;
case 2:
[Link]("Enter Decimal number: ");
int dec2 = [Link]();
[Link]("Octal: " + [Link](dec2));
break;
case 3:
[Link]("Enter Decimal number: ");
int dec3 = [Link]();
[Link]("Hexadecimal: " + [Link](dec3));
break;
case 4:
[Link]("Enter Binary number: ");
3

String bin = [Link]();


[Link]("Decimal: " + [Link](bin, 2));
break;
case 5:
[Link]("Enter Octal number: ");
String oct = [Link]();
[Link]("Decimal: " + [Link](oct, 8));
break;
case 6:
[Link]("Enter Hexadecimal number: ");
String hex = [Link]() ;
[Link]("Decimal: " + [Link](hex, 16));
break;
default:
[Link]("Invalid Choice!");
break;
}
}
}
4

OUTPUT :

Conversion Menu
1. Decimal to Binary
2. Decimal to Octal
3. Decimal to Hexadecimal
4. Binary to Decimal
5. Octal to Decimal
6. Hexadecimal to Decimal
Enter your choice: 6
Enter Hexadecimal number: 1a
Decimal: 26
5

VARIABLE DESCRIPTION TABLE :

VARIABLE NAME DATA TYPE DESCRIPTION


num int Stores the input
number.
choice int Stores the user's
choice.
binary String Stores the binary
equivalent of the
number.
octal String Stores the octal
equivalent of the
number.
hex String Stores the
hexadecimal
equivalent of the
number.
6

QUESTION. 02

Design a class Ddate that takes a future date a (any date after
01-01-2023) and prints what day it will be (given the day of
01-01-2023)
class name : Date
Data Members :
String s : to store the future date in DD-MM-YYYY format
String f : to store the day of 01-01-2023
Member Functions :
Ddate : constructor to initialize data members to null
void accept () : to accept values of s and f
void find_day () : to find and display the day of the future date
7

import [Link].*;
class Ddate
{
String s;
String f;
Ddate( )
{
s = "";
f = "";
}
void accept( )
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a future date (DD-MM-YYYY): ");
s = [Link]();
[Link]("Enter the day of 01-01-2023 (e.g., Sunday): ");
f = [Link]();
}
void find_day( )
{
String days[ ] =
{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Satur
day"};
int monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int d = [Link]([Link](0,2));
int m = [Link]([Link](3,5));
int y = [Link]([Link](6));
int totalDays = 0;
for(int i=2023; i<y; i++)
{
if((i%4==0 && i%100!=0) || (i%400==0))
totalDays += 366;
else
totalDays += 365;
}
if((y%4==0 && y%100!=0) || (y%400==0))
monthDays[1] = 29;
8

for(int i=0; i<m-1; i++)


{
totalDays += monthDays[ i ];
}
totalDays += d-1;
int startIndex = -1;
for(int i=0; i<7; i++)
{
if(days[i].equalsIgnoreCase(f))
{
startIndex = i;
break;
}
}

int futureDayIndex = (startIndex + totalDays) % 7;


[Link]("The day on " + s + " will be: " +
days[futureDayIndex]);
}
public static void main( )
{
Ddate obj = new Ddate( );
[Link]( );
obj.find_day( );
}
}
9

OUTPUT :

Enter a future date (DD-MM-YYYY): 29-08-2025


Enter the day of 01-01-2023 (e.g., Sunday): Sunday
The day on 29-08-2025 will be: Friday
10

VARIABLE DESCRIPTION TABLE :

VARIABLE NAME DATA TYPE DESCRIPTION


s String Stores the future date.

f String Stores the day of


01-01-2023.
days String Stores names of the
days of the week.
monthDays int Stores number of
days in each month.
d int Day part extracted
from the input date.
m int Month part extracted
from the input date.
y int Year part extracted
from the input date.
totalDays int Count total number of
days passed.
i int Loop counter.
startIndex int Stores index of the
starting day in days.
futureDayIndex int Stores the index of
the day of given date.
11

QUESTION. 03

Write a program to input a number and using recursion method


check whether the number is an Emirp Number or not.
An emirp number is a number which is prime backward- and
forwards Example :13 and 31 are both prime numbers Thus, 13 is an
emirp number.
class name : Emirp
Data Members :
int num : to input a number
int rev : stores the reverse of the number n
int flag : is a flag variable
Member Methods
Emirp(int nn) : parameterised constructor to assign num=n
int isprime(int x) : recursive function to check whether the
parmeter prime number or not
void isEmirp () : calls isprime(int) and checks whether it is a
emirp number or not
12

import [Link].*;
class Emirp
{
int num;
int rev;
int flag;
Emirp(int nn)
{
num = nn;
rev = 0;
flag = 0;
}
int isPrime(int x, int i)
{
if (x <= 1)
return 0;
if (i == 1)
return 1;
if (x % i == 0)
return 0;
return isPrime(x, i - 1);
}
int reverseNumber(int n) {
if (n == 0)
return 0;
int r = n % 10;
return r * (int)[Link](10, (int)Math.log10(n)) + reverseNumber(n /
10);
}
void isEmirp()
{
int check1 = isPrime(num, num - 1);
rev = reverseNumber(num);
if (num == rev)
{
[Link](num + " is not an Emirp Number.");
return;
13

}
int check2 = isPrime(rev, rev - 1);
if (check1 == 1 && check2 == 1)
[Link](num + " is an Emirp Number.");
else
[Link](num + " is not an Emirp Number.");
}

public static void main( )


{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
Emirp obj = new Emirp(n);
[Link]();
}
}
14

OUTPUT :

Enter a number: 13
13 is an Emirp Number.

Enter a number: 23
23 is not an Emirp Number.
15

VARIABLE DESCRIPTION TABLE :

VARIABLE NAME DATA TYPE DESCRIPTION


num int To store the number.
rev int To store the reverse of
the number.
flag int A flag variable.
nn int Constructor
parameter used to
initialize num.
x int Parameter used in the
recursive isPrime
method to check for
prime.
i int Divisor used in the
recursive check for
prime.
n int To store the input
number.
r int Stores the last digit of
the number during
reversal.
16

QUESTION. 04

Define a class called Abword to print any name in abbreviated


form. For example: AJAY GOPAL RAMESH KRISHNA will be printed
as [Link].
Class name : Abword
Data Members :
String nm : To store name
String anm : To store abbreviated name
Member Methods
void read() : Accepts any name in uppercase
String print(String a) : Using recursive technique, prints name in
abbreviated te as shown above and returns the result
void show() : Calls print(String) and prints it in abbreviated form
static void main() : Creates object and calls other methods
17

import [Link].*;
class Abword
{
String nm;
String anm = "";
void read()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter name in uppercase: ");
nm = [Link]().trim();
}
String print(String a)
{
int i = [Link](' ');
if (i == -1)
return a;
anm += [Link](0) + ".";
return print([Link](i + 1));
}
void show()
{
String result = print(nm);
[Link](anm + result);
}
public static void main( )
{
Abword obj = new Abword();
[Link]();
[Link]();
}
}
18

OUTPUT :

Enter name in uppercase: MOHANDAS KARAMCHAND GANDHI


[Link]
19

VARIABLE DESCRIPTION TABLE :

VARIABLE NAME DATA TYPE DESCRIPTION


nm String Stores the full name.
anm String Stores the
abbreviated part of
the name.
a String Parameter, method
representing the
current substring of
the name being
processed.
i int Index of the first
space in the current
substring to separate
words.
20

QUESTION. 05

Design a class palindrome to check if a given number is a


palindrome number or not. [A number is said to be palindrome if it
treats same from both ends.] Examples are : 121, 55, 3223, 45754, etc.
Some of the members of the class are given below :
Class name : Palindrome
Data members :
num : To store the number.
rev : To store its reverse.
Methods functions :
Palindrome (int nn) : Parameterised constructor to initialize the
data members num = nn and rev to 0.
int revnum (int i) : Returns the reverse of the number num, using
a recursive technique.
void check () : Checks whether the given number is palindrome
or not, by invoking the function revnum (int i) and displays the
result with appropriate message.
Specify the class Palindrome giving details of the constructor, int
revnum(int) and void check (). Define a main () function to create
an object and call the functions accordingly to enable the task.
21

import [Link].*;
class Palindrome
{
int num;
int rev;
Palindrome(int nn)
{
num = nn;
rev = 0;
}
int revnum(int i)
{
if (i == 0)
return 0;
return (i % 10) * (int)[Link](10, (int)Math.log10(i)) + revnum(i /
10);
}
void check()
{
rev = revnum(num);
if (rev == num)
[Link](num + " is a Palindrome Number.");
else
[Link](num + " is not a Palindrome Number.");
}
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
Palindrome obj = new Palindrome(n);
[Link]();
}
}
22

OUTPUT :

Enter a number: 1234


1234 is not a Palindrome Number.

Enter a number: 45754


45754 is a Palindrome Number.
23

VARIABLE DESCRIPTION TABLE :

VARIABLE NAME DATA TYPE DESCRIPTION


num int Stores the original
number input by user.
rev int Stores the revered
number.
nn int Constructor
parameter used to
initialize num.
i int Parameter used in the
recursive function
revnum().
n int Variable to accept
user's input.
24

QUESTION. 06

Design a class BinSearch to search for a particular value in an array.


Some of the members of the class are given below:
Class : BinSearch
Data members/instance variables:​
arr[ ] : to store integer elements
n : integer to store the size of the array
Member functions/methods:​
BinSearch(int nn ) : Constructor to initialize n=nn and declare the
array.
void fillarray( ) : Accepts ‘n’ numbers in array arr[]
void sort( ) : searches for the value ‘v’ using binary search and
recursive technique and returns its location if found otherwise
returns -1
Define the class BinSearch giving details of the constructor( ), void
fillarray( ), void sort( ) and int bin_search(int,int,int).
Define the main( ) function to create an object and call the
functions accordingly to enable the task.
25

import [Link].*;
class BinSearch
{
int arr[];
int n;
static Scanner x=new Scanner([Link]);
BinSearch(int nn)
{
n=nn;
}
void fillarray()
{
arr=new int[n];
[Link]("Enter "+n + " elements");
for(int i =0;i<n;i++)
arr[i]=[Link]();
}
void sort()
{
int t;
for(int i=0;i<n-1;i++)
for(int j =0;j<n-1-i;j++)
{
if (arr[j]>arr[j+1])
{
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
int bin_search(int l,int u, int v )
{
int m=(l+u)/2;
if(arr[m]==v)
return m;
else if(l>u)
26

return -1;
else if (arr[m]>v)
return bin_search(l,m-1,v);
else
return bin_search(m+1,u,v);
}
static void main()
{
BinSearch obj = new BinSearch(5);
[Link]();
[Link]();
[Link](" location: " + obj.bin_search(0,4,20) );
}
}
27

OUTPUT :

Enter 5 elements
34 12 20 56 2
location: 2
28

VARIABLE DESCRIPTION TABLE :

VARIABLE NAME DATA TYPE DESCRIPTION

arr int Array to store integer


elements entered by
user.
n int Size of the array.
t int Used in swapping.
i int Loop counter
j int Loop counter.
l int Lower bounds used in
sorting.
u int Upper bounds used in
sorting.
v int Value to be searched
in array.
m int Midpoint index used in
binary search.
29

QUESTION. 07

Write program in Java to create 3 matrices, and store the elements


in two ditterert matrices A and B of order 3 x 3. Find the product of
both the matrices and store the result in another matrix and
display the result matrix.
For example :

Array 1
1 2 3

4 5 6

1 2 3

Array 2
2 3 4

1 5 3

6 5 3

3rd array after multiplying


22 28 19

46 67 49

22 28 19
30

import [Link].*;
class MatrixMultiply
{
public static void main( )
{
Scanner sc = new Scanner([Link]);
int[][] A = new int[3][3];
int[][] B = new int[3][3];
int[][] C = new int[3][3];
[Link]("Enter elements for Matrix A (3x3):");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
A[i][j] = [Link]();
[Link]("Enter elements for Matrix B (3x3):");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
B[i][j] = [Link]();
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
for (int k = 0; k < 3; k++)
C[i][j] += A[i][k] * B[k][j];
[Link]("Resultant Matrix after multiplication:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
[Link](C[i][j] + "\t");
[Link]();
}
}
}
31

OUTPUT :

Enter elements for Matrix A (3×3) :


5 2 1
0 3 4
6 7 8

Enter elements for Matrix B (3×3) :


1 0 2
3 5 6
7 8 9

Resultant Matrix after multiplication :


18 10 24
33 39 42
93 31 108
32

VARIABLE DESCRIPTION TABLE :

VARIABLE NAME DATA TYPE DESCRIPTION


A int Matrix to store the 1st
set of input values.
B int Matrix to store the 2nd
set of input values.
C int Matrix to store the
result of multiplying A
and B.
i int Loop counter for rows.
j int Loop counter for
columns.
k int Loop counter for inner
multiplication steps.
33

QUESTION. 08

Design a class to convert and print the given word in alphabetical order.
Class name : convert
Data Members :
String word : To input the word to be converted.
String sortwd : To store the converted word.
Member Methods :
convert() : Default constructor to initialise the word and sorted.
void input() : To input the word.
void sort() : To sort the word in alphabetical order.
void display() : To print both words.
34

import [Link].*;
class convert
{
String word;
String sortwd;
convert()
{
word = "";
sortwd = "";
}
void input()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a word: ");
word = [Link]();
}
void sort()
{
char[] arr = [Link]();
[Link](arr);
sortwd = new String(arr);
}
void display()
{
[Link]("Original word: " + word);
[Link]("Sorted word: " + sortwd);
}
public static void main( )
{
convert obj = new convert();
[Link]();
[Link]();
35

[Link]();
}
}
36

OUTPUT :

Enter a word: banana


Original word: banana
Sorted word: aaabnn
37

VARIABLE DESCRIPTION TABLE :

VARIABLE NAME DATA TYPE DESCRIPTION


word String Stores the original
input word.
sorted String Stores the word
sorted alphabetically.
arr char Character array used
to sort the word.
38

QUESTION. 09

Design a class called Banking_system with the following specifications:


Data Members :
cus_name : to store the name of the depositor.
accno : to store the account number.
type acc : to store the type of the account(s/c).
bal : to store the balance amount in the account.
Member Functions :
void input( ): to input data to all the data members.
(Minimum balance Rs.1000)
void deposit(int d) : to deposit the amount d and update the balance.
void withdraw(int w) : to withdraw the amount a and update the
balance.
void print() : to print all the details.
Write the main method to create an object of the class and call the
above methods. .
39

import [Link].*;
class Banking_system
{
String cus_name;
String accno;
String type_acc;
int bal;
void input()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter customer name: ");
cus_name = [Link]();
[Link]("Enter account number: ");
accno = [Link]();
[Link]("Enter account type (s/c): ");
type_acc = [Link]();
do
{
[Link]("Enter balance (minimum Rs.1000): ");
bal = [Link]();
}
while (bal < 1000);
}
void deposit(int d)
{
bal += d;
}
void withdraw(int w)
{
if (bal - w >= 1000)
{
bal -= w;
}
else
{
[Link]("Insufficient balance. Minimum balance of
Rs.1000 must be maintained.");
40

}
}

void print()
{
[Link]("Customer Name: " + cus_name);
[Link]("Account Number: " + accno);
[Link]("Account Type: " + type_acc);
[Link]("Balance: Rs." + bal);
}
public static void main( )
{
Banking_system obj = new Banking_system();
[Link]();
[Link](5000);
[Link](2000);
[Link]();
}
}
41

OUTPUT :

Enter customer name: John Doe


Enter account number: 123456789
Enter account type (s/c): s
Enter balance (minimum Rs.1000): 800
Enter balance (minimum Rs.1000): 1500
Customer Name: John Doe
Account Number: 123456789
Account Type: s
Balance: Rs.4500
42

VARIABLE DESCRIPTION TABLE :

VARIABLE NAME DATA TYPE DESCRIPTION


cus_name String Stores the name of
the account holder.
accno String Stores the account
number.
type_acc String Stores the type of
account.
bal int Stores the current
balance amount in the
account.
d int Deposit amount
passed to the deposit
method. .
w int Withdrawal amount
passed to the
withdraw method.
43

QUESTION. 10

Write a Java program using methods to print whether a number is


an Armstrong number or not.
Following are the specifications of the class to be used in the
program :
Class : armstrong
Data Members : int n
Member Methods :
void input() : Inputs the number to be checked in n
boolean check_armstrong() : Returns true if the number is an
Armstrong eise false.
void display() : calls check_armstrong() and prints Armstrong or
not.
An Armstrong number is a positive m-digit number that is equal to
the sum of the powers of their digits. It is also known as the
pluperfect number.
For example,
153 : 1³+5³+3³ = 1+125+27 = 153
125 : 1³+2³+5³ = 1+8+125 = 134 (Not an Armstrong Number)
1634 : 1⁴+6⁴+3⁴+4⁴ = 1+1296+81+256 = 1643
44

import [Link].*;
class armstrong {
int n;
void input()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
n = [Link]();
}
boolean check_armstrong()
{
int temp = n, sum = 0, digits = 0;
while (temp > 0)
{
digits++;
temp /= 10;
}
temp = n;
while (temp > 0)
{
int digit = temp % 10;
sum += [Link](digit, digits);
temp /= 10;
}
return sum == n;
}
void display() {
if (check_armstrong())
[Link](n + " is an Armstrong number");
else
[Link](n + " is not an Armstrong number");
}

public static void main( )


{
armstrong obj = new armstrong();
[Link]();
45

[Link]();
}
}
46

OUTPUT :

Enter a number: 153


153 is an Armstrong number

Enter a number: 125


125 is not an Armstrong number
47

VARIABLE DESCRIPTION TABLE :

VARIABLE NAME DATA TYPE DESCRIPTION


n int Stores the number to
be checked if it is an
Armstrong number.
temp int Temporary variable
used for processing
digits of the number.
sum int Stores the sum of
digits raised to the
power of the number
of digits.
digits int Stores the total
number of digits in the
number.
digit int Stores the current
digit being processed.

You might also like