In JAVA Create a PrintChar class that implements Runnable. The constructor shoul
ID: 3799447 • Letter: I
Question
In JAVA
Create a PrintChar class that implements Runnable. The constructor should accept a character and an integer as parameters. The run method should print the character the number of times indicated by the integer. Create an application that instantiates two PrintChar objects, one passed “A” and 200 and one passed “B” and 200. It then instantiates and starts two thread objects, one for each of the two PrintChar objects. Experiment with the resulting system, using different numerical parameters for the PrintChar objects. Create a report about the results of your experimentation.
Explanation / Answer
// PrintChar.java
public class PrintChar implements Runnable{
char ch;
int num;
PrintChar(char c, int n){
ch = c;
num = n;
}
public void run(){
for(int i = 0; i < num; i++){
System.out.print(ch);
}
}
}
// Test.java
public class Test{
public static void main(String args[]){
PrintChar object1 = new PrintChar('A', 200);
PrintChar object2 = new PrintChar('B', 200);
(new Thread(object1)).start();
(new Thread(object2)).start();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.