Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Can someone please help me with this simple java program???

    I have an assignment to do, and I'm totally new to Java (I come from an engineering / very basic C++ background) Can someone please help me with this? I know it's something very simple to do for someone that knows Java, and I've spent hours messing with it (using Netbeans), but No matter what I do I get the error ---> Could not find the main class: ThreadExample. Program will exit.

    I know I sound like a total noob, but I would really appreciate the help. Assigment is below...


    Write a Java application which spawns at least three threads named Thread1, Thread2 and Thred3. All threads should be instances of the same class named SimpleThread. The SimpleThread class should extend the Java provided Thread class, and should contain a static variable named x, and an instance variable name y.

    class SimpleThread extends Thread {

    public static int x = 0;
    public int y = 0;

    public SimpleThread(String str) {
    super(str);
    }

    public void run() {
    Ö
    }
    }

    Inside of the run() method, each thread should enter a loop an print the threadís name and the value of x and y to the screen 10 times. Each time the values are printed to the screen, the threads should all increment the variables x and y and then sleep for a random amount of time (use Math.Random()*1000 as your sleep time) before re looping.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Can someone please help me with this simple java program???

    So, what's the code for the ThreadExample class?

    Note that it must have a properly declared main() method:

    Java Code:
    public class ThreadExample {
        public static void main(String[] args) {
            // stuff here
        }
    }

  3. #3
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: Can someone please help me with this simple java program???

    Quote Originally Posted by pbrockway2 View Post
    So, what's the code for the ThreadExample class?

    Note that it must have a properly declared main() method:

    Java Code:
    public class ThreadExample {
        public static void main(String[] args) {
            // stuff here
        }
    }

    Thanks for the reply.... So I put it how you say but I'm still getting the error....
    run:
    java.lang.VerifyError: (class: ThreadExample, method: <init> signature: ()V) Constructor must call super() or this()
    Could not find the main class: ThreadExample. Program will exit.
    Exception in thread "main" Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)



    public class ThreadExample {
    public static void main(String[] args) {

    class SimpleThread extends Thread {

    public static int x = 0;
    public int y = 0;

    public SimpleThread(String str) {
    super(str);

    }
    public void run() {

    }
    }
    }
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Can someone please help me with this simple java program???

    Don't declare SimpleThread inside ThreadExample - use twoo .java files. One for each class.

  5. #5
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: Can someone please help me with this simple java program???

    Quote Originally Posted by pbrockway2 View Post
    Don't declare SimpleThread inside ThreadExample - use twoo .java files. One for each class.
    Here is where I'm at.... probably a mess lol ... if anyone could modify it to get it right I would really appreciate it :)




    class SimpleThread extends Thread {

    public static int x = 0;
    public int y = 0;


    SimpleThread() {
    }
    SimpleThread(String threadName) {
    super(threadName);
    System.out.println(this);
    start();
    }

    }
    public class SimpleThread {

    public static void main(String[] args) {
    Thread thread1 = new Thread(new XThread(), "thread1");
    Thread thread2 = new Thread(new XThread(), "thread2");
    Thread thread3 = new Thread(new XThread(), "thread3");
    thread1.start();
    thread2.start();
    thread3.start();

    public void run() {
    System.out.println(Thread.currentThread().getName( ));
    }
    for(int i = 0; i < 10; i++)
    {
    System.out.println(x);
    System.out.println(y);
    x++;
    y++;
    this.sleep(Math.Random()*1000);
    }


    }
    }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Can someone please help me with this simple java program???

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Can someone please help me with this simple java program???

    Two classes in separate files, one called SimpleThread and the other called ThreadExample.
    You currently have two classes called the same thing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Simple java program help.
    By CGHMN in forum New To Java
    Replies: 35
    Last Post: 07-15-2011, 01:31 PM
  2. Simple java program, need help
    By cliffh in forum New To Java
    Replies: 1
    Last Post: 10-21-2010, 04:32 AM
  3. Simple Java program
    By Rolle in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 05:05 PM
  4. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 09:03 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 10:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •