0% found this document useful (0 votes)
2 views3 pages

Java Code Examples

The document contains several Java code examples demonstrating various programming concepts, including a synchronized bank withdrawal and deposit system, file reading and writing, custom exception handling, and a simple Java Swing application for concatenating names. Each example is structured with classes and methods to illustrate functionality. The code is intended for educational purposes, showcasing practical implementations of Java features.

Uploaded by

golurwt9720
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)
2 views3 pages

Java Code Examples

The document contains several Java code examples demonstrating various programming concepts, including a synchronized bank withdrawal and deposit system, file reading and writing, custom exception handling, and a simple Java Swing application for concatenating names. Each example is structured with classes and methods to illustrate functionality. The code is intended for educational purposes, showcasing practical implementations of Java features.

Uploaded by

golurwt9720
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
You are on page 1/ 3

Java Code Examples

Deposit and Withdraw Problem in Java


class Bank {
int balance = 1000;

synchronized void withdraw(int amount) {


System.out.println("Trying to withdraw: " + amount);
if (balance < amount) {
System.out.println("Insufficient balance, waiting for deposit...");
try {
wait();
} catch (Exception e) {
System.out.println(e);
}
}
balance -= amount;
System.out.println("Withdraw successful. Remaining balance: " + balance);
}

synchronized void deposit(int amount) {


System.out.println("Depositing: " + amount);
balance += amount;
System.out.println("Deposit successful. New balance: " + balance);
notify();
}
}

class WithdrawThread extends Thread {


Bank b;
WithdrawThread(Bank b) {
this.b = b;
}

public void run() {


b.withdraw(1500);
}
}

class DepositThread extends Thread {


Bank b;
DepositThread(Bank b) {
this.b = b;
}

public void run() {


b.deposit(1000);
}
}

class main {
public static void main(String args[]) {
Bank obj = new Bank();
WithdrawThread t1 = new WithdrawThread(obj);
DepositThread t2 = new DepositThread(obj);

t1.start();
t2.start();
}
Java Code Examples

File Printing using Scanner


import java.util.*;
import java.io.*;

class test {
public static void main(String arg[]) throws IOException {
File f = new File("this.txt");
Scanner sc = new Scanner(f);
while (sc.hasNextLine()) {
String s = sc.nextLine();
System.out.println(s);
}
sc.close();
}
}

File Writing using FileWriter


import java.util.*;
import java.io.*;

class test {
public static void main(String arg[]) throws IOException {
Scanner sc = new Scanner(System.in);
FileWriter f = new FileWriter("this.txt");

while (true) {
String s = sc.next();
if (s.equalsIgnoreCase("exit")) {
break;
}
f.write(s + "\n");
}

f.close();
}
}

Custom Exception Handling


import java.util.*;

class myexception extends Exception {


public myexception(String e) {
super(e);
}
}

public class test {


public static void main(String arg[]) {
Scanner sc = new Scanner(System.in);
try {
int a = sc.nextInt();
int b = sc.nextInt();
Java Code Examples

if (b == 0) {
throw new myexception("Are bhai ye kya kar raha hai, 0 se koi chiz kaise divide
hogi?");
}

System.out.println(a / b);
} catch (myexception e) {
System.out.println(e.getMessage());
}
}
}

Java Swing - Concatenate Names


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class demo extends JFrame {


JButton jb1;
JLabel jl1, jl2, jl3;
JTextField jf1, jf2, jf3;

demo() {
jb1 = new JButton("Concatenate");
jl1 = new JLabel("Enter first name");
jl2 = new JLabel("Enter second name");
jl3 = new JLabel("Result");
jf1 = new JTextField(20);
jf2 = new JTextField(20);
jf3 = new JTextField(20);

setLayout(new FlowLayout());
add(jl1);
add(jf1);
add(jl2);
add(jf2);
add(jl3);
add(jf3);
add(jb1);

jb1.addActionListener(new xy());
}

class xy implements ActionListener {


public void actionPerformed(ActionEvent e) {
jf3.setText(jf1.getText() + " " + jf2.getText());
}
}

public static void main(String arg[]) {


demo d = new demo();
d.setSize(300, 200);
d.setVisible(true);
d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

You might also like