Open zhamri opened 5 years ago
public class ThreadExecise extends Thread {
public static void main(String[] args) {
new Thread(new ThreadExecise()).start();
}
@Override
public void run() {
try {
for (int x = 1; x < 1000; ++x) {
System.out.println(x);
sleep(500);
System.out.println(x+0.5);
sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class ThreadExercise extends Thread {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Thread(new ThreadExercise()).start();
}
@Override
public void run() {
try {
for (int y = 1; y < 1000; y++) {
System.out.println(y);
sleep(500);
System.out.println(y+0.5);
sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class javaapplication20 extends Thread {
public static void main(String[] args) {
new Thread(new javaapplication20()).start();
}
@Override
public void run() {
try {
for (int x = 1; x < 1000; x++) {
System.out.println(x);
sleep(500);
System.out.println(x+0.5);
sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Exercise1Thread extends Thread {
public static void main(String[] args) {
new Thread(new Exercise1Thread()).start();
}
@Override
public void run() {
super.run();
try {
double mean=0;
double total=0;
for ( int a = 1; a<=1000; a++) {
total= total +a;
mean=total/a;
System.out.println(mean);
sleep(500);
}
} catch (Exception n) {
n.printStackTrace();
}
}
}
package threadtest;
/**
*
* @author 11474
*/
public class Threadtest extends Thread {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Thread(new Threadtest()).start();
}
@Override
public void run(){
try {
for (int x = 1; x < 1000;x++) {
System.out.println(x);
sleep(500);
System.out.println(x+0.5);
sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package realtime_programming;
public class issues3 extends Thread
{
public static void main(String[] args)
{
new Thread(new issues3()).start();
}
@Override
public void run()
{
try
{
float sum = 0;
float mean = 0;
for (int x = 1; x <= 1000; ++x)
{
sum = sum + x;
mean = sum/1000;
System.out.println(mean);
sleep(500);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public class Main extends Thread {
public static void main(String[] args) {
new Thread(new Main()).start();
}
@Override
public void run() {
try {
double totalvalue = 0;
double mean = 0;
for (int x = 1; x < 1000; x++) {
System.out.println(x);
sleep(500);
System.out.println(x+0.5);
sleep(500);
totalvalue +=(x+x+0.5);
mean = totalvalue/(2*x);
}
System.out.println("mean : "+mean);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MyThread extends Thread {
@Override
public void run() {
try {
for (int x = 1; x <=1000; x++) {
System.out.println(x);
sleep(500);
System.out.println(x+0.5);
sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
MyThread t1 = new MyThread();
t1.start();
}
}
public class ThreadIssue extends Thread { public static void main(String[] args) { new Thread(new ThreadIssue()).start(); }
@Override
public void run() {
try {
for (int x = 1; x < 1000; ++x) {
System.out.println(x);
sleep(800);
System.out.println(x+0.5);
sleep(800);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package realTime;
public class Issue1 extends Thread {
public static void main(String[] args) {
new Thread(new Issue1()).start();
}
@Override
public void run() {
try {
double mean = 0;
double sum = 0;
for (int x = 1; x <= 1000; x++) {
System.out.println(x);
sleep(500);
if(x != 1000) {
System.out.println(x + 0.5);
sleep(500);}
sum += (x + x + 0.5);
mean = sum/(2*x);
}
System.out.println("Sum = " + sum);
System.out.println("Mean = " + mean);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class ThreadIssue3 extends Thread { public static void main(String[] args) { new Thread(new ThreadIssue3()).start(); }
@Override
public void run() {
try {
for (int x = 1; x < 1000; ++x) {
System.out.println(x);
sleep(500);
System.out.println(x+0.5);
sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Code :
Result :
package RealTimeProg;
public class ExercOne extends Thread {
public static void main(String[] args) {
new Thread(new ExercOne()).start();
}
@Override
public void run() {
try {
double mean = 0;
double total = 0;
for (int x = 1; x <= 500; x++) {
System.out.println(x);
sleep(500);
System.out.println(x + 0.5);
sleep(500);
total += (x + x + 0.5);
mean = total / (2 * x);
}
System.out.println("The mean of 1000 sequence numbers = " + mean);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Code
Output
/*
/*
@author Lai Yee */ public class ThreadClassExercise extends Thread{
/**
public void run(){ try{ for (int x =1; x<1000;++x){ System.out.println(x); sleep(500); System.out.println(x+0.5); sleep(500); } }catch (Exception a){ a.printStackTrace(); } }
}
public void run() {
try {
for (int x = 1; x < 501; x++) {
System.out.println(x);
sum += x;
System.out.println(" Total number now : "+sum);
count++;
sleep(500);
System.out.println(x+0.5);
sum += x+0.5;
System.out.println(" Total number now : "+sum);
count++;
sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(" the mean of these sequence numbers above is : " + sum/count +" with "+count+" of sequence number" );
}
public class ThreadExecrise extends Thread { public static void main(String[] args) { new Thread(new ThreadExecrise()).start(); }
@Override
public void run() {
try {
for (int x = 1; x < 1000; x++) {
System.out.println(x);
sleep(500);
System.out.println(x+0.5);
sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class exercise extends Thread {
public static void main(String[] args) {
new Thread(new exercise()).start();
}
@Override
public void run() {
try {
double var=1;
System.out.println(var);
sleep(500);
for (int i = 0; i <= 1000; i++) {
var = var+0.5;
System.out.println(var);
sleep(500);
}
double mean = var/(1000);
System.out.println("Mean: "+mean);
} catch (Exception e) {
e.printStackTrace();
}
}
}
`
public class Issue3 extends Thread {
public static void main(String[] args) {
new Thread(new Issue3()).start();
}
@Override
public void run() {
try {
for (int x = 0; x < 1000; x++) {
System.out.println(x);
sleep(500);
System.out.println(x+0.5);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Example extends Thread {
public static void main(String[] args) {
new Thread(new Example()).start();
}
@Override
public void run() {
try {
double y = 2;
for (int x = 1; x < 1000; x++) {
System.out.println(x);
if (x < 1000) {
double mean = (x + y) / 2;
System.out.println(mean);
}
y++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package realtime_programming;
public class issues3 extends Thread
{
public static void main(String[] args)
{
new Thread(new issues3()).start();
}
@Override
public void run()
{
try
{
double sum = 0;
double mean = 0;
for (int x = 1; x <= 1000; ++x)
{
sum = sum + x;
mean = sum/x;
System.out.println(mean);
sleep(500);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
package thread1;
public class Thread1 extends Thread {
public static void main(String[] args) {
// TODO code application logic here
new Thread(new Thread1()).start();
}
@Override
public void run(){
try {
float sum = 2;
for (int i = 1; i < 1000; i++) {
System.out.println(i);
if (i < 1000) {
float mean = (i + sum) / 2;
System.out.println(mean);
sleep(500);
}sum++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package thread1; public class Thread1 extends Thread { public static void main(String[] args) { // TODO code application logic here new Thread(new Thread1()).start(); } @Override public void run(){ try { for (int i = 1; i < 1000; i++) { System.out.println(i); sleep(500); System.out.println(i+0.5); sleep(500); } } catch (Exception e) { e.printStackTrace(); } } }
sir said that this code is wrong "System.out.println(i+0.5);". He does not want the code like that
package thread1; public class Thread1 extends Thread { public static void main(String[] args) { // TODO code application logic here new Thread(new Thread1()).start(); } @Override public void run(){ try { for (int i = 1; i < 1000; i++) { System.out.println(i); sleep(500); System.out.println(i+0.5); sleep(500); } } catch (Exception e) { e.printStackTrace(); } } }
sir said that this code is wrong "System.out.println(i+0.5);" im not sure but the output is same :-D
package thread1; public class Thread1 extends Thread { public static void main(String[] args) { // TODO code application logic here new Thread(new Thread1()).start(); } @Override public void run(){ try { for (int i = 1; i < 1000; i++) { System.out.println(i); sleep(500); System.out.println(i+0.5); sleep(500); } } catch (Exception e) { e.printStackTrace(); } } }
sir said that this code is wrong "System.out.println(i+0.5);" im not sure but the output is same :-D
i also not sure about the code, hmmmm
package thread1; public class Thread1 extends Thread { public static void main(String[] args) { // TODO code application logic here new Thread(new Thread1()).start(); } @Override public void run(){ try { for (int i = 1; i < 1000; i++) { System.out.println(i); sleep(500); System.out.println(i+0.5); sleep(500); } } catch (Exception e) { e.printStackTrace(); } } }
sir said that this code is wrong "System.out.println(i+0.5);" im not sure but the output is same :-D
i also not sure about the code, hmmmm im trying to amend it now... tq hadi
package thread1; public class Thread1 extends Thread { public static void main(String[] args) { // TODO code application logic here new Thread(new Thread1()).start(); } @Override public void run(){ try { for (int i = 1; i < 1000; i++) { System.out.println(i); sleep(500); System.out.println(i+0.5); sleep(500); } } catch (Exception e) { e.printStackTrace(); } } }
sir said that this code is wrong "System.out.println(i+0.5);" im not sure but the output is same :-D
i also not sure about the code, hmmmm im trying to amend it now... tq hadi
edited
public class MyThreadThread extends Thread {
double sum =0;
double mean= 0;
public static void main(String[] args) {
new Thread(new MyThreadThread()).start();
}
@Override
public void run() {
try {
for (int x = 1; x <= 1000; ++x) {
System.out.println(x);
sleep (500);
System.out.println(x+0.5);
sum+=0.5 + x+x;
mean = sum/(1000);
sleep(500);
}System.out.print("The mean value is: "+mean);
}
catch (Exception e) {
e.printStackTrace();
}
}
public class Isu3_Thread extends Thread {
public static void main(String[] args) {
new Thread(new Isu3_Thread ()).start();
}
@Override
public void run() {
try {
float mean;
int sum, i;
int n = 2;
int a[] = {1,0};
sum = 0;
for(i = 0; i < n; i++) {
sum += a[i];
}
System.out.println("Mean :" + sum /(float) n );
for (int x = 1; x < 1000; ++x){
System.out.println(x);
sleep(100);
System.out.println(x + 0.5);
sleep(100);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.*;
import static com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type.Int;
import static java.lang.Thread.sleep;
public class MeanGenerator extends Thread{
@Override
public void run(){
double [] rand = new double[1001];
for(int x=0;x<rand.length;x++){
rand[x]=x;
}
try{
for(int x=0;x<=rand.length;x++){
double num1 = rand[x];
double num2 = rand[x+1];
System.out.printf("\nMean of index %d+%d is %.1f",x ,x+1 , (num1+num2)/ 2);
sleep(500);
}
System.out.println("");
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[]args) throws InterruptedException {
MeanGenerator m1 = new MeanGenerator();
Thread t1 = new Thread(m1);
t1.start();
//System.out.println(Thread.currentThread().getId()+" "+t1.getName()+" "+t1.getState()+" "+t1.getPriority()+" "+t1.isDaemon());
}
}
Instruction:
Write a Java program by extending the
Thread
class to calculate the mean value of 1000 sequence numbers starting from 1. Each result must be displayed every 0.5 second.Example of the output:
1 1.5 2 2.5 3 3.5 4 ... ... ...