STIW3054-A182 / Main-Issues

5 stars 1 forks source link

Thread Class #3

Open zhamri opened 5 years ago

zhamri commented 5 years ago

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 ... ... ...

HENGZHIYONG commented 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();
            }
        }

}

image image

Yvevonwen commented 5 years ago
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();
            }
        }
}

image

lancelot9927 commented 5 years ago
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();
            }
        }

}
SINHUI commented 5 years ago
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();
            }
        }
    }

d dd

shyyahcheang commented 5 years ago

image

limxinyii commented 5 years ago
sc2 sc1
lishaobin commented 5 years ago
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();
            }

    }
}

1550749451

muhamaddarulhadi commented 5 years ago
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();
    }
    }
}

image

mwng97 commented 5 years ago

issue 1

TanShiJet commented 5 years ago
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();
        }
    }
}

image

najihah45 commented 5 years ago
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();
    }
}

issue1 issue1 1

mimiothman commented 5 years ago

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();
        }
    }

}

image

trinanda98 commented 5 years ago

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();
    }
}

}

screenshot 4 screenshot 3

roflinasuha commented 5 years ago

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();
    }
}

}

image

AhKitt commented 5 years ago

Code : capture11

Result : capture12

capture13

alfmrza commented 5 years ago

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();
    }
}

} 1000seq 1000seq2

ChongMeiYong commented 5 years ago

Code

thread exercise 12

Output

thread exercise 1 thread exercise 11
slyee96 commented 5 years ago

/*

/*

} image

raihanwidia commented 5 years ago

53228910-aff07100-36bd-11e9-9603-273d10e71782 53228945-cac2e580-36bd-11e9-95ed-f98dd5f14d78

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" );

}
lingzhongli commented 5 years ago

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();
    }
}    

} image

ShahifahYahya commented 5 years ago

image

rahimahsahar commented 5 years ago

image

image

porstuart commented 5 years ago

mythread

muhammadbimo1 commented 5 years ago

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();
    }
}

} ` image

ChanJunLiang commented 5 years ago

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();
    }
}

}

0 5

tanyujia commented 5 years ago

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();
    }
}

} snipaste_2019-02-24_01-24-06

fncscafred commented 5 years ago

image

image

muhamaddarulhadi commented 5 years ago
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();
    }
    }
}

image

prevenkumar commented 5 years ago

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();
            }
        }
    }
prevenkumar commented 5 years ago

output

muhamaddarulhadi commented 5 years ago
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

prevenkumar commented 5 years ago
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

muhamaddarulhadi commented 5 years ago
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

prevenkumar commented 5 years ago
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

prevenkumar commented 5 years ago
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

sonyhana7 commented 5 years ago
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();
            }

        }

image

FatihahFauzi commented 5 years ago

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();
        }
    }

}

Capture1

WongKinSin13 commented 5 years ago

MeanGenerator.Java

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());
    }

}

Output

image

image