Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

🕹

JAVA Internals

EX1
1A. Write a Java program to create Class as Registration with properties as Full
Name(String), Gender(char), Age(int), Height(double), Phone Number(long), and
isMarried(Boolean) and print their values.

public class EX1A {


public static void main(String args[]) {
String fullName = "Chaitanya";
char gender = 'M';
int age = 19;
double height = 5.7;
long phoneNumber = 9177675611L;
boolean isMarried = false;
System.out.println("Name: " + fullName);
System.out.println("Gender: " + gender);
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Contact Number: " + phoneNumber);
System.out.println("Married: " + isMarried);
}
}

/*
Output:
Name: Chaitanya
Gender: M
Age: 19
Height: 5.7
Contact Number: 9177675611
Married: false
*/

1B. 1. Write a Java program to implement Type Casting and Conversion.

public class EX1B {


public static void main(String args[]) {
// type casting
int i = 10;
double d = i;
System.out.println(d);
// type conversion
double d1 = 10.5;
int i1 = (int) d1;
System.out.println(i1);

JAVA Internals 1
}
}

/*
* Output:
* 10.0
* 10
*/

1C. 1. Write a Java program to implement Wrapper Classes.

public class EX1C {


public static void main(String args[]) {
String a = "90.01";
double b = Double.parseDouble(a);
System.out.println(b);
float c = Float.parseFloat(a);
System.out.println(c);

}
}

/*
* Output:
* 90.01
* 90.01
*/

EX2
2A. 1. Write a Java program to take input as Regd.No and print the branch depending
upon the department code in that Regd.No using else-if and switch statements.
(EgRegNo:
19KD1A0505, 8th character is department Code, 5-CSE, 4-ECE, 3-MECH, 2-EEE etc.

import java.util.Scanner;

import javax.lang.model.util.ElementScanner14;

public class EX2A {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a Registration Numebr: ");
String st = input.nextLine();
char ch = st.charAt(0);
if (ch == '5')
System.out.println("CSE");
else if (ch == '4')
System.out.println("ECE");
else if (ch == '3')
System.out.println("Mech");
else if (ch == '2')
System.out.println("EEE");
else
System.out.println("Invalid");
input.close();
}
}

/*
* Output:
* Enter a Registration Numebr: 20KD1A05G6
* CSE
*/

2B. 1. Write a Java program to read input integers from Command Line Arguments and
print first and second largest numbers

JAVA Internals 2
import java.util.Arrays;

public class EX2B {


public static void main(String[] args) {
int size = args.length;
int[] arr = new int[size];
for (int i = 0; i < size; ++i)
arr[i] = Integer.parseInt(args[i]);

Arrays.sort(arr);
System.out.println("Largest Element: " + arr[size]);
System.out.println("Second Largest Element: " + arr[size - 1]);
}
}

/*
* Output:
* javac EX2B.java
* java EX2B 1 2 3 4 5 6 7 8 9 10
* Largest Element: 10
* Second Largest Element: 9
*/

2C. 1. Write a Java program to take input as Integer array and print even indexed even
numbers and odd indexed odd numbers.

import java.util.*;

public class EX2C {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = input.nextInt();
int[] arr = new int[size];
System.out.print("Enter the elements in the array: ");
for (int i = 0; i < size; ++i) {
arr[i] = input.nextInt();
}
System.out.print("Even Numbers: ");
for (int i = 0; i < size; i += 2) {
if (arr[i] % 2 == 0)
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.print("Odd Numbers:");
for (int i = 1; i < size; i += 2) {
if (arr[i] % 2 != 0)
System.out.print(arr[i] + " ");
}
System.out.println();
input.close();

}
}

/*
* Output:
* Enter the size of the array: 10
* Enter the elements in the array: 2 3 4 5 6 7 8 9 10 11
* Even Numbers: 2 4 6 8 10
* Odd Numbers: 3 5 7 9 11
*/

EX3
3A. Write a Java program to take input as Decimal number and convert into Roman
Number.

JAVA Internals 3
import java.util.Scanner;

public class EX3A {


public static void integerToRoman(int n) {
int[] values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
String[] romanLiterals = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
String roman = new String();
for (int i = 0; i < values.length; i++) {
while (n >= values[i]) {
n -= values[i];
roman += romanLiterals[i];
}
}
System.out.println("Roman Numeral: " + roman);
}

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
integerToRoman(n);
input.close();
}
}

/*
* Output:
* Enter a number: 1081
* Roman Numeral: MMLXXXI
*/

3B. Write a Java program to check whether given number is Extension number. The
extension number is the number which is present in the last digit(s) of its square.
(Eg. N=25, 625 is Extension number since it contains 25).

import java.util.Scanner;

class EX3B {
public static void main(String[] args) {
var input = new Scanner(System.in);
System.out.print("Enter a Number: ");
int num = input.nextInt();
long square = num * num;
String strSquare = Long.toString(square);
boolean flag = false;
for (int i = 0; i < strSquare.length(); i++) {
if (square % (Math.pow(10, i)) == num) {
flag = true;
break;
}
}
if (flag)
System.out.println("Extension Number");
else
System.out.println("Not an Extension Number");
input.close();
}
}

/*
* Output:
* Enter the number: 625
* Extension Number
*/

3C. Write a Java program to take input as Amount in rupees and print their
denominations and total number notes

import java.util.Scanner;

JAVA Internals 4
public class EX3C {

public static void denominations(int x) {


int[] arr = { 2000, 500, 200, 100, 50, 20, 10, 5, 2, 1 };
int count = 0;
for (int i = 0; i < arr.length; i++) {
count = 0;
while (x >= arr[i]) {
count = count + 1;
x -= arr[i];
}
if (count > 0)
System.out.println("Number of " + arr[i] + ": " + count);
}
}

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Enter amount: ");
int x = input.nextInt();
denominations(x);
input.close();

}
}

/*
* Output:
* Enter amount: 19021
* Number of 2000: 9
* Number of 500: 2
* Number of 20: 1
* Number of 1: 1
*/

EX4
4A. Create a Class named Student with properties as Student Id, Student Name, gender,
department, Age, Aggregate and methods as insertStudent() for inserting student
details and displayStudent() for printing student details.

class Student {
public String student_id;
public String full_name;
public String gender;
public String department;
public int age;
public float aggregate;

public void insertStudent(String sid, String name, String gen, String dept, int age, float agg) {
this.student_id = sid;
this.full_name = name;
this.gender = gen;
this.department = dept;
this.age = age;
this.aggregate = agg;
System.out.println();
System.out.println("---------------------------------");
System.out.println("All details inserted successfully");
System.out.println("---------------------------------");
System.out.println();
}

public void displayStudent() {


System.out.println("Id : " + student_id);
System.out.println("Name : " + full_name);
System.out.println("Gender : " + gender);
System.out.println("Department:" + department);
System.out.println("Age :" + age);
System.out.println("Aggregate :" + aggregate);
}
}

public class EX4A {


public static void main(String[] args) {
Student rahul = new Student();
System.out.println();
rahul.insertStudent("20KD1A05K4", "Rahul", "M", "CSE", 19, 9.03F);

JAVA Internals 5
rahul.displayStudent();
}

/*
* Output:
* ---------------------------------
* All details inserted successfully
* ---------------------------------
*
* Id : 20KD1A05K4
* Name : Rahul
* Gender : M
* Department:CSE
* Age :19
* Aggregate :9.03
*/

4B. Create a class Student with same properties as above and create a constructor to
insert student details and return the data using toString() method.

class Student {
public String student_id;
public String full_name;
public String gender;
public String department;
public Integer age;
public Float aggregate;

public Student(String sid, String name, String gen, String dept, int age, float agg) {
this.student_id = sid;
this.full_name = name;
this.gender = gen;
this.department = dept;
this.age = age;
this.aggregate = agg;
}

public void getStudentDetails() {


System.out.println(
student_id + ", " + full_name + ", " + gender + ", " + department + ", " + age.toString() + ", "
+ aggregate.toString());
}

public class EX4B {


public static void main(String[] args) {
Student rahul = new Student("20KD1A05K4", "Rahul", "M", "CSE", 20, 8.5f);
rahul.getStudentDetails();
}

/*
* Output:
* 20KD1A05K4, Rahul, M, CSE, 20, 8.5
*/

EX5
5A. Design a Class named Transaction to transfer amount (double) in different ways
using Account Number(int) , Phone Number(Long) and qr Code (String) as parameter into
a method transferAmount() to achieve Method or Constructor OverLoading.

class Transaction {
void transferAmount(int accountNumber, double amount, String qrCode) {

JAVA Internals 6
System.out.println("Account Number: " + accountNumber);
System.out.println("Amount: " + amount);
System.out.println("QR Code: " + qrCode);

void transferAmount(String qrCode, double amount) {


System.out.println("QR Code: " + qrCode);
System.out.println("Amount: " + amount);
}

class EX5A {
public static void main(String[] args) {
Transaction t1 = new Transaction();
Transaction t2 = new Transaction();
t1.transferAmount(123456789, 100.00, "8948348934");
t2.transferAmount("8948348934", 100.00);
}
}

/*
* Output:
* Account Number: 123456789
* Amount: 100.0
* QR Code: 8948348934
* QR Code: 8948348934
* Amount: 100.0
*/

5B.Design a super Class Account and sub Classes as LoanAccount, SavingsAccount and
CurrentAccount and implement relationship between parent and child classes. (Implement
Packages for the above classes)

class Account {
public int accNo;
public String accName;
public double accBalance;

public void getAccountDetails() {


System.out.println("Account Number: " + accNo);
System.out.println("Account Name: " + accName);
System.out.println("Account Balance: " + accBalance);
System.out.println();
}
}

class LoanAccount extends Account {


public double loanAmount;
public double loanRate;
public double interestAmount = loanAmount * loanRate;

public void loanAccountDetails() {


System.out.println("------Loan Account------");
System.out.println("Loan Amount: " + loanAmount);
System.out.println("Loan Rate: " + loanRate);
System.out.println("Interest Amount: " + interestAmount);
}
}

class SavingsAccount extends Account {


public double savingsAmount;
public double savingsRate;
public double interestAmount = savingsAmount * savingsRate;

public void savingsAccountDetails() {


System.out.println("------Savings Account------");
System.out.println("Savings Amount: " + savingsAmount);
System.out.println("Savings Rate: " + savingsRate);
System.out.println("Interest Amount: " + interestAmount);
}
}

class CurrentAccount extends Account {


public double currentAmount;

JAVA Internals 7
public double currentRate;
public double interestAmount = currentAmount * currentRate;

public void currentAccountDetails() {


System.out.println("------Current Account------");
System.out.println("Current Amount: " + currentAmount);
System.out.println("Current Rate: " + currentRate);
System.out.println("Interest Amount: " + interestAmount);
}
}

public class EX5B {


public static void main(String[] args) {
LoanAccount loan = new LoanAccount();
loan.accNo = 101;
loan.accName = "Loan Account";
loan.accBalance = 10000;
loan.loanAmount = 10000;
loan.loanRate = 0.05;
loan.loanAccountDetails();
loan.getAccountDetails();

SavingsAccount savings = new SavingsAccount();


savings.accNo = 102;
savings.accName = "Savings Account";
savings.accBalance = 20000;
savings.savingsAmount = 20000;
savings.savingsRate = 0.05;
savings.savingsAccountDetails();
loan.getAccountDetails();

CurrentAccount current = new CurrentAccount();


current.accNo = 103;
current.accName = "Current Account";
current.accBalance = 30000;
current.currentAmount = 30000;
current.currentRate = 0.05;
current.currentAccountDetails();
loan.getAccountDetails();

}
}

/*
* Output:
* ------Loan Account------
* Loan Amount: 10000.0
* Loan Rate: 0.05
* Interest Amount: 500.0
* Account Number: 101
* Account Name: Loan Account
* Account Balance: 10000.0
* ------Savings Account------
* Savings Amount: 20000.0
* Savings Rate: 0.05
* Interest Amount: 5000.0
* Account Number: 102
* Account Name: Savings Account
* Account Balance: 20000.0
* ------Current Account------
* Current Amount: 30000.0
* Current Rate: 0.05
* Interest Amount: 15000.0
* Account Number: 103
* Account Name: Current Account
* Account Balance: 30000.0
*/

EX6
6A. 1. Write a Java program to implement this and super keywords.

class Animal {
public String name;
public int age;

JAVA Internals 8
public void displayDetails(String name, int age) {
this.name = name;
this.age = age;

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


System.out.println("Age: " + age);
}
}

class Lion extends Animal {

public void getDetails(String name, int age) {


super.displayDetails(name, age);
System.out.println("Lion Roars");
}

public class EX6A {


public static void main(String[] args) {
var lion = new Lion();
lion.getDetails("Simba", 10);

/*
* Output:
* Name: Simba
* Age: 10
* Lion Roars
*/

6B. 1. Write a Java program to implement Static property, method, block and package.

import static java.lang.Math.*;

class Animal {
static String name = "Animal";
static int age = 4;

static void eat() {


System.out.println("Animal is eating");
}
}

public class EX6B {


public static void main(String[] args) {
System.out.println("Name: " + Animal.name);
System.out.println("Age: " + Animal.age);
System.out.println("Square root age: " + sqrt(Animal.age));
Animal.eat();
System.out.println();

}
}

/*
* Output:
* Name: Animal
* Age: 4
* Square root age: 2.0
* Animal is eating
*/

6C.Write a Java program to implement final property, method and class.

final class Bank {


final int account = 94913;
final String name = "SBI";
final String address = "Bangalore";

JAVA Internals 9
final void display() {
System.out.println("Account: " + account);
System.out.println("Name: " + name);
System.out.println("Address: " + address);
}
}

public class EX6C {


public static void main(String[] args) {
var bank = new Bank();
bank.display();
}
}

/*
* Output:
* Account: 94913
* Name: SBI
* Address: Bangalore
*/

EX7
7A. 1. Write a Java program to implement Data Abstraction using Abstract class and
Interface.

abstract class Animal {


public void eatAnimal() {
System.out.println("Animal is eating");
}

public abstract void eat();


}

interface AnimalInterface {
final public int age = 4;

void eat();
}

class Lion extends Animal implements AnimalInterface {


public void eat() {
System.out.println("Lion is eating");
}
}

public class EX7A {


public static void main(String[] args) {
var lion = new Lion();
lion.eat();
lion.eatAnimal();
System.out.println("Lion age: " + lion.age);
}
}

/*
* Output:
* Lion is eating
* Animal is eating
* Lion age: 4
*/

7B. 1. Write a Java program to implement Multiple Inheritance through Interfaces

interface AnimalEat {
void eat();
}

JAVA Internals 10
interface AnimalTravel {
void travel();
}

class Animal implements AnimalEat, AnimalTravel {


public void eat() {
System.out.println("Animal is eating");
}

public void travel() {


System.out.println("Animal is travelling");
}
}

public class EX7B {


public static void main(String[] args) {
var animal = new Animal();
animal.eat();
animal.travel();
}

/*
* Output:
* Animal is eating
* Animal is travelling
*/

EX8
8A. Write a Java program to take input as String Sentence S and print largest and
shortest word in S.

public class EX8A {


public static void main(String[] args) {
String str = "This is a Java program to take input as String Sentence S and print largest and shortest word in S.";
String words[] = str.split(" ");
int len = words.length;
String max = words[0];
String min = words[len - 1];
for (String i : words) {
if (i.length() > max.length())
max = i;
if (i.length() < min.length())
min = i;
}
System.out.println("Largest Word: " + max);
System.out.println("Smallest Word: " + min);

/*
* Largest Word: Sentence
* Smallest Word: a
*/

8B. 1. Write a Java program to take input as String S and remove the consecutive
repeated characters from S. (Eg. S = Raaaamaaa then, Rama)

import java.util.Scanner;

public class EX8B {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
char words[] = str.toCharArray();
int len = words.length;

JAVA Internals 11
for (int i = 0; i < len - 1; ++i) {
if (words[i] == words[i + 1])
words[i] = '0';
}

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


if (words[i] != '0')
System.out.print(words[i]);
}
System.out.println();
sc.close();
}

/*
* Enter a string: Raaaaaaamaaaaaaaaa
* Rama
*/

EX9
9A. Write a Java program to implement Map interface.

import java.util.*;

public class EX9A {


public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(1, "One");
hm.put(2, "Two");
hm.put(3, "Three");
for (Map.Entry m : hm.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());
}

/*
* Output:
* 1 : One
* 2 : Two
* 3 : Three
*/

9B. Write a Java program to implement Set Interface.

import java.util.*;

public class EX9B {


public static void main(String[] args) {
HashSet<Integer> hs = new HashSet<Integer>();
hs.add(1);
hs.add(2);
hs.add(3);
for (Integer i : hs) {
System.out.println(i);
}
}

/*
* Output:
* 1
* 2
* 3
*/

JAVA Internals 12
9C. Write a Java program to implement Set Interface.

import java.util.*;

public class EX9B {


public static void main(String[] args) {
HashSet<Integer> hs = new HashSet<Integer>();
hs.add(1);
hs.add(2);
hs.add(3);
for (Integer i : hs) {
System.out.println(i);
}
}

/*
* Output:
* 1
* 2
* 3
*/

9D. 1. Write a Java program to implement ComparatorInterface.

import java.util.*;

class Student {
int rollno;
String name, address;

public Student(int rollno, String name, String address) {


this.rollno = rollno;
this.name = name;
this.address = address;
}

public String toString() {


return this.rollno + " " + this.name + " "
+ this.address;
}
}

class Sortbyroll implements Comparator<Student> {


public int compare(Student a, Student b) {
return a.rollno - b.rollno;
}
}

class Sortbyname implements Comparator<Student> {


public int compare(Student a, Student b) {
return a.name.compareTo(b.name);
}
}

class GFG {
public static void main(String[] args) {
ArrayList<Student> ar = new ArrayList<Student>();

ar.add(new Student(111, "Mayank", "london"));


ar.add(new Student(131, "Anshul", "nyc"));
ar.add(new Student(121, "Solanki", "jaipur"));
ar.add(new Student(101, "Aggarwal", "Hongkong"));

System.out.println("Unsorted");
for (Student s : ar) {
System.out.println(s);
}
Collections.sort(ar, new Sortbyroll());
System.out.println("Sorted by rollno");
for (Student s : ar) {
System.out.println(s);
}
Collections.sort(ar, new Sortbyname());
System.out.println("Sorted by name");
for (Student s : ar) {
System.out.println(s);

JAVA Internals 13
}
}
}

/*
Output:
Unsorted
111 Mayank london
131 Anshul nyc
121 Solanki jaipur
101 Aggarwal Hongkong
Sorted by rollno
101 Aggarwal Hongkong
111 Mayank london
121 Solanki jaipur
131 Anshul nyc
Sorted by name
101 Aggarwal Hongkong
131 Anshul nyc
111 Mayank london
121 Solanki jaipur
*/

EX10
10A. Write a Java program to read data from Employee file and print Highest salary
employee information. (Employee File Contains: ID, name, Dept., Salary).

import java.io.*;

public class EX10A {


public static void main(String args[]) throws IOException {
String line;
int sal = 0, max = 0;
String name = "", deptid = "", empid = "";
FileReader file = new FileReader("E:\\emp.txt");
BufferedReader br = new BufferedReader(file);
while ((line = br.readLine()) != null) {
String row[] = line.split("\t");
if (row[3].equals("Salary"))
continue;
sal = Integer.parseInt(row[3]);
if (sal > max) {
max = sal;
empid = row[0];
name = row[1];
deptid = row[2];
}
}
System.out.println(name + "," + deptid + "," + empid + "," + max);
}
}

/*
* 102,manager,mike,60000
*/

10B. Write a java program to implements Serializable Interface to read and write
Objects to/from the file.

import java.io.*;

class Student implements Serializable {


private String sname;
private int sno;

public void setName(String sname) {


this.sname = sname;
}

public void setNo(int sno) {


this.sno = sno;
}

JAVA Internals 14
public String getName() {
return this.sname;
}

public int getSno() {


return this.sno;
}
}

public class EX10B {


public static void main(String args[]) {
Student s = new Student();
s.setName("vinod");
s.setNo(100);
try {
FileOutputStream file = new FileOutputStream("\\E:Student.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(s);
out.close();
file.close();
System.out.println("object serialized in student.ser");
} catch (IOException io) {
System.out.println(io);
}
}
}

/*
* object serialized in student.ser
*/

EX11
11A. Write a Java program to implement try, catch, finally blocks.

public class EX11A {


public static void main(String args[]) {
try {
int a = 30, b = 0;
int c = a / b; // cannot divide by zero
System.out.println("Result = " + c);
} catch (ArithmeticException e) {
System.out.println("Can't divide a number by 0");
} finally {
}
}
}

/*
* Can't divide a number by 0
*/

11B. Write a Java program to create user defined Exception and implement throw and
throws handlers.

import java.util.Scanner;

public class EX11B extends Exception {


EX11B(String str) {
super(str);
}

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


Scanner sc = new Scanner(System.in);
System.out.println("enter voter age");
try {
int age = sc.nextInt();
if (age < 18) {
throw new EX11B("voter age less");
} else
System.out.println("valid age");
} catch (ArithmeticException a) {
System.out.println(a);
} finally {
}

JAVA Internals 15
}
}

/*
* enter voter age
* 12
* Exception in thread "main" VoterAgeLessException: voter age less
* at VoterAgeLessException.main(VoterAgeLessException.java:17)
*
* Output - 2
* enter voter age
* 18
* valid age
*/

EX12
12A. Write a Java program to create Thread using Thread Class and Runnable Interface.

import java.lang.Thread;
import java.lang.Runnable;

class MyThread1 extends Thread {


public void run() {
System.out.println("Thread-1 running");
}
}

class MyThread2 implements Runnable {


public void run() {
System.out.println("Thread-2 running");
}
}

public class EX12A {


public static void main(String args[]) {
MyThread1 t1 = new MyThread1();
t1.start();
MyThread2 m = new MyThread2();
Thread t2 = new Thread(m);
t2.start();
}
}

/*
* Thread-1 running
* Thread-2 running
*/

12B. Write a Java program to implement multi threading and synchronization. class
MovieReservation implements Runnable

class MovieReservation implements Runnable {


int ticket = 2;
int t;

MovieReservation(int t) {
this.t = t;
}

public void run() {


String name = Thread.currentThread().getName();
synchronized (this) {
if (t <= ticket) {
System.out.println(name + "Ticket booked");
ticket = ticket - 1;
} else {
System.out.println(name + " ticket not booked");
}
try {
Thread.sleep(1500);
} catch (Exception e) {
System.out.println(e);
}
}
}

JAVA Internals 16
}

public class EX12B {


public static void main(String args[]) {
MovieReservation m = new MovieReservation(1);
Thread t1 = new Thread(m);
t1.setName("ravi");
Thread t2 = new Thread(m);
t2.setName("kalyan");
Thread t3 = new Thread(m);
t3.setName("nina");
t1.start();
t2.start();
t3.start();
}
}

/*
* raviTicket booked
* kalyanTicket booked
*/

12C. Write a Java program to implement Inter Thread Communication.

class Account {
volatile int balance_amount = 15000;

public synchronized void withdraw(int amount) {


if (amount > balance_amount) {
System.out.println("wait for deposit");
try {
this.wait();
} catch (InterruptedException ie) {
}
}
balance_amount = balance_amount - amount;
System.out.println("balance amount after withdrawn=" + balance_amount);
}

public synchronized void deposit(int amount) {


balance_amount = balance_amount + amount;
System.out.println("balance amount after deposit=" + balance_amount);
this.notifyAll();
}
}

public class EX12C {


public static void main(String args[]) {
final Account acc = new Account();
new Thread() {
public void run() {
acc.withdraw(14000);
}
}.start();
new Thread() {
public void run() {
acc.withdraw(1000);
}
}.start();
new Thread() {
public void run() {
acc.deposit(10000);
}
}.start();
new Thread() {
public void run() {
acc.withdraw(2000);
}
}.start();
}
}

/*
* 14000 Rs/- Amount is withdrawn
* Balance Amount is :1000
* 1000 Rs/- Amount is withdrawn
* Balance Amount is :0
* Wait for the amount being deposited...
* Amount of Rs 10000 is deposited

JAVA Internals 17
* 1000 Rs/- Amount is withdrawn
* Balance Amount is :9000
*/

EX13

JAVA Internals 18

You might also like