0% found this document useful (0 votes)
37 views10 pages

Java Practical Output

The document contains two Java programs: one for a scientific calculator using Swing for the GUI, and another for finding common prime and Fibonacci numbers using threads and piped streams. The scientific calculator supports basic arithmetic operations and trigonometric functions. The second program generates prime numbers and Fibonacci numbers concurrently, then identifies and prints the common numbers between the two sets.
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)
37 views10 pages

Java Practical Output

The document contains two Java programs: one for a scientific calculator using Swing for the GUI, and another for finding common prime and Fibonacci numbers using threads and piped streams. The scientific calculator supports basic arithmetic operations and trigonometric functions. The second program generates prime numbers and Fibonacci numbers concurrently, then identifies and prints the common numbers between the two sets.
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

PROGRAM

import [Link].*;
import [Link].*;
import [Link].*;
class ScientificCalculator extends JFrame implements ActionListener {
private JTextField display;
private String operator = "";
private double num1 = 0;
public ScientificCalculator() {
setTitle("Scientific Calculator");
setSize(400, 500);
setLayout(new BorderLayout());
display = new JTextField();
[Link](new Font("Arial", [Link], 24));
[Link](false);
add(display, [Link]);
JPanel panel = new JPanel();
[Link](new GridLayout(6, 4, 5, 5));
String[] buttons = {
"7","8","9","/",
"4","5","6","*",
"1","2","3","-",
"0",".","=","+",
"sin","cos","tan","log",
"sqrt","C"
};
for(String text : buttons) {
JButton btn = new JButton(text);
[Link](this);
[Link](btn);
}
add(panel, [Link]);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = [Link]();
if([Link]("[0-9.]")) {
[Link]([Link]() + command);
}
else if([Link]("[+\\-*/]")) {
num1 = [Link]([Link]());
operator = command;
[Link]("");
}
else if([Link]("=")) {
double num2 = [Link]([Link]());
double result = 0;
switch(operator) {
case "+": result = num1 + num2; break;
case "-": result = num1 - num2; break;
case "*": result = num1 * num2; break;
case "/": result = num1 / num2; break;
}
[Link]([Link](result));
}
else if([Link]("sin")) {
double value = [Link]([Link]());
[Link]([Link]([Link]([Link](value))));
}
else if([Link]("cos")) {
double value = [Link]([Link]());
[Link]([Link]([Link]([Link](value))));
}
else if([Link]("tan")) {
double value = [Link]([Link]());
[Link]([Link]([Link]([Link](value))));
}
else if([Link]("log")) {
double value = [Link]([Link]());
[Link]([Link](Math.log10(value)));
}
else if([Link]("sqrt")) {
double value = [Link]([Link]());
[Link]([Link]([Link](value)));
}
else if([Link]("C")) {
[Link]("");
}
}
public static void main(String[] args) {
new ScientificCalculator();
}
}
OUTPUT:
PROGRAM
import [Link].*;
import [Link].*;
class PrimeThread extends Thread {
private PipedOutputStream out;
public PrimeThread(PipedOutputStream out) {
[Link] = out;
}
public void run() {
try {
DataOutputStream dos = new DataOutputStream(out);
for (int i = 2; i <= 100; i++) {
if (isPrime(i)) {
[Link](i);
}
}
[Link]();
} catch (Exception e) {
[Link]();
}
}
private boolean isPrime(int n) {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
return false;
}
return true;
}
}
class FibonacciThread extends Thread {
private PipedOutputStream out;
public FibonacciThread(PipedOutputStream out) {
[Link] = out;
}
public void run() {
try {
DataOutputStream dos = new DataOutputStream(out);
int a = 0, b = 1;
while (a <= 100) {
[Link](a);
int temp = a + b;
a = b;
b = temp;
}
[Link]();
} catch (Exception e) {
[Link]();
}
}
}
class CommonPrimeFibonacci {
public static void main(String[] args) throws Exception {
PipedOutputStream primeOut = new PipedOutputStream();
PipedInputStream primeIn = new PipedInputStream(primeOut);
PipedOutputStream fibOut = new PipedOutputStream();
PipedInputStream fibIn = new PipedInputStream(fibOut);
PrimeThread primeThread = new PrimeThread(primeOut);
FibonacciThread fibThread = new FibonacciThread(fibOut);
[Link]();
[Link]();
DataInputStream primeData = new DataInputStream(primeIn);
DataInputStream fibData = new DataInputStream(fibIn);
Set<Integer> primeSet = new HashSet<>();
Set<Integer> fibSet = new HashSet<>();
try {
while (true) {
[Link]([Link]());
}
} catch (EOFException e) {}
try {
while (true) {
[Link]([Link]());
}
} catch (EOFException e) {}
[Link](fibSet);
[Link]("Common Prime and Fibonacci Numbers:");
for (int num : primeSet) {
[Link](num);
}
}
}
OUTPUT:

You might also like