Question

Hello, so I need someone to look over this code and see if im on the...

Hello,

so I need someone to look over this code and see if im on the right path. I feel like im
missing something.

Here is the assignment:

Create a class called Slogans. This class will extend the Thread class. This thread will read from a file several quotes that are on each line. It will pick a quote randomly and display it to the console. It will change quotes every 7 seconds. This will be updated in the final project to display in a GUI.

Here are the quotes I need to display:

May the force be with you
Don't cross the streams
Live long and prosper
I'll be back
Strength and honor
Yo, Adrian
Autobots, transform and roll out
Hulk Smash!
With great power comes great responsibility
There is only one thing we say to death: not today

Here is the code:

I started off with this:

public class Main implements Runnable {
String msg;
Main(String s)
{
msg=s;
}
public static void main(String[] args) {
Thread t1 = new Thread(new Main("m"));
Thread t2 = new Thread(new Main("n"));
t1.start();
t2.start();
}

public void main(String[] args){

}

I'm sorry the language is for Java.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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();
   }
}
================================================================================

Add a comment
Know the answer?
Add Answer to:
Hello, so I need someone to look over this code and see if im on the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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....

    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)....

    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...

    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...

    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()...

    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...

    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)...

    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...

    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...

    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...

    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;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT