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

Java Programs Lab

The document contains multiple Java programs demonstrating various functionalities such as creating GUI calculators, generating prime numbers, performing string operations using String and StringBuffer classes, and implementing threading. Each program is structured with classes and methods to handle specific tasks, including event handling for GUI components and asynchronous execution for threading. Overall, it showcases practical applications of Java programming concepts.

Uploaded by

krerthimugu19
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 views7 pages

Java Programs Lab

The document contains multiple Java programs demonstrating various functionalities such as creating GUI calculators, generating prime numbers, performing string operations using String and StringBuffer classes, and implementing threading. Each program is structured with classes and methods to handle specific tasks, including event handling for GUI components and asynchronous execution for threading. Overall, it showcases practical applications of Java programming concepts.

Uploaded by

krerthimugu19
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

1.

Write a calculator program using a grid layout with buttons for numbers and operations (+, -,
*, %), and display the result in a text field.
import [Link].*;
import [Link].*;
import [Link].*;

class Calc extends JFrame implements ActionListener {

JTextField t = new JTextField();


double a,b; char op;

Calc(){
add(t,[Link]);
JPanel p=new JPanel(new GridLayout(4,4));

String s[]={"7","8","9","+","4","5","6","-
","1","2","3","*","0","%","=","C"};
for(String x:s){
JButton b=new JButton(x);
[Link](this);
[Link](b);
}

add(p);
setSize(250,300);
setVisible(true);
setDefaultCloseOperation(3);
}

public void actionPerformed(ActionEvent e){


String s=[Link]();

if([Link]([Link](0))) [Link]([Link]()+s);

else if("+-*%".contains(s)){ a=[Link]([Link]());


op=[Link](0); [Link](""); }

else if([Link]("=")){
b=[Link]([Link]());
if(op=='+') [Link](""+(a+b));
if(op=='-') [Link](""+(a-b));
if(op=='*') [Link](""+(a*b));
if(op=='%') [Link](""+(a%b));
}

else [Link]("");
}

public static void main(String[] args){ new Calc(); }


}

2. Create a simple calculator with three text boxes and radio buttons for operations. Display the
result in the third text box.

)
import [Link].*;
import [Link].*;
import [Link].*;

class RadioCalc extends JFrame implements ActionListener {

JTextField t1=new JTextField(5), t2=new JTextField(5), t3=new


JTextField(5);
JRadioButton r1=new JRadioButton("+"), r2=new JRadioButton("-"),
r3=new JRadioButton("*"), r4=new JRadioButton("%");
JButton b=new JButton("Result");

RadioCalc(){
setLayout(new FlowLayout());

ButtonGroup g=new ButtonGroup();


[Link](r1); [Link](r2); [Link](r3); [Link](r4);

add(new JLabel("A")); add(t1);


add(new JLabel("B")); add(t2);
add(r1); add(r2); add(r3); add(r4);
add(b);
add(new JLabel("Result")); add(t3);

[Link](this);

setSize(250,200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e){


double a=[Link]([Link]());
double b=[Link]([Link]());
double r=0;

if([Link]()) r=a+b;
if([Link]()) r=a-b;
if([Link]()) r=a*b;
if([Link]()) r=a%b;

[Link](""+r);
}

public static void main(String args[]){


new RadioCalc();
}
}

3. Write a Java program that implements a simple GUI calculator with


three text boxes for input, operations, and result.

import [Link].*;
import [Link].*;
import [Link].*;

class Calc extends JFrame implements ActionListener {

JTextField t1=new JTextField(5), t2=new JTextField(5), t3=new


JTextField(5);
JButton b=new JButton("Calc");

Calc(){
setLayout(new FlowLayout());

add(new JLabel("A")); add(t1);


add(new JLabel("B")); add(t2);
add(b);
add(new JLabel("Result")); add(t3);

[Link](this);

setSize(250,150);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e){


int a=[Link]([Link]());
int b=[Link]([Link]());

[Link](""+(a+b)); // simple operation


}

public static void main(String args[]){


new Calc();
}
}

4. Write a Java program that prompts the user for an integer and then prints out all the prime
numbers up to that Integer

PROGRAM:

import [Link];

public class PrimeNumbers {

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

int n;

int p;

Scanner s=new Scanner([Link]);

[Link]("Enter a number: ");

n=[Link]();

[Link]("Prime numbers generated for the given input range");

for(int i=2;i<n;i++)

p=0;

for(int j=2;j<i;j++)
{

if(i%j==0)

p=1;

if(p==0)

[Link](i);

}
}
5. Write a program to perform the following string operations using String class: a) String
Concatenation b) Search a substring c) To extract substring from given string.

PROGRAM:

public class StringOperations {

public static void main(String[] args) {

String str1 = "Hello";

String str2 = "World";

String c = [Link](str2); // or str1 + str2

[Link]("Concatenated String: " + c);

String s = "World";

boolean s1 = [Link](s);

if (s1) {

[Link]("\"" + s + "\" found in concatenated string.");

} else {

[Link]("\"" + s + "\" not found in concatenated string.");

String e = [Link](0, 4); // "Hell"

[Link]("Extracted Substring: " + e);

}
}
6. Write a program to perform string operations using StringBuffer class: a) Length of a string
b) Reverse a string c) Delete a substring from the given string

PROGRAM:

import [Link];

public class Main {

public static void main(String[] args) {

Scanner s = new Scanner([Link]);

[Link]("Enter a string: ");

StringBuffer s1 = new StringBuffer([Link]());

int l = [Link]();

[Link]("Length of the string: " + l);

StringBuffer r = [Link]();

[Link]("Reversed string: " + r);

[Link]("Enter the starting index to delete: ");

int start = [Link]();

[Link]("Enter the ending index to delete: ");

int end = [Link]();

[Link](); // consume newline

if (start < 0 || end > l || start >= end) {

[Link]("Invalid indices for deletion.");

} else {

[Link](start, end);

[Link]("String after deletion: " + s1);

[Link]();

}
7. Write a threading program which uses the same method asynchronously to print the numbers 1
to 10 using Thread1 and to print 90 to 100 using Thread2.

PROGRAM:

class Thread1 extends Thread {

public void run() {

for (int i = 1; i <= 10; i++) {

[Link](i);

try {

[Link](100);

} catch (InterruptedException e) {

[Link](e);

class Thread2 extends Thread {

public void run() {

for (int i = 90; i <= 100; i++) {

[Link](i);

try {

[Link](100);

} catch (InterruptedException e) {

[Link](e);

public class ThreadDemo {

public static void main(String[] args) {


Thread1 thread1 = new Thread1();

Thread2 thread2 = new Thread2();

[Link]();

[Link]();

try {

[Link]();

[Link]();

} catch (InterruptedException e) {

[Link](e);

[Link]("Both threads have completed execution.");

}
}

You might also like