so in try to finish my h.w, and one thing in not go well to me yet. in this program i using 2 classes, this is a compaction of running, i just want to declare a place of the player who win the compation.
for example: we have 4 Thread. if the second Thread reach 100M last, i want to post a MSG that he came in 4th place.
class of Racer:
package assig1_2;
public class Racer extends Thread{
public static int globalid = 1;
private int id;
private int speed;
private Track track;
public Racer(int Speed,Track track) {
this.speed = Speed;
this.track = track;
this.id = this.globalid;
this.globalid++;
}
public void run() {
go ();
}
void go () {
this.track.setPriority(this.speed);
for(int i=1; i<=10; i++) {
System.out.println("runner " + this.id + " run " + i + " meters " );
if (i == 10) {
this.track.setPlace(id);
System.out.println("Runner " + this.id + " finished " + track.getPlaceNumber() + track.getPlaceName() );
}
}
}
}
class of Track:
package assig1_2;
public class Track extends Thread {
private int finishedRacers;
public String place;
public void setPlace(int numOfPlace) {
if (numOfPlace == 1) {
this.finishedRacers = numOfPlace;
this.place = "st";
} else if (numOfPlace == 2) {
this.finishedRacers = numOfPlace;
this.place = "nd";
} else if (numOfPlace == 3) {
this.finishedRacers = numOfPlace;
this.place = "rd";
} else {
this.finishedRacers = numOfPlace;
this.place = "th";
}
}
public int getPlaceNumber() {
return this.finishedRacers;
}
public String getPlaceName() {
return this.place;
}
}
its silly program in our first lesson when we use Thread.