i have problem in that program

i have problem in that program

1. Write a multi-threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number (some examples are 2, 3, 5, 13, etc.). Design a thread that generates prime numbers below 100,000 and writes them into a pipe. Design another thread that generates fibonacci numbers and writes them to another pipe. The main thread should read both the pipes to identify numbers common to both..



2. Develop a simple OPAC system for library using even-driven and concurrent programming paradigms of Java. Use JDBC to connect to a back-end database.
View Answers

September 9, 2010 at 4:39 PM

Hi Friend,

Try the following code:

import java.io.*;
import java.util.*;

class MyThread1 extends Thread{
private PipedReader pr;
private PipedWriter pw;

MyThread1(PipedReader pr, PipedWriter pw){
this.pr = pr;
this.pw = pw;
}
public void run(){
try{
int i;
for (i=1;i<10;i++){
int j;
for(j=2; j<i; j++){
int n = i%j;
if (n==0){
break;
}
}
if(i == j){
pw.write(i+"\n");
}
}
pw.close();
} catch (IOException e) {
}
}
}
class MyThread2 extends Thread{
private PipedReader pr;
private PipedWriter pw;

MyThread2(PipedReader pr, PipedWriter pw) {
this.pr = pr;
this.pw = pw;
}
public void run() {
try {
int f1, f2 = 1, f3 = 1;
for (int i = 1; i <10; i++) {
pw.write(f3+"\n");
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
pw.close();
}
catch (IOException e) {
}
}
}
class MultithreadedProgram{
public static void main(String[] args) throws Exception {
PipedWriter pw1 = new PipedWriter();
PipedReader pr1 = new PipedReader(pw1);
MyThread1 mt1 = new MyThread1(pr1, pw1);
System.out.println("Prime Numbers: ");
mt1.start();
int item1;
while ((item1 = pr1.read()) != -1){
char ch1=((char) item1);
System.out.print(Character.toString(ch1));
}
pr1.close();
PipedWriter pw2 = new PipedWriter();
PipedReader pr2 = new PipedReader(pw2);
MyThread2 mt2 = new MyThread2(pr2, pw2);
System.out.println("Fibonacci Numbers: ");
mt2.start();
int item2;
while ((item2 = pr2.read()) != -1){
char ch2=((char) item2);
System.out.print(Character.toString(ch2));
}
pr2.close();

}
}

Hope that it will be helpful for you.
Thanks









Related Tutorials/Questions & Answers:
i have problem in that program - JavaMail
i have problem in that program  1. Write a multi-threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number... = pw; } public void run(){ try{ int i; for (i=1;i<10;i
i have problem in that program sir - JavaMail
i have problem in that program sir  1.Develop multi-threaded echo server and a corresponding GUI client in Java
Advertisements
i have problem in that program to my assignment sir - JavaMail
i have problem in that program to my assignment sir   Develop a programmer's editor in Java that supports syntax-highlighting, compilation support, debugging support, etc
i have problem with classnofounderror
i have problem with classnofounderror   import java.sql.*; public class Tyagi { public static void main (String args[])throws SQLException { ResultSet rs; try { Class.forName
i have problem with classnofounderror
i have problem with classnofounderror   import java.sql.*; public class Tyagi { public static void main (String args[])throws SQLException { ResultSet rs; try { Class.forName
i have a problem in spring - Spring
i have a problem in spring  spring Aop: how to configure proxyfactorybean in xml file for providing advices for one particular class
I have problem in my Project
I have problem in my Project  Dear Sir, i have problem in my project about Jtable i have EDIT JButton whenevery i was click on edit he is display all data from database but i want to select any row
Can you see what i have done because i did it but i still have problem - SQL
Can you see what i have done because i did it but i still have problem  This is what i have done but i still have a problem:- Can you correct my mistakes? ------------------------------------------------------- create database
Hello Sir I Have problem with My Java Project - Java Beginners
Hello Sir I Have problem with My Java Project  Hello Sir I want Ur Mail Id To send U details and Project Source Code, plz Give Me Ur Mail Id
i have a problem to do this question...pls help me..
i have a problem to do this question...pls help me..  Write a program... reversedNumber = 0; for (int i = 0; i <= num; i...; reversedNumber = reversedNumber * 10 + r; i = 0
i have problem with this query... please tell me the resolution if this .........
i have problem with this query... please tell me the resolution if this .........  select length(ename)||' charecters exist in '||initcap(ename)||'s name' as "names and length" from emp
i have problem with this query... please tell me the resolution if this .........
i have problem with this query... please tell me the resolution if this .........  select initcap(ename),job from emp where substr(job,4,length(job,4,3)))='age
I have a small problem in my datagridview - Design concepts & design patterns
I have a small problem in my datagridview  i have datagridviewer in c#(platform) and i try that change cell beckground, this cell Should... the backcolor of individual cells please help me. Sorry for My English.(I am
problem i coding
problem i coding  i have a problem in coding a combobox containing a number of *.java program on click one of them that program should compile and run and display the result in other window
what is wrong in this program?I have netbeans 7.0 & jdk 7. please do corrections.
what is wrong in this program?I have netbeans 7.0 & jdk 7. please do corrections.  import java.io.*; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.List
I have crude application
I have crude application   I have crude application, how to load into this roseindia.net
i have created interface program for simple arithmetic operation but its show some error can i get reason and mistakes
i have created interface program for simple arithmetic operation but its show some error can i get reason and mistakes  import java.io.*; interface... is your code, we have modified it. import java.io.*; interface arith { int a=5
i have created interface program for simple arithmetic operation but its show some error can i get reason and mistakes
i have created interface program for simple arithmetic operation but its show some error can i get reason and mistakes  import java.io.*; interface... is your code, we have modified it. import java.io.*; interface arith { int a=5
I have need to help
I have need to help  Write a program that, for four points A, B, C and P, draws a triangle formed by ABC and a small cross showing the position of P; and displays a line of text indicating which of the following three cases
problem in servlet program
problem in servlet program  Dear Sir, I have a problem to insert the Blob type data like as video file, audio file in the database using Servlet and html code
Java I/O problem
Java I/O problem   Write a Java application that prompts the user.... The program should use the FileWriter class and an appropriate processing stream... by line, and output it to the console with the line number. The program should
i have an ques. of advanced java
i have an ques. of advanced java  write a wap to implement AWT by login form
I have doubt in jsp file.
I have doubt in jsp file.  I have doubt in jsp file. In a jsp file, there should be two buttons. One is start and other is stop. When we click on start then only stop should be enabled. Other wise it should be disabled. When
Java program problem
Java program problem  Hi, I took the chance to login here in your.... Please help me. I provided below the program assignment. Thanks in advance. Create program that: Will search through two directory structures and produce
i need program or code for this program
i need program or code for this program  out should be in this form 1 2 3 4 5 6 3 5 7 9 11 8 12 16 20 20 28 36 48 64 112
I have a doubt regarding action - Struts
I have a doubt regarding action   hi, I have doubt regarding struts,I got response through jsp and once again it redirecting to action..., Please check Action Mapping in the struts-config.xml. If you have any problem
servlet program problem - Java Beginners
servlet program problem   i used ur servlet example prg with xml file of helloworld program and i run dat program in tomcat, it shows only the files in my specified directory but when i clicked dat file name it just displaying
:( I am not getting Problem (RMI)
I am not getting Problem (RMI)  When i am excuting RMI EXAMPLE 3,2 I am getting error daying nested exception and Connect Exception  
how do i solve this problem?
how do i solve this problem?  Define a class named Circle with the following properties: List item An integer data field named radius... another class named TestCircle to test the Circle program by creating two Circle
program problem - Java Beginners
program problem  Design and write a Java application that takes as input a single letter and displays the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC 4 = GHI 6
i have no programming knowledge is it possible for study java?
i have no programming knowledge is it possible for study java?  hello sir i have no programming knowledge is it possible to study java and jsp and servlet and struts.if it is then how i schedule for this and how to study
In Java I have append to an existing string efficiently
In Java I have append to an existing string efficiently  Hi, In a Java program I have to append to a existing string more efficiently so that the performance of the application becomes maximum. What will be the best code
i need help to solve this problem
i need help to solve this problem  Write a stack class ArrayStack.java implements PureStack interface that reads in strings from standard input.... H and I join the queue h. G leaves the queue i. H and I leave the queue
I have to retrieve these data from the field table
I have to retrieve these data from the field table  Hi. I have... chennai,trichy,kanchipuram for a single record. I have to retrieve these data from... as single values like chennai as one value, trichy as one value. and i have
I have only one day to visit the Jaipur..
I have only one day to visit the Jaipur..  Hi, I have only a day to travel in Jaipur ..hence, bit worried about what to see first
depending on the form name i have to display the message
depending on the form name i have to display the message  depending on the form name i have to display the message. for example, i have 3 jsp pages like contact us,suggest us, feed back and i have to use servlet, if user clicks
software or i have to write a code for this suff
convert windows picture or fax viewer file into ms word or pdf   sir i want to convert windows picture or fax viewer file into ms word or pdf so for this is i have to take help from the java code or any software is present
Do I have to be good at math to be a data scientist?
Do I have to be good at math to be a data scientist?  Hi, I am... for the tutorials to learn: Do I have to be good at math to be a data scientist? Try... I have to be good at math to be a data scientist?". Also tell me which
Still have the same problem--First Example of Struts2 - Struts
Still have the same problem--First Example of Struts2  Hi I tried the example in the link. But still I am getting the same problem like as I... the current time. I also tried the directory structure mentioned in the above link
I/O Program output error
I/O Program output error  Hello All, I am working on a program... file, but I am getting incorrect output. I have been successfull with part of the program in that it reads the text file and analyzes it, however I need it to take
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |  import java.io.*; public class Sort{ public static void...++){ for( i=0;i<11-1;i++){ if((byte)c==(byte)first.charAt(i)) { count1
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |  import java.io.*; public class Sort{ public static void...++){ for( i=0;i<11-1;i++){ if((byte)c==(byte)first.charAt(i)) { count1
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |  import java.io.*; public class Sort{ public static void...++){ for( i=0;i<11-1;i++){ if((byte)c==(byte)first.charAt(i)) { count1
I face some problem - Java Beginners
I face some problem  I face some problem in my project plz deepak halp me how can i solve this problem i m using bean and jsp file also. and deployee the using tomcat server plz help me how can i solve thisproblem HTTP Status
write program have product - using loops
write program have product - using loops  Write a program...), name (String) and price (double). Your program should continue reading new...(list2); Collections.sort(list3); double sum=0; for(int i=0;i<
hi .. need help ASAP ..i have a project buit in eclipse , i have installed jasper. i want the steps to work on it ..
hi .. need help ASAP ..i have a project buit in eclipse , i have installed jasper. i want the steps to work on it ..   hi .. need help ASAP ..i have a project buit in eclipse , i have installed jasper. i want the steps to work
I want solution for this jsp program..
I want solution for this jsp program..   <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.sql.*" %> JSP Page <%! String
Problem in running first hibernate program.... - Hibernate
Problem in running first hibernate program....  Hi...I am using... programs.It worked fine.To run a hibernate sample program,I followed the tutorial below...(); session.close(); } } } Thanks  I have changed
which tag i have to use - Java Server Faces Questions
which tag i have to use  Hi All, I have one Question Regadding Jsf. when i clicked on one dropdown list box.when i select one item it should.... for that what tag i have to use.please help me. Thanks, Puppy.  use
Can I have multiple main() methods in the same class?
Can I have multiple main() methods in the same class?  Hi, Can I have multiple main() methods in the same class?   Hi, No, We cann't use multiple main() method within the same class. If we do this then the program

Ads