Java Lab List
Java Lab List
2. Write a Java program to sort for an element in a given list of elements using bubble
sort.
Solution
Public class bubble
{
public static void main(String args[])
{
int[] data = { -2, 45, 0, 11, -9 };
Int size=data.length;
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (data[j] > data[j + 1])
{
int temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
System.out.println("Sorted Array in Ascending Order:");
for(int i=0;i<size;i++)
System.out.println(data[i]+” ”);
}
}
}
3. Write a JAVA Program to demonstrate Constructor Overloading and Method
Overloading
4. Write a program in Java for String handling, the program must implement any 5
methods of String and String Buffer classes.
import java.lang.String;
public class StringMethods{
Void stringclassmethods(){
String targetString = "Java is fun to learn";
String s1= "JAVA";
String s2= "Java";
String s3 = " Hello Java ";
System.out.println("Char at index 2(third position): " + targetString.charAt(2));
System.out.println("After Concat: "+ targetString.concat("-Enjoy-"));
System.out.println("Checking equals ignoring case: " +s2.equalsIgnoreCase(s1));
System.out.println("Checking equals with case: " +s2.equals(s1));
System.out.println("Checking Length: "+ targetString.length());
System.out.println("Replace function: "+ targetString.replace("fun", "easy"));
System.out.println("SubString of targetString: "+ targetString.substring(8));
System.out.println("SubString of targetString: "+ targetString.substring(8, 12));
System.out.println("Converting to lower case: "+ targetString.toLowerCase());
System.out.println("Converting to upper case: "+ targetString.toUpperCase());
System.out.println("Triming string: " + s3.trim());
System.out.println("searching s1 in targetString: " + targetString.contains(s1));
System.out.println("searching s2 in targetString: " + targetString.contains(s2));
char [] charArray = s2.toCharArray();
System.out.println("Size of char array: " + charArray.length);
System.out.println("Printing last element of array: " + charArray[3]);
}
Void stringbuffer(){
s.setCharAt(0,'C');
int a = 007;
s.reverse();
s.reverse();
}
}
Class Demo{
public static void main(String[] args) {
StringMethods s= new StringMethods();
s.stringclassmethods();
s.stringbuffer();
}
}
7. Complete the following: a. Create a package named shape. b. Create some classes
in the package representing some common shapes like Square, Triangle, and Circle. c.
Import and compile these classes in other program.
/*Mainclass.java*/
import shape.Square;
import shape.Triangle;
import shape.Circle;
public class MainClass
{
public static void main(String args[])
{
Square square=new Square();
Triangle triangle=new Triangle();
Circle circle=new Circle();
square.getData((float)5.5);
System.out.println("The area of square is:"+square.area());
triangle.getData((double)20.56,(double)23.90);
System.out.println("The area of triangle is:"+triangle.area());
circle.getData((double)5.5);
System.out.println("The area of circle is:"+circle.area());
}
}
/*Square.java*/
package shape;
public class Square
{
float side;
public void getData(float temp)
{
side=temp;
}
public double area()
{
return (side*side);
}
}
/*Triangle.java*/
package shape;
public class Triangle
{
double base;
double height;
public void getData(double temp1,double temp2)
{
base=temp1;
height=temp2;
}
public double area()
{
return ((1.0/2.0)*(base*height));
}
}
/*Circle.java*/
package shape;
public class Circle
{
double radius;
double height;
public void getData(double temp)
{
radius=temp;
}
public double area()
{
return ((3.1427*(2.0*radius)*(2.0*radius))/4.00);
}
}
8. Write a JAVA program to implement user defined Exception Handling for withdrawal
process in a bank (also make use of throw, throws).
InSufficientFundException.java
This is Custom Exception class.In this class we have created two constructors for
displaying error message.
this.message = message;
super(cause);
this.message = message;
return message;
}
}
Account.java
In this class we have created two methods withdraw and deposit.Initial Balance
of Account is 3000 dollars.Withdraw method parameter is amount and that
amount we are subtracting from initial balance. If amount passes is greater
than initial balance, it will throw Custom Exception
InSufficientFundException,else it will calculate new balance by subtracting
amount from initial balance.
return balance;
if (amount <= 0) {
}
Test.java
Now create a test class to test our implemented Custom Exception Class.
System.out.println("Withdrawing $200");
acct.withdraw(200);
acct.withdraw(3500);
class MultiThread {
public static void main(String args[]) {
new MyThread("One");
new MyThread("Two");
new NewThread("Three");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
10. Write a JAVA applet program which read 2 numbers and displaying their sum
11. Write a JAVA Program to create a simple calculator which performs a basic
mathematical operations using java swing.
import javax.swing.*;
import java.awt.event.*;
Calc()
{
f=new JFrame("Calculator");
t=new JTextField();
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b0=new JButton("0");
bdiv=new JButton("/");
bmul=new JButton("*");
bsub=new JButton("-");
badd=new JButton("+");
bdec=new JButton(".");
beq=new JButton("=");
bclr=new JButton("Clear");
t.setBounds(30,40,280,30);
b7.setBounds(40,100,50,40);
b8.setBounds(110,100,50,40);
b9.setBounds(180,100,50,40);
bdiv.setBounds(250,100,50,40);
b4.setBounds(40,170,50,40);
b5.setBounds(110,170,50,40);
b6.setBounds(180,170,50,40);
bmul.setBounds(250,170,50,40);
b1.setBounds(40,240,50,40);
b2.setBounds(110,240,50,40);
b3.setBounds(180,240,50,40);
bsub.setBounds(250,240,50,40);
bdec.setBounds(40,310,50,40);
b0.setBounds(110,310,50,40);
beq.setBounds(180,310,50,40);
badd.setBounds(250,310,50,40);
bclr.setBounds(180,380,100,40);
f.add(t);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(bdiv);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(bmul);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(bsub);
f.add(bdec);
f.add(b0);
f.add(beq);
f.add(badd);
f.add(bclr);
f.setLayout(null);
f.setVisible(true);
f.setSize(350,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
badd.addActionListener(this);
bdiv.addActionListener(this);
bmul.addActionListener(this);
bsub.addActionListener(this);
bdec.addActionListener(this);
beq.addActionListener(this);
bclr.addActionListener(this);
}