0% found this document useful (0 votes)
48 views

Java Programs

The document describes a Java class for a bank database that supports depositing, withdrawing, and checking account balances. The class includes methods to open an account, show account details, deposit funds, withdraw funds, and search for an account. It also includes a main method that initializes an array of account objects, prompts the user for various banking transactions, and performs the requested operations like deposit, withdrawal, or displaying accounts.

Uploaded by

Moin Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Java Programs

The document describes a Java class for a bank database that supports depositing, withdrawing, and checking account balances. The class includes methods to open an account, show account details, deposit funds, withdraw funds, and search for an account. It also includes a main method that initializes an array of account objects, prompts the user for various banking transactions, and performs the requested operations like deposit, withdrawal, or displaying accounts.

Uploaded by

Moin Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 111

JAVA Programming Language (403)

Q-11. Design a class for a bank database the database should support the
following

operations :

1. Deposit a certain amount into an Account

2. Withdrawing a certain amount from an Account

3. Return a value specifying the amount (I.e. balance) in an amount ANS:- import

java.util.Scanner;

class BankDetails {

private String accno;

private String name;

private String

acc_type; private long

balance;

Scanner sc = new Scanner(System.in); public

void openAccount() {

System.out.print("Enter Account No: "); accno

= sc.next();

System.out.print("Enter Account type: "); acc_type

= sc.next();

System.out.print("Enter Name: "); name

= sc.next();

Moinkhan
JAVA Programming Language (403)

System.out.print("Enter Balance: "); balance

= sc.nextLong();

page no:1
public void showAccount() {

System.out.println("Name of account holder: " + name);

System.out.println("Account no.: " + accno);

System.out.println("Account type: " + acc_type);

System.out.println("Balance: " + balance);

public void deposit() { long

amt;

System.out.println("Enter the amount you want to deposit: "); amt

= sc.nextLong();

balance = balance + amt;

public void withdrawal() { long

amt;

System.out.println("Enter the amount you want to withdraw: ");

amt = sc.nextLong(); if (balance >= amt) { balance = balance -

amt;

System.out.println("Balance after withdrawal: " + balance);

Moin KHAN
JAVA Programming Language (403)

} else {

System.out.println("Your balance is less than " + amt + "\tTransaction


failed...!!" ); }

page no:2
}

public boolean search(String ac_no) {

if (accno.equals(ac_no)) {

showAccount(); return (true);

return (false);

public class Q11 {

public static void main(String arg[]) {

Scanner sc = new Scanner(System.in);

System.out.print("How many number of customers do you want to input?


");

int n = sc.nextInt();

Moinkhan
JAVA Programming Language (403)

BankDetails C[] = new BankDetails[n]; for

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

C[i] = new BankDetails();

C[i].openAccount();

int ch; do

System.out.println("\n ***Banking System Application***");

page no:3
System.out.println("1. Display all account details \n 2. Search by Account

number\n 3. Deposit the amount \n 4. Withdraw the amount \n 5.Exit ");

System.out.println("Enter your choice: "); ch = sc.nextInt(); switch (ch) { case 1:

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

C[i].showAccount();

break; case

2:

System.out.print("Enter account no. you want to search: "); String

ac_no = sc.next();

boolean found = false;

Moin KHAN
JAVA Programming Language (403)

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

found = C[i].search(ac_no);

if (found) {

break;

}
if (!found) {

System.out.println("Search failed! Account doesn't exist..!!");

page no:4
break; case

3:

System.out.print("Enter Account no. : "); ac_no

= sc.next();

found = false;

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

found = C[i].search(ac_no);

if (found) { C[i].deposit();

break;

Moinkhan
JAVA Programming Language (403)

if (!found) {

System.out.println("Search failed! Account doesn't exist..!!");

break; case

4:

System.out.print("Enter Account No : "); ac_no

= sc.next();

found = false;

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

found = C[i].search(ac_no);

if (found) {

page no:5
C[i].withdrawal();

break;

}
if (!found) {

System.out.println("Search failed! Account doesn't exist..!!");

break; case

5:

Moin KHAN
JAVA Programming Language (403)

System.out.println("See you soon..."); break;

}
while (ch != 5);

OUTPUT:-

page no:6

Moinkhan
JAVA Programming Language (403)

Moin KHAN
JAVA Programming Language (403)

page no:7

Moinkhan
JAVA Programming Language (403)

page no:8
Q-12. Create a abstract class employee, having its properties & abstract
function for calculating net salary and displaying the information. Drive
manager & clerk class from this abstract class & implements the
abstract method net salary and override the display method. ANS:- class
EmployeeDetails { int emp_id, salary;

String name, address, department, email; public

int getEmp_id() {

return emp_id;

public void setEmp_id(int emp_id) { this.emp_id

= emp_id;

public int getSalary() { return

salary;

public void setSalary(int salary) { this.salary

= salary;

public String getName() { return

name;

Moin KHAN
JAVA Programming Language (403)

public void setName(String name) { this.name

= name;

page no:9
}

public String getAddress() { return

address;

public void setAddress(String address) { this.address

= address;

public String getDepartment() { return

department;

public void setDepartment(String department) { this.department

= department;

public String getEmail() { return

email;

public void setEmail(String email) { this.email

= email;

Moinkhan
JAVA Programming Language (403)

public String toString() {

return "Employee [emp_id = " + emp_id + ", salary = " + salary + ", name = " +
name + ", address = " + address

+ ", department = " + department + ", email = " + email + "]";

page no:10

Moin KHAN
JAVA Programming Language (403)

public class Q12{

public static void main(String args[]) {

EmployeeDetails emp = new EmployeeDetails();

emp.setEmp_id(101); emp.setName("Emma

Watson"); emp.setDepartment("IT");

emp.setSalary(15000);

emp.setAddress("New Delhi");

emp.setEmail("Emmawatson123@gmail.com");

System.out.println(emp); int sal =

emp.getSalary(); int increment = 0;

if ((sal >=1000) && (sal <=1500))

increment += (sal * 2)/100; sal

= sal+increment;

emp.setSalary(sal); page no:11


JAVA Programming Language (403)

System.out.println("\n Salary is incremented \n");

System.out.println(emp);

Roll no : 30
Gheewala Aadil

MOIN
JAVA Programming Language (403)

}else if ((sal >=1500) && (sal <=20000)){

increment += (sal * 5)/100;

sal = sal+increment;

emp.setSalary(sal);

System.out.println("\n Salary is incremented \n");

System.out.println(emp);

}else {

System.out.println("\n Salary is not incremented \n");

System.out.println(emp);

OUTPUT:-

Gheewala Aadil Roll no : 30


JAVA Programming Language (403)
Q-13. Write down a java program to print event and odd number series
respectively from two threads: t1 and t2 synchronizing on a shared object.

Let t1 print message “ping ” and

Let t2 print message “,-- pong”.

Take as command line argument, the following input to the program

Sleep interval for thread t1

Sleep interval for thread t2

Message per cycle

No. of cycle

ANS:- public class Q13 {

public static void main(String[] args) {

Object LOCK_OBJECT = new Object();

Thread ping = new Thread(new PingPongThread(LOCK_OBJECT, "Ping"));

Thread pong = new Thread(new PingPongThread(LOCK_OBJECT, "Pong"));

ping.start();

pong.start();

class PingPongThread implements Runnable{

MOIN
private Object LOCK_OBJECT; private

String name;

page no:13
JAVA Programming Language (403)

JAVA Programming Language (403)

public PingPongThread(Object LOCK_OBJECT, String name) {

this.LOCK_OBJECT = LOCK_OBJECT;

this.name = name;

public void run() {

synchronized (LOCK_OBJECT) { while(true)

System.out.println(name);

try {

Thread.sleep(1000);

} catch (InterruptedException e1) {

e1.printStackTrace();

LOCK_OBJECT.notify();

try { page no:


14
MOIN
LOCK_OBJECT.wait(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}
JAVA Programming Language (403)
}

OUTPUT:-

MOIN
page no:15
JAVA Programming Language (403)

JAVA Programming Language (403)

Q-14. Write a java program to display transpose matrix. ANS:-

public class Q14{

public static void main(String args[]){

int original[][]={{1,3,4},{2,4,3},{3,4,5}};

int transpose[][]=new int[3][3]; for(int

i=0;i<3;i++){

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

transpose[i][j]=original[j][i];

System.out.println("Printing Matrix without transpose:");

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

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

System.out.print(original[i][j]+" ");

System.out.println();

page no:16
MOIN
System.out.println("Printing Matrix After Transpose:");

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

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

System.out.print(transpose[i][j]+" ");

System.out.println();

Roll no : 30
Gheewala Aadil
JAVA Programming Language (403)
}

}}

OUTPUT:-

MOIN
page no:17
JAVA Programming Language (403)

JAVA Programming Language (403)

Q-15. Write a java program to search key element in an array.

ANS:- import java.util.Scanner;

public class Q15

public static void main(String[] args)

{
int n, x, flag = 0, i = 0;

Scanner s = new Scanner(System.in);

System.out.print("Enter no. of elements you want in array:"); n

= s.nextInt();

int a[] = new int[n];

System.out.println("Enter all the elements:"); for(i

= 0; i < n; i++)

{
a[i] = s.nextInt();

System.out.print("Enter the element you want to find:"); x

= s.nextInt();

for(i = 0; i < n; i++)

{
MOIN
if(a[i] == x)

{
flag = 1;
JAVA Programming Language (403)

MOIN
JAVA Programming Language (403)
break;

}
else

{
flag = 0;

if(flag == 1)
"Element found at position:"+(i + 1));
{

System.out.println(

}
"Element not found");
else

System.out.println(

Roll no : 30 page no:19


OUTPUT:-
JAVA Programming Language (403)
Gheewala Aadil Roll no : 30 page no:20
JAVA Programming Language (403)

Q-16. Write a program to generate menu driven program for getChars, insert,
replace, reverse, setCharAt, deleteCharAt, Toggle Case, Upper Case & Lower
Case.

ANS:- import java.util.Scanner;

public class Q16

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.println("Enter '1' to display upper case letters from Z to A");

System.out.println("Enter '2' to display lower case letters from a to z");

System.out.print("Enter your choice: "); int

ch = in.nextInt();

int count = 0;

switch (ch) {

case 1:
for (int i = 90; i > 64; i--) {

char c = (char)i;

System.out.print(c);

page no:21
JAVA Programming Language (403)

System.out.print(" ");

count++;

if (count == 10) {

System.out.println();

count = 0;

break;

case 2:

for (int i = 97; i < 123; i++) {

char c = (char)i;

System.out.print(c);

System.out.print(" ");

count++;

if (count == 10) {

System.out.println();

count = 0;

}
page no:22
}

break;

default:
JAVA Programming Language (403)

System.out.println("Incorrect Choice");

OUTPUT:-

MOIN KHAN :
JAVA Programming Language (403)

Q-17. Write a java program to add matrices of the same size.

ANS:- import java.util.Scanner;

public class Q17 {

public static void main(String args[])

int m, n, c, d;

Scanner in = new Scanner(System.in);

System.out.println("Input number of rows of matrix");

m = in.nextInt();

System.out.println("Input number of columns of matrix");

n = in.nextInt();

int array1[][] = new int[m][n];

int array2[][] = new int[m][n];

int sum[][] = new int[m][n];

System.out.println("Input elements of first matrix");

page no:24
for ( c = 0 ; c < m ; c++ ) for ( d

= 0 ; d < n ; d++ )

array1[c][d] = in.nextInt();
JAVA Programming Language (403)

System.out.println("Input the elements of second matrix");

for ( c = 0 ; c < m ; c++ )

for ( d = 0 ; d < n ; d++ )

array2[c][d] = in.nextInt();

for ( c = 0 ; c < m ; c++ )

for ( d = 0 ; d < n ; d++ )

sum[c][d] = array1[c][d] + array2[c][d];

System.out.println("Sum of the matrices:-");

for ( c = 0 ; c < m ; c++ )

{
for ( d = 0 ; d < n ; d++ )

System.out.print(sum[c][d]+"\t");

System.out.println();

page no:25
}

OUTPUT:-

MOIN KHAN
JAVA Programming Language (403)
pageno:26

MOIN khan
JAVA Programming Language (403)

Q-18. Write a java program to enter the numbers till the user wants and at the
end it’s should display the count of positive, negative and zeros.

ANS:- import java.util.Scanner;

public class Q18

public static void main(String[] args)

int countP=0, countN=0, countZ=0, i; int[]

arr = new int[10];

Scanner scan = new Scanner(System.in);

System.out.print("Enter 10 Numbers: "); for(i=0;

i<10; i++)

arr[i] = scan.nextInt();

for(i=0; i<10; i++)

if(arr[i]<0)
JAVA Programming Language (403)

countN++;

else if(arr[i]>0)

countP++; else

pageno:27

MOIN khan
countZ++;

System.out.println("\nTotal Positive Number: " +countP);

System.out.println("Total Negative Number: " +countN);

System.out.println("Total Zero: " +countZ);

OUTPUT:-
JAVA Programming Language (403)
page no:28

MOIN khan
Programming Language (403)
JAVA

Q-19. Write a java method to find LCM and GCD of two numbers.

ANS:- import java.util.Scanner;

public class Q19

static int gcd(int x, int y)

int r=0, a, b;
a = (x > y) ? x : y;

b = (x < y) ? x : y;

r = b;

while(a % b != 0)

r = a % b;

a = b;

b = r;

return r;
page no:29
JAVA Programming Language (403)

static int lcm(int x, int y)

{
int a;

MOIN khan
Roll no : 30
JAVA Programming Language (403)
a = (x > y) ? x : y; while(true)

if(a % x == 0 && a % y == 0) return

a;

++a;

public static void main(String args[])

Scanner input = new Scanner(System.in);

System.out.println("Enter the two numbers: "); int

x = input.nextInt();

int y = input.nextInt();

System.out.println("The GCD of two numbers is: " + gcd(x, y));

System.out.println("The LCM of two numbers is: " + lcm(x, y));

input.close();

MOIN khan
OUTPUT:-

page no:30
JAVA Programming Language (403)

MOIN khan
JAVA Programming Language (403)

page no:31
Q-20. Write a java program to create a simple calculator.

ANS:- import java.util.Scanner;

class Q20 {

public static void main(String[] args) {

char operator;

Double number1, number2, result;

Scanner input = new Scanner(System.in);

System.out.println("Choose an operator: +, -, *, or /");

operator = input.next().charAt(0);

System.out.println("Enter first number"); number1 =

input.nextDouble();

System.out.println("Enter second number"); number2

= input.nextDouble();

switch (operator) { case '+':

result = number1 + number2;

System.out.println(number1 + " + " + number2 + " = " + result); break;

Moin KHAN
JAVA Programming Language (403)

case '-':

page no:32
result = number1 - number2;

System.out.println(number1 + " - " + number2 + " = " + result); break;

case '*':

result = number1 * number2;

System.out.println(number1 + " * " + number2 + " = " + result); break;

case '/':

result = number1 / number2;

System.out.println(number1 + " / " + number2 + " = " + result); break;

default:

System.out.println("Invalid operator!");

break;

input.close();

OUTPUT:-

MOIN khan
JAVA Programming Language (403)

page no:33

Moin KHAN
JAVA Programming Language (403)

page no:34
Q-21. Write a java program that would print the information (name, yearof
joining, salary, address) of three employee by creating a class method ‘
Employee’ the output should be as follows:

Name year of joining address

Robert 1994 64C-Surat

Sam 2000 68D-Vadodara

Robert 1999 64B-Rajkot

ANS:- class Employee

String Name; int

Year;

String Address;

Employee(String nam,int years, String addr)

Name=nam;

Year=years;

Address=addr;

void Sam()

MOIN khan
JAVA Programming Language (403)

System.out.println(Name+ " " +Year + " " +Address);

void Robert()

page no:
35
{

System.out.println(Name+ " " +Year + " " +Address);

void John()

System.out.println(Name+ " " +Year + " " +Address);

class Q21

public static void main(String args[])

System.out.println("Name" + " Year of Joining " + "Address" );

Employee e=new Employee("Robert" ,1994, "64C-Surat");

Moin KHAN
JAVA Programming Language (403)

e.Sam();

Employee e1=new Employee("Sam" ,2000, "68D-Vadodara");

e1.Robert();

Employee e2=new Employee("Robert" ,1999, "64B-Rajkot");

e2.Sam();

OUTPUT:- page no:36

MOIN khan
JAVA Programming Language (403)

page no:37
Q-22. Write a program to accept multiple line contents until you press “N” then check
total no of consonants, lines & total no. of word.

Input :

Enter String :

This is Java book

Continue : Y

Java is programming lang.

Continue : N

Output:

Total line : 2

Constants : 22

Words : 08

ANS:- import java.util.Scanner;

public class Q22 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int numLines = 0; int numConsonants = 0;

int numWords = 0;

Moin KHAN
JAVA Programming Language (403)

while (true) {

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

page no:38

MOIN khan
JAVA Programming Language (403)

String line = scanner.nextLine();

if (line.equalsIgnoreCase("N")) {

break;

numLines++;

numWords += countWords(line); numConsonants

+= countConsonants(line);

System.out.print("Continue : ");

String continueInput = scanner.nextLine();

if (continueInput.equalsIgnoreCase("N")) {

break;

System.out.println("Total line : " + numLines);


page no:39
JAVA Programming Language (403)

System.out.println("Consonants : " + numConsonants);

System.out.println("Words : " + numWords);

Moin Khan
JAVA Programming Language (403)

JAVA Programming Language (403)

private static int countConsonants(String line) { int

numConsonants = 0;

for (int i = 0; i < line.length(); i++) { char c =

line.charAt(i); if (Character.isLetter(c)

&& !isVowel(c)) {

numConsonants++;

return numConsonants;

private static boolean isVowel(char c) { return

"AEIOUaeiou".indexOf(c) != -1;

private static int countWords(String line) { String[]

words = line.split("\\s+");

page no:40

Moin Khan
return words.length;

OUTPUT:-
JAVA Programming Language (403)

Moin Khan
page no:41
JAVA Programming Language (403)

Q-23. Create 2 classes which contains following details.

Emp_table Dept_table

Empno Deptno

Empname Deptname

Esal

Edesignation

Enter atleast 5 records & display report in format & Accept data through user

& print it to console.

Ename Edesignation Deptname Esal

ANS:-

OUTPUT:-

Moin Khan
JAVA Programming Language (403)

page
no:42

Q-24. Write a program to accept number from command line and display
indivisual digit at interval of 3 second.

ANS:- import java.util.Scanner;

public class Q24

public static void main(String args[])

int n, temp, digit, count=0;

Scanner sc = new Scanner(System.in);

System.out.print("Enter any number: ");

n=sc.nextInt();

temp=n;

while(n>0)

n=n/10;

Moin Khan
JAVA Programming Language (403)

count++;

while(temp > 0)

digit=temp%10;

System.out.println("Digit at place "+count+" is: "+digit);

temp=temp/10; page no:43


count--;
}

OUTPUT:-

Moin Khan
JAVA Programming Language (403)

page no:44 Q-25. Write a program with options to add node at starting and at end
in singly link list.

ANS:- public class Q25 {

class Node{ int

data;

Node next;

public Node(int data) { this.data

= data;

this.next = null;

public Node head = null; public

Node tail = null;

public void addAtStart(int data) {

Node newNode = new Node(data);

if(head == null) {

Moin Khan
JAVA Programming Language (403)

head = newNode; tail

= newNode;

page no:45

Moin Khan
JAVA Programming Language (403)

else {

Node temp = head;

head = newNode;

head.next = temp;

public void display() {

Node current = head; if(head

== null) {

System.out.println("List is empty");

return;

System.out.println("Adding nodes to the start of the list: ");

while(current != null) {

System.out.print(current.data + " "); current

= current.next;

}
page no:46
System.out.println();

public static void main(String[] args) {

Roll no : 30
Gheewala Aadil
Q25 sList = new Q25();
JAVA Programming Language (403)
sList.addAtStart(1);

sList.display();

sList.addAtStart(2);

sList.display();

sList.addAtStart(3);

sList.display();

sList.addAtStart(4);

sList.display();

OUTPUT:-
page no:47
JAVA Programming Language (403)

Q-26. Write a program to add node in middle in circular link list.

ANS:- public class Q26 {

public class Node{

int data;

Node next;

public Node(int data) { this.data

= data;

public int size;

public Node head = null;

public Node tail = null;

public void add(int data){

Node newNode = new Node(data);

if(head == null) {

head = newNode; tail

= newNode;
page no:48
newNode.next = head;

}
else {

tail.next = newNode;
JAVA Programming Language (403)

tail = newNode; tail.next

= head;

}
size++;

public void addInMid(int data){

Node newNode = new Node(data);

if(head == null){

head = newNode;

tail = newNode;

newNode.next = head;

}
else{

Node temp,current;

int count = (size % 2 == 0) ? (size/2) : ((size+1)/2); temp

= head;

current= null;

for(int i = 0; i < count; i++){ current

= temp;
page no:49
temp = temp.next;

}
JAVA Programming Language (403)

current.next = newNode; newNode.next

= temp;

}
size++;

public void display() {

Node current = head;

if(head == null) {

System.out.println("List is empty");

}
else {
do{

System.out.print(" "+ current.data); current

= current.next;

}while(current != head);

System.out.println();

page no:50
public static void main(String[] args)

{ Q26 cl = new Q26(); cl.add(1);


cl.add(2);
JAVA Programming Language (403)
cl.add(3);

cl.add(4);

System.out.println("Original list: "); cl.display();

cl.addInMid(5);

System.out.println( "Updated List: "); cl.display();

cl.addInMid(6);

System.out.println("Updated List: "); cl.display();

OUTPUT:-

Moin Khan page no:51


JAVA Programming Language (403)

Q-27. Write a program to delete node from given position in circular link list.

ANS:- public class Q27 {

public class Node{ int

data;

Node next;

public Node(int data) { this.data

= data;

public Node head = null;

public Node tail = null;

public void add(int data){

Node newNode = new Node(data);

if(head == null) {

head = newNode; tail

= newNode;

newNode.next = head;

}
else {

tail.next = newNode; tail

= newNode;

page no:52
JAVA Programming Language (403)

tail.next = head;

public void deleteEnd() { if(head

== null) {

return;

else {
if(head != tail ) {

Node current = head;

while(current.next != tail) { current

= current.next;

tail = current; tail.next

= head;

else {
page no:53
head = tail = null;

}
JAVA Programming Language (403)

public void display() {

Node current = head; if(head

== null) {

System.out.println("List is empty");

else {
do{

System.out.print(" "+ current.data); current

= current.next;

}while(current != head);

System.out.println();

public static void main(String[] args) {

Q27 cl = new Q27();

cl.add(1);

cl.add(2);
page no:54
cl.add(3);

cl.add(4);

System.out.println("Original List: ");

cl.display();
while(cl.head != null) { cl.deleteEnd();
JAVA Programming Language (403)
System.out.println("Updated List: "); cl.display();

OUTPUT:-

MOIN KHAN page no:55

You might also like