I need JAVA help code inside
I have 2 classes but this is my main--- how do i have it so it takes 1 name at a time and displays 4999 spaces between the names? the test class is below.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Friend extends Thread //make the class
{
private String spac = " ";
private final int wait = 4999;
private final int END = 1;
private String fName;
public Friend(String name)
{
fName = name;
}
public void run()
{
for(int x = 0; x < END; ++x)
{
System.out.print(fName);
try
{
sleep(4999);
}
catch(InterruptedException e)
{
}
}
}
}
Test Class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class testFriend //make the class
{
public static void main(String[] args)
{
Friend a = new Friend(" Trista ");
Friend b = new Friend(" Nate ");
Friend c = new Friend(" Ash ");
a.start();
b.start();
c.start();
}
}
Re: i need JAVA help code inside
also when the thread is started the names should print and the ordeer will change and the amount of blank spaces will be different between the names
Re: i need JAVA help code inside
I have a small question regarding your program. What output do you want? Otherwise your code seems to be just fine. Did you tried to run your program? Do you got any error or wrong output? The only thing which I should change is replace your statement:
Code:
System.out.print(fName);
with
Code:
System.out.print(spac);
in your run().
Re: i need JAVA help code inside
what i need is the following:
1st class creates thread with a constructor that accepts the name of a friend and stores the name in an instance variable. The run() method prints 4999 blank spaces then the friends name.
create a 2nd program with a main that creates 3 friends. The names are hard coded. Start the thread to see which name comes out 1st. When you run the program several times a different friend will win by a different margin-- represented by the number of blank spaces between the names.
output might look like:
alex
pat
sam
running 2nd time may look like:
same
alex
pat
thanks for your help