0

My program needs 2 classes which generates Threads with random numbers in decided intervals.

  1. HeltalsGenerator defines a Thread that creates 5 randomized numbers within a decided interval, the interval gets decided when an object of the type HeltalsGenerator gets created.

  2. SlumpTal1 creates and starts 2 Threads of the type HeltalsGenerator.

  3. SlumpTal2 creates and starts an array of 4 Threads of the type HeltalsGenerator.

package thread;

class HeltalsGenerator extends Thread {

private int x;
private int y;

public HeltalsGenerator(int x, int y) {

    this.x = x;
    this.y = y;
}

@Override
public void run() {
    for (int i = 0; i < 5; i++) {
        int m = (int) (Math.random() * (x + 1) - y);
        System.out.println(m);
    }
    }
}

class SlumpTal1 {

Thread s1 = new Thread(new HeltalsGenerator(0, 9));
Thread s2 = new Thread(new HeltalsGenerator(-9, -1));

}

class SlumpTal2 {

Thread t1 = new Thread(new HeltalsGenerator(1, 4));
Thread t2 = new Thread(new HeltalsGenerator(5, 8));
Thread t3 = new Thread(new HeltalsGenerator(9, 12));
Thread t4 = new Thread(new HeltalsGenerator(13, 16));

Thread[] s = {t1, t2, t3, t4};
}

class Main {

public static void main(String[] args) {
    SlumpTal1 T1 = new SlumpTal1();
    T1.start();
    SlumpTal2 T2 = new SlumpTal2();
    T2.start();
    }
}

Both T1.start(); and T2.start(); creates the error "Cannot find symbol: method start() and variable T1 of SlumpTal1"

DuWavE
  • 13
  • 2
  • 2
    `main()` can be put in either the same class (not suggested) or a different class which actually creates and starts your threads – TheLostMind Nov 18 '15 at 10:52

1 Answers1

1

Make a new class and execute it.

Steps:

  1. Create a new file Main.java (preferrable in same package/folder than the others)
  2. Put this code on the file

public class Main {

    public static void main(String[] args) {
        // do your stuff, ask the user or what you want here, for example:
        SlumpTal1 tail1 = new SlumpTal1();  // execute 2 threads
        tail1.start();
        SlumpTal2 tail2 = new SlumpTal2();  // execute 4 threads
        tail2.start();
    }
}
  1. Execute the app

    • With your IDE (it will find automatically main method)
    • From command line.

      javac Main.java  // compile
      java Main.java   // execute
      
  2. Edit the contructor of HeltalsGenerator

    public HeltalsGenerator(int x, int y) {
        super(x + "-" + y);
    
        this.x = x;
        this.y = y;
    }
    

NOTES:

  • If all classes are NOT in same package you will need do add extra imports
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Thanks for the advice but I will need a little more guidance. When I create SlumpTal1 tail1 = new SlumpTal1(); nothing happens since I don't have a run/start command. – DuWavE Nov 18 '15 at 11:05
  • @DuWavE please kindly check my update and don't hesitate to ask if you have more doubts – Jordi Castilla Nov 18 '15 at 11:07
  • @JordiCastilla Thanks I checked out your update and I appreciate the help. I'm very new to programming so excuse my noob questions, but I created two objects in my main method and I put them in an "system.out.println" and then I just get the error "thread.SlumpTal2@2a139a55" – DuWavE Nov 18 '15 at 11:24
  • `thread.SlumpTal2@2a139a55` is the representation of the thread object, you have a print statement inside the `run` methods, so just start your threads with `start()`, check my edit – Jordi Castilla Nov 18 '15 at 11:37
  • When I write `tail1.start();` I get the error that says `Cannot find symbol: method start()`, the same with `tail2.start();`. – DuWavE Nov 18 '15 at 11:42
  • this is because you don't execute `super` in the constructor... [please check this question](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread) and my edit – Jordi Castilla Nov 18 '15 at 11:48