Progam
Progam
Progam
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
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
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
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
}
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
Source code:
class CatchExercise {
public static void main(String[] args) {
try {
System.out.println("ArithmeticException occurs");
} catch (ArrayIndexOutOfBoundsException e) {
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
Source code:
class InvalidAgeException extends Exception {
public InvalidAgeException(String str) {
super(str);
}
}
class TestCustomException1 {
static void validate(int age) throws InvalidAgeException {
if (age < 18) {
Input:
Output:
EXPERIMENT NO 8
Source code:
import java.awt.*;
import java.awt.event.*;
Input:
Output:
EXPERIMENT NO 9
Source code:
import java.awt.*;
import java.awt.event.*;
setSize(300, 300);
setLayout(null);
setVisible(true);
}
Input:
Output:
EXPERIMENT NO 10
Source code:
import java.awt.*;
import java.awt.event.*;
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);
}
Input:
Output: