Progam

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

MID TERM – 2 PROGRAMS FOR TECHNICAL TRAINING

EXPERIMENT NO 1

Write a Java program to print name and account balance using packages

Source Code:
package mypack;
class Balance
{
String name;
double bal;
Balance(String n, double b)
{
name = n;
bal = b;
}
void show()
{
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
class AccountBalance
{
public static void main(String args[])
{
Balance current[] = new Balance[3];
current[0] = new Balance("Bhupender", 123.23);
current[1] = new Balance("Sumit", 157.02);
current[2] = new Balance("Abi", -12.33);
for(int i=0; i<3; i++) current[i].show();
}
}
Input:
Output:
EXPERIMENT NO 2

Write a program to convert all String to lowercase

Source code:
class StringLowerExample {
public static void main(String args[]) {
String s1 = "WELCOME TO JAVA”;
String lower = s1.toLowerCase();
System.out.println(lower);
}
}
Input:
Output:
EXPERIMENT NO 3

Write a java program to change the name of the current


running thread.

Source code:
class CurrentThreadDemo
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
// change the name of the thread
t.setName("My Thread");
System.out.println("After name change: " + t);
try
{
for(int n = 5; n > 0; n--)
{
System.out.println(n);
Thread.sleep(10000);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted");
}
}
}
Input:
Output:
EXPERIMENT NO 4

Write a java program to create a new thread with the help of


implementing Runnable interface.

Source code:
class NewThread implements Runnable {
Thread t;

NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}

// This is the entry point for the second thread.


public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(6000);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}

class ThreadDemo {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for (int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(5000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
Input:
Output:

EXPERIMENT NO 5

Write a java program using multiple catch blocks.

Source code:
class CatchExercise {
public static void main(String[] args) {
try {

int a[] = new int[5];


a[5] = 30 / 5;
} catch (ArithmeticException e) {

System.out.println("ArithmeticException occurs");
} catch (ArrayIndexOutOfBoundsException e) {

System.out.println(" ArrayIndexOutOfBoundsException occurs");


} catch (Exception e) {
System.out.println("Parent Exception occurs");
}
System.out.println("Rest of the code");
}
}
Input:
Output:
EXPERIMENT NO 6

Write a program using AWT create a lable,textbox and button.

Source code:
import java.awt.*;

class AWTExample2 {
AWTExample2() {
Frame f = new Frame();
Label l = new Label("Employee id:");
Button b = new Button("Submit");
TextField t = new TextField();
l.setBounds(20, 80, 80, 30);
t.setBounds(20, 100, 80, 30);
b.setBounds(100, 100, 80, 30);
f.add(b);
f.add(l);
f.add(t);
f.setSize(400, 300);
f.setTitle("Employee info");
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[]) {
AWTExample2 awt_obj = new AWTExample2();
}
}
Input:
Output:

EXPERIMENT NO 7

Write a program to show user defined exception.

Source code:
class InvalidAgeException extends Exception {
public InvalidAgeException(String str) {
super(str);
}
}

class TestCustomException1 {
static void validate(int age) throws InvalidAgeException {
if (age < 18) {

throw new InvalidAgeException("age is not valid to vote");


} else {
System.out.println("welcome to vote");
}
}

public static void main(String args[]) {


try {
validate(13);
} catch (InvalidAgeException ex) {
System.out.println("Caught the exception");
System.out.println("Exception occured: " + ex);
}
System.out.println("rest of the code...");
}
}

Input:
Output:

EXPERIMENT NO 8

Write a Program to calculate additon and substraction using


AWT textfield with ActionListener

Source code:
import java.awt.*;
import java.awt.event.*;

class TextFieldExample extends Frame implements ActionListener {


TextField tf1, tf2, tf3;
Button b1, b2;
TextFieldExample() {
tf1 = new TextField();
tf1.setBounds(50, 50, 150, 20);
tf2 = new TextField();
tf2.setBounds(50, 100, 150, 20);
tf3 = new TextField();
tf3.setBounds(50, 150, 150, 20);
tf3.setEditable(false);
b1 = new Button("+");
b1.setBounds(50, 200, 50, 50);
b2 = new Button("-");
b2.setBounds(120, 200, 50, 50);
b1.addActionListener(this);
b2.addActionListener(this);
add(tf1);
add(tf2);
add(tf3);
add(b1);
add(b2);
setSize(300, 300);
setLayout(null);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


String s1 = tf1.getText();
String s2 = tf2.getText();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
int c = 0;
if (e.getSource() == b1) {
c = a + b;
} else if (e.getSource() == b2) {
c = a - b;
}
String result = String.valueOf(c);
tf3.setText(result);
}

public static void main(String[] args) {


new TextFieldExample();
}
}

Input:
Output:
EXPERIMENT NO 9

Write a program to Draw Free image with the help of mouse .

Source code:
import java.awt.*;
import java.awt.event.*;

class MouseMotionListenerExample extends Frame implements


MouseMotionListener {
MouseMotionListenerExample() {
addMouseMotionListener(this);

setSize(300, 300);
setLayout(null);
setVisible(true);
}

public void mouseDragged(MouseEvent e) {


Graphics g = getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(), e.getY(), 20, 20);
}

public void mouseMoved(MouseEvent e) {}

public static void main(String[] args) {


new MouseMotionListenerExample();
}
}

Input:
Output:

EXPERIMENT NO 10

Write a Program to count character and word using KeyListner

Source code:
import java.awt.*;
import java.awt.event.*;

class KeyListenerExample extends Frame implements KeyListener {


Label l;
TextArea area;

KeyListenerExample() {
l = new Label();
l.setBounds(20, 50, 200, 20);
area = new TextArea();
area.setBounds(20, 80, 300, 300);
area.addKeyListener(this);

add(l);
add(area);
setSize(400, 400);
setLayout(null);
setVisible(true);
}

public void keyPressed(KeyEvent e) {


}

public void keyReleased(KeyEvent e) {


String text = area.getText();
String words[] = text.split("\\s");
l.setText("Words: " + words.length + " Characters:" + text.length());
}

public void keyTyped(KeyEvent e) {


}

public static void main(String[] args) {


new KeyListenerExample();
}
}

Input:
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy