STIW3054-A182 / Main-Issues

5 stars 1 forks source link

Runnable Interface #4

Open zhamri opened 5 years ago

zhamri commented 5 years ago

Instruction:

Write a Java program by implementing the Runnable interface to display 10 random numbers between 100 and 1000. Each result must be displayed every 1 second.

alfmrza commented 5 years ago
import java.util.Random;

class RandNumbers implements Runnable {

    @Override
    public void run() {
        Random random = new Random ();

        for (int i = 1; i < 11; i++) {
            System.out.println(random.nextInt((1000-100)+1)+100);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        // TODO Auto-generated method stub

    }}
    public static void main ( String args []){
        new Thread (new RandNumbers()).start();

    }

}

github

trinanda98 commented 5 years ago

package realTime;

import java.util.*;

public class MyRunnable implements Runnable {
    Random randNum = new Random();

        public static void main(String[] args) {
            new Thread(new MyRunnable()).start();
        }

        @Override
        public void run() {
            try {
                for (int x = 0; x < 10; x++) {
                    System.out.println(randNum.nextInt((1000 - 100) + 1) + 100);
                    Thread.sleep(1000);

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

untitled

muhammadbimo1 commented 5 years ago
import java.util.Random;
class MyRunnable implements Runnable {

    @Override
    public void run() {
        Random r = new Random();
        int low = 100;
        int high = 1000;
        int result = r.nextInt(high-low) + low;
        System.out.println(result);

    }

    public static void main(String args[]) {
        MyRunnable m1 = new MyRunnable();
        try {
            for(int i =1;i<=10;i++){
                Thread t1 = new Thread(m1);
                t1.start();  
                t1.sleep(1000);
            }
          } catch (Exception e) {
            e.printStackTrace();
          }

    }
}

image

lishaobin commented 5 years ago

import static java.lang.Thread.sleep;
import java.util.Random;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author 11474
 */
public class MyRunnable implements Runnable {

    @Override
    public void run() {
         try {
         for (int x = 1; x <= 10;x++) {
                 Random r =new Random();
                 int y=r.nextInt(901);
         System.out.println(y+100);
         sleep(1000);

                   }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    public static void main(String args[]) {
         new Thread(new MyRunnable()).start();
    }
}

image

mwng97 commented 5 years ago
import static java.lang.Thread.sleep;

public class Issue4 implements Runnable {

    @Override
    public void run() {
        for (int x = 0; x < 10; x++) {
            Random random = new Random();
            int j = random.nextInt(1000-100)+100 ;
            System.out.println(j);
        }
    }

    public static void main(String[] args) {
        try {
            Issue4 a = new Issue4();
            Thread t1 = new Thread(a);
            t1.start();
            sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

issue 4

lancelot9927 commented 5 years ago
import static java.lang.Thread.sleep;
import java.util.Random;

/**
 *
 * @author master lab
 */

class MyRunnable implements Runnable {

    public static void main(String args[]) {
        MyRunnable m1 = new MyRunnable();
        Thread t1 = new Thread(m1);
        t1.start();
    }

    @Override
    public void run() {
        Random rand = new Random();
            try {
                for (int y = 0; y < 10; y++) {
                    int n = rand.nextInt(901);
                    System.out.println(n+100);
                        sleep(1000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

}

image

porstuart commented 5 years ago

import java.util.Random;

class Issue4 implements Runnable {

    @Override
    public void run() {
        Random num = new Random();
        int number;

        try{
           for(int index = 1;index <= 10;index++){
              number = 100 + num.nextInt(1001 - 100);
              System.out.println(number);
              Thread.sleep(1000);
           }
        }catch(Exception e){
           e.printStackTrace();
        }
    }

    public static void main(String args[]) {
        Issue4 m1 = new Issue4();
        Thread t1 = new Thread(m1);
        t1.start();
    }
}

issue4

prevenkumar commented 5 years ago
package Week_02;
import java.util.Random;

class MyRunnable1 implements Runnable {

    @Override
    public void run() {

            Random random = new Random ();

            for (int i = 1; i < 10 ; i++) {
                System.out.println(random.nextInt((1000-100)+1)+100);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            }}
    public static void main(String args[]) {
        MyRunnable1 m1 = new MyRunnable1();
        Thread t1 = new Thread(m1);
        t1.start();
    }
}

issue4

fncscafred commented 5 years ago
package rtime;
import java.util.concurrent.ThreadLocalRandom;
class MyRunnable extends Thread {

    public static void main(String[] args) {
    new Thread(new MyRunnable()).start();
    }

    @Override
    public void run() {
        try {
            for (int a = 1; a <= 10; ++a) {
                int randomInt = ThreadLocalRandom.current().nextInt(100,1000);
                System.out.println(""+randomInt);
                sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
            }
    }
}

image

tanyujia commented 5 years ago
import static java.lang.Thread.sleep;
import java.util.Random;

public class Exercise implements Runnable {

    Random ran = new Random();

    @Override
    public void run() {
        try {
            for (int i = 1; i <= 10; i++) {
                int number = ran.nextInt(900) + 100;
                sleep(1000);
                System.out.println(number);
            }
        } catch (InterruptedException e) {

        }
    }

    public static void main(String[] args) {
        Exercise abc = new Exercise();
        abc.run();

    }

}

capture

ChongMeiYong commented 5 years ago
import static java.lang.Thread.sleep;
import java.util.Random;

@author master lab

class MyThread implements Runnable {

Random rdm = new Random();

@Override
public void run() {
    try {
        for (int i = 1; i <= 10 ; i++) {
            int num = (int) (Math.random()*901+100);
            sleep(1000);
            System.out.println(num);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String args[]) throws InterruptedException {
    MyThread m1 = new MyThread();
    m1.run();
}
}

image

limxinyii commented 5 years ago
import java.util.Random;
import static java.lang.Thread.sleep;

public class MyRunnable implements Runnable {

    @Override
    public void run() {
         try {
         for (int a = 1; a <= 10; a++) {
                 Random num =new Random();
                 int b=num.nextInt(901);
         System.out.println(b+100);
         sleep(1000);
     }
         } catch (Exception e) {
                e.printStackTrace();
         }
    }

    public static void main(String args[]) {
         new Thread(new MyRunnable()).start();
    }
}

capture

SINHUI commented 5 years ago
import java.util.Random;
import static java.lang.Thread.sleep;
public class MyRunnable implements Runnable  {

    @Override
    public void run() {

        Random number = new Random();
        int OMG;
        try {
        for(int a =1 ; a<=10 ; a++) {
            OMG= (Math.random()*901+100);

            System.out.println(OMG);
            sleep(1000);
        }

    }catch (Exception e) {
        e.printStackTrace();
    }
}

    public static void main(String args[]) {
     MyRunnable hui1 =new MyRunnable();
     hui1.run();
    }   
}

image

shyyahcheang commented 5 years ago
package myrunnable;

import static java.lang.Thread.sleep;

public class MyRunnable implements Runnable{

    @Override
    public void run() {
        try {
            for(int i=1; i<=10; ++i) {
                System.out.println(" " + getRandomInteger(100,1000));
                sleep(1000);
            }  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getRandomInteger(int max, int min) {
        return ((int)(Math.random()*(max - min))) + min;
    }

    public static void main(String[] args) {
        MyRunnable m1 = new MyRunnable();
        Thread t1 = new Thread(m1);
        t1.start();
    }
}

issue4

AhKitt commented 5 years ago

import java.util.Random;

public class RunnableEx implements Runnable{
    Random rand = new Random();

    public static void main(String[]args) {
        RunnableEx m1 = new RunnableEx();
        Thread t1 = new Thread(m1);

        t1.start();
    }

    public void run(){
        try{

                for (int i=0; i<10; i++){
                    int x = 100 + rand.nextInt(1001 - 100);
                    System.out.println(x);
                    Thread.sleep(1000);
                }
        }catch (Exception e){
            e.printStackTrace();
        }
    } 
}

run

ChanJunLiang commented 5 years ago

class Issue4 implements Runnable {

    public static void main(String args[]) {
       Issue4 ran = new Issue4();
        Thread r = new Thread(ran);
        r.start();
}
    public void run() {
        try
        {
            for(int x = 0; x < 10 ; x++)
            {
            double random = Math.random();
            random = random * (100 + (1001-100));
            int rand = (int)random;
            System.out.println(rand);
            Thread.sleep(1000);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}

issue4

slyee96 commented 5 years ago

/*

/*

HENGZHIYONG commented 5 years ago

package week02;

import java.util.Random;

public class TestingRunnableInterface implements Runnable 
{

        public void run() 
        {
            Random ran = new Random();

            try 
            {
                for(int i=1; i<=10 ; i++) 
                {
                    int n = ran.nextInt(900) + 100;
                    System.out.println(n);
                    Thread.sleep(1000);
                }
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }

        }

        public static void main(String args[]) 
        {
            TestingRunnableInterface testing1 = new TestingRunnableInterface();
            Thread thread1 = new Thread(testing1);
            thread1.start();
        }
}

image

Yvevonwen commented 5 years ago
import java.util.Random;

class Issues4 implements Runnable{
     Random random= new Random();
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Issues4 i4 = new Issues4();
        Thread thread1 = new Thread(i4);
        thread1.start();
    }

    @Override
    public void run() {
        try 
        {
            for(int m=0; m<10 ; m++) 
            {
                int result = random.nextInt(700) + 300;
                System.out.println(result);
                Thread.sleep(1000);
            }
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}

image

roflinasuha commented 5 years ago
package issues;
import java.util.Random;
class issues4Runnable implements Runnable
{
public void run()
{
      Random rand = new Random();
      for (int j = 1; j<=10; j++)
      {
          int pick = rand.nextInt(1000-100)+100;
          System.out.println(pick);

      try
      {
         Thread.sleep (1000);
      }
      catch (InterruptedException interruptedException)
      {

          System.out.println("The Thread is interrupted when it is sleeping" +interruptedException);
      }
  }
}

public static void main(String args[]) {
      new Thread (new issues4Runnable()).start();
}
}

image

TanShiJet commented 5 years ago
import java.util.Random;

class Main implements Runnable {

    @Override
    public void run() {
        Random rn = new Random();

        for(int i =0; i < 10; i++)
            {
                int answer = rn.nextInt(1000 - 100) + 100;
                System.out.println(answer);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

    }

    public static void main(String args[]) {
        Main m1 = new Main();
        Thread t1 = new Thread(m1);
        t1.start();
    }
}

image

lingzhongli commented 5 years ago
import java.util.Random;
public class Exe4 implements Runnable{

    @Override
    public void run() {
        try{
        for (int i = 0; i < 10; i++) {
    System.out.println(getRandomNumberInRange(100, 1000));
        Thread.sleep(1000);
        }
         }
        catch (Exception e) {
            e.printStackTrace();
          }
    }

    public static void main(String args[]) {
        Exe4 m1 = new Exe4();
        Thread t1 = new Thread(m1);
        t1.start();

    }

    private static int getRandomNumberInRange(int min, int max) {

    Random r = new Random();
    return r.nextInt((max - min) + 1) + min;
    }
}

image

raihanwidia commented 5 years ago
package runnableinterface;

import java.util.*;

public class RunnableInterface implements Runnable {

    Random a = new Random();
    static int rnumber;

    public static void main(String[] args) {
        new Thread(new RunnableInterface()).start();
        Thread mainThread = Thread.currentThread();

        try {
            mainThread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < 10; i++) {

            System.out.println(rnumber);

            try {
                mainThread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void run() {
        try {
            for (int i = 0; i < 10; i++) {

                rnumber = a.nextInt((1000 - 100) + 1) + 100;
                Thread.sleep(1001);

            }
        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

image

mimiothman commented 5 years ago
package test;

import java.util.Random;

public class Summing implements Runnable {

    int a;

    public Summing(int a) {
        this.a = a;
    }

    public void run() {
        addRondom();
    }

    public void addRondom() {
        Random rand = new Random();
        int n = rand.nextInt(1000) + 1;
        System.out.println("number generated: " + n);
        synchronized (this) {
            a += n;
        }
    }

    public static void main(String[] args) {
        int base = 100;

        Summing sum2 = new Summing(base);

        Thread t1 = new Thread(sum2);
        Thread t2 = new Thread(sum2);
        Thread t3 = new Thread(sum2);
        Thread t4 = new Thread(sum2);
        Thread t5 = new Thread(sum2);
        Thread t6 = new Thread(sum2);
        Thread t7 = new Thread(sum2);
        Thread t8 = new Thread(sum2);
        Thread t9 = new Thread(sum2);
        Thread t10 = new Thread(sum2);

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();

        try {
            t1.join();
            t2.join();
            t3.join();
            t4.join();
            t5.join();
            t6.start();
            t7.start();
            t8.start();
            t9.start();
            t10.start();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

       // System.out.print("final result: " + sum2.a);
    }
}

image

WongKinSin13 commented 5 years ago
public class MyRunnable implements Runnable{

    @Override
    public void run() {
        try{
            for(int x=0;x<10;x++) {
            System.out.println(intGenerator(100,1000));             
            Thread.sleep(1000);}
        }
        catch(Exception e){
            e.printStackTrace();}
    }

    public static int intGenerator(int min, int max){
           int range = (max - min) + 1;     
           return (int)(Math.random() * range) + min;
    }

    public static void main(String args[]) {
        MyRunnable m1 = new MyRunnable();
        Thread t1 = new Thread(m1);
        t1.start();
    }

image

muhamaddarulhadi commented 5 years ago
import java.util.Random;

class Random_Number implements Runnable
{
    public static void main(String[] args) 
    {
        new Thread (new Random_Number()).start();
    }
    @Override
    public void run() 
    {
        Random ran = new Random();
        int number;

        for (int i=1; i<=10; i ++)
        {
            try 
            {
                 number = 100 + ran.nextInt(1001 - 100);
                 System.out.println(number);
                 Thread.sleep(1000);
        } 
            catch (InterruptedException e) 
            {
             e.printStackTrace();
        }
        }
     }
}

image

nabilahnorizan commented 5 years ago
package issue4;
import java.util.Random;

 public class issue4 implements Runnable {
     Random rn = new Random();

     public static void main (String []args)
     {
         new Thread(new issue4()).start();
     }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        int c;
        Random t = new Random();

        // random integers in [0, 1000]

        for (c = 1; c <= 10; c++) {
          System.out.println(t.nextInt(1000));

    }

    }
}

a

FatihahFauzi commented 5 years ago

import java.util.Random;

public class isu4_Runnable implements Runnable {

    @Override
    public void run() {
        Random random = new Random ();

        for (int i = 0; i < 11; i++) {
            System.out.println(random.nextInt((1000-100)+1)+100);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // TODO Auto-generated method stub

        }}
    public static void main ( String args []){
        new Thread (new isu4_Runnable()).start();

    }

}

isu4

sonyhana7 commented 5 years ago
import java.util.Random;

     public class RunableInterface  implements Runnable{

        @Override
        public void run() {
            Random random = new Random ();

            for (int i = 1; i <=10; i++) {
                System.out.println(random.nextInt((1000-100)+1)+100);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // TODO Auto-generated method stub

            }}
        public static void main ( String args []){
            new Thread (new RunableInterface()).start();
            RunableInterface rund =new RunableInterface();
            rund.run();
        }

    }

image

rahimahsahar commented 5 years ago
package issues4;

import java.util.Random;

public class Issues4 implements Runnable {
    Random rn = new Random();

     public static void main (String []args)
     {
         new Thread(new Issues4()).start();
     }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        int c;
        Random t = new Random();

        // random integers in [0, 1000]

        for (c = 1; c <= 10; c++) {
          System.out.println(t.nextInt(1000));

    }   
}
}

issues4

ShahifahYahya commented 5 years ago
import java.util.Random;
class MyRunnable implements Runnable {

    @Override
    public void run() {
        Random r = new Random();
        int low = 100;
        int high = 1000;
        int result = r.nextInt(high-low) + low;
        System.out.println(result);

    }

    public static void main(String args[]) {
        MyRunnable m1 = new MyRunnable();
        try {
            for(int i =1;i<=10;i++){
                Thread t1 = new Thread(m1);
                t1.start();  
                t1.sleep(1000);
            }
          } catch (Exception e) {
            e.printStackTrace();
          }
    }
}

image