Ok Questions
Ok Questions
4. Using for and if loops generate the Prime and Fibonacci Numbers
Program:
import java.util.concurrent.*;
ExecutionException {
if (i % j == 0) {
isPrime = false;
break;
if (isPrime) {
return primeNumbers;
};
int a = 0, b = 1, c;
c = a + b;
a = b;
b = c;
return fibonacciNumbers;
};
System.out.println(primeResult.get());
System.out.println(fibonacciResult.get());
executor.shutdown();
Question - The aim of this program is to create a Java program that generates rational numbers in
reduced form by taking input from the user for the numerator and denominator values, simplifying
the fraction using the greatest common divisor, and displaying the simplified fraction as output.
Procedure Steps:
1. The program imports the java.util package, which contains the Scanner class.
4. A method named "reducedform()" is defined, which simplifies the fraction by dividing the
numerator and denominator by their greatest common divisor (GCD).
5. A method named "display()" is defined, which prints the simplified fraction (n/d).
6. In the main method, a Scanner object is created to take input from the user for the
numerator and denominator values. These values are then passed as arguments to create an object
of the Rational class.
7. The reducedform() and display() methods are called on the object, and the simplified
fraction is printed.
class Rational
int n,d;
n=x; d=y;
System.out.println("NUMERATOR="+n); System.out.println("DENOMINATOR="+d);
System.out.println("BEFORE SIMPLIFICATION="+n+"/"+d);
/* This method finds the reduce form of Numerator and Denominator value*/
public void reducedform()
if(n<d)
for(int i=2;i<=n;i++)
n=n/i; d=d/i;
else
for(int i=2;i<=d;i++)
n=n/i; d=d/i;
void display()
System.out.println("Reduced form="+n+"/"+d);
/**
* @param args
*/
public static void main(String args[])
R.display();
S.close();
PROCEDURE STEPS/ALOGRITHM :
2. Create the month year day parameters and read it from the imported file
PROGRAM :
import java.util.GregorianCalendar;
String am_pm;
if (cal.get(GregorianCalendar.AM_PM) == 0)
am_pm = "PM";
else
am_pm = "AM";
System.out.println("Current Time : " + hour + ":" + minute + ":" + second + " " +
am_pm);
System.out.println("**");
// D.MDY();
MDY();
System.out.println("**");
// D.HMS();
HMS();
}
Question - Write a Java Program to implement MULTITHREADED--PRIME AND FIBONACCI NUMBER
Procedure Steps/Algorithm:
4. Using for and if loops generate the Prime and Fibonacci Numbers
Program:
import java.util.concurrent.*;
if (i % j == 0) {
isPrime = false;
break;
if (isPrime) {
return primeNumbers;
};
int a = 0, b = 1, c;
c = a + b;
a = b;
b = c;
return fibonacciNumbers;
};
System.out.println(primeResult.get());
System.out.println(fibonacciResult.get());
executor.shutdown();
Question - Develop a MultiThreaded Echo Server using Swings and use appropriate Listener.
import java.io.*;
import java.net.*;
import javax.swing.*;
public EchoServer() {
super("Echo Server");
textArea = new JTextArea();
add(new JScrollPane(textArea));
setSize(400, 300);
setVisible(true);
try {
while (true) {
thread.start();
catch(IOException ex) {
ex.printStackTrace();
server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
server.startServer();
this.socket = socket;
try {
socket.getInputStream());
socket.getOutputStream());
catch(IOException ex) {
ex.printStackTrace();
try {
while (true) {
outputToClient.writeUTF(message);
textArea.append(message + '\n');
catch(IOException ex) {
ex.printStackTrace();
}
9. Demonstrate the concept of Threads to achieve multitasking program
using Thread class and Runnable Interface (separately) for the below
scenario:
Create an Array of 9 numbers. And create three Threads to split the task evenly
among the three threads. And each thread has to add up and report the answer to the
main thread where the main thread waits for the 3 threads and computes the
summation of all the three threads. Note: Assign names to the threads as well.
import java.util.Arrays;
3"));
thread1.start();
thread2.start();
thread3.start();
thread1.join();
thread2.join();
thread3.join();
this.numbers = numbers;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.name = name;
return sum;
@Override
sum += numbers[i];
import java.util.Arrays;
// create three threads to split the task evenly among the three threads
thread1.setName("Thread 1");
thread2.setName("Thread 2");
thread1.start();
thread2.start();
thread3.start();
try {
thread1.join();
thread2.join();
thread3.join();
} catch (InterruptedException e) {
e.printStackTrace();
this.numbers = numbers;
this.startIndex = startIndex;
this.endIndex = endIndex;
@Override
return sum;
8,Write a Java program for user defined exception that checks the internal
and external marks; if the internal marks is greater than 30 it raises the
super(message);
try {
} catch (MarksException e) {
System.out.println(e.getMessage());
}
Question - 4. In this problem, you'll create a program that guesses a secret number!
The program works as follows: you (the user) thinks of an integer between 0
(inclusive) and 100 (not inclusive). The computer makes guesses, and you give it
input - is its guess too high or too low? Using bisection search, the computer will
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low.
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low.
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low.
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low.
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low.
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low.
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low.
Your program should use bisection search. So think carefully what that means. What
will the first guess always be? How should you calculate subsequent guesses? Hint:
import java.util.Scanner;
int low = 0;
int guess;
while (true) {
System.out.println("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too
if (response == 'c') {
break;
low = guess;
high = guess;
} else {