Hello,
Please remember to upvote the answer if it helps you. Below is the code .only thing you need to change is provide the location to your text file.
=============================================================================================
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;
class Slogans extends Thread {
final long timeInterval = 7000;
@Override
public void run() {
//This is the simple implementation
to run the thread for every 7 sec
while (true) {
String s =
null;
try {
//Provide your text file location here.
s = choose(new
File("C:\\Working\\sts-bundle\\workspace\\springMVC4\\Chegg\\File.txt"));
System.out.println(s);
Thread.sleep(timeInterval);
} catch
(FileNotFoundException e) {
e.printStackTrace();
} catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
public String choose(File f) throws
FileNotFoundException {
String result = null;
//To pick any random lines from
file
Random rand = new Random();
int n = 0;
//Scanner is reading the file line
by line. This type of pattern is called
//RESERVIOUR_SAMPLING.
for (Scanner sc = new Scanner(f);
sc.hasNext();) {
++n;
String line =
sc.nextLine();
if
(rand.nextInt(n) == 0)
result = line;
}
return result;
}
}
=================================================================================
public class Main {
public static void main(String[] args) {
Slogans thread=new Slogans();
thread.start();
}
}
================================================================================
Hello, so I need someone to look over this code and see if im on the...
1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...
Review the following code examples and improve them (You should keep and improve the multithreading parts). Once the code files have been fixed, make them flexible in such a way that the words are provided by a user. In other words, the program asks a user to enter three sets of words which are used to list the words. public class Called { void call(String msg) { System.out.print(" [" + msg); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Interrupted"); }...
Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both the “ReadInput” and the “Count” classes. Currently these classes extends Thread. You should now use the Runnable interface to implement the thread. The output of the program should not change. I have also attached the class ThreadType2 to give you a working example on how to implement a thread using the Runnable interfacethe program is here package threadExamples; import java.util.Scanner; public class InteractiveCounting {...
Please help with Java test questions, I will post the rest of the question in separate post. Question 1 Consider the following program: public class CountDownApp3 { public static void main(String[] args) { Thread t1 = new CountDown(); Thread t2 = new CountDown(); t1.start(); t2.start(); } } Assuming that the CountDown class extends the Thread class, how many threads are used to run this program? a. 3 b. 2 c. 4 d. 1...
1. public class Threads5 { 2. public static void main (String[l args) 3. new Thread(new Runnable() 4. public void run() 5. System.out.print("bar"); 6. start(; 7 What is the result? A. Compilation fails. B. An exception is thrown at runtime. C. The code executes normally and prints "bar". D. The code executes normally, but nothing prints
Can someone modify my code so that I do not get this error: Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1043) at java.awt.Container.add(Container.java:363) at RatePanel.<init>(RatePanel.java:64) at CurrencyConverter.main(CurrencyConverter.java:16) This code should get the amount of money in US dollars from user and then let them select which currency they are trying to convert to and then in the textfield print the amount //********************************************************************* //File name: RatePanel.java //Name: Brittany Hines //Purpose: Panel for a program that convers different currencyNamerencies to use dollars //*********************************************************************...
Consider the following codes: public class TestThread extends Thread { public static void main(String[] args) { TestThread thread = new TestThread(); } @Override public void run() { printMyName(); } private void printMyName() { System.out.println("Thread is running"); } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...
Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change some syntax to make them compile and run import java.io.*; import java.lang.*; import java.util.*; class MyThreadExampleMutex{ public static void main(String[] args) { new HelloThread (1). start (); new HelloThread (2). start (); new HelloThread (3). start (); System.out.print("Global.buffer Content = "); for (int i = 0; i < 9; i++) { System.out.print(Global.buffer[i] + "; "); }} } class Global { public static int[] buffer...
I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...
Saw this code online an im trying to understand each step. Can someone add comments to this ? package javaapplication5; interface PairInterface<String>{ void setFirst(String first); void setSecond(String second); String getFirst(); String getSecond(); } class BasicPair<String> implements PairInterface{ String first; String second; BasicPair(String first,String second){ this.first=first; this.second=second; } @Override public String getFirst() { return first; } @Override public String getSecond() { return second; } @Override public void setFirst(Object first ) { this.first=(String)first; } @Override public void setSecond(Object second) { this.second=(String)second;...