北美代写cs作业 java编程代写 Java Swing 以及多线程处理

0 Comments

1. (25 points)   Implement a Java application that starts two threads at the same time. The first thread outputs a prompt and asks for a string input. As soon as the user inputs a string from keyboard and hits ENTER, the string must be encrypted with AES algorithm and passed to the second thread. The second thread decrypts the string and outputs it to the screen. The first thread meanwhile returns back to the keyboard listening mode, and this process repeats over and over. Terminate application with CTRL-C key combination. Make sure to generate a new secret key for every session and print the key (in HEX) from the first thread. The key must be printed before any output from the second thread.

Name the project Ex3_1

2. (25 points)   Design a Java Swing application with the following functionality and layout as close as possible.

The window area must consist of 3 areas: top, center, and bottom (use BorderLayout). The top and bottom areas are text fields, the top one is for input, the bottom one for output. The middle area has to have 3 buttons with the obvious functionality. The Reset button just clears the text in the top and bottom areas. Typing text in the top window and pressing one of the other buttons must lead to recomputing its digest and displaying it in the bottom text field. Set window size 500 x 120.

Hints: Check here to refresh your knowledge on Java Swing applications and here for layout managers.

Name the project Ex3_2

1. (25 points)   Design a Java "Hello World" program with the following functionality. The program should mimic distributed processing of printing the string "Hello World!" on display. For doing that the main() method just creates 5 threads and stores references to them in an array of threads, which will be passed to each thread for communication purposes:

4

Thread[] workers = new Worker[5];  // assuming class Worker extends Thread

5.

String text = "Hello, World!";

6.

for (int=0; i<5; i++)

7.

workers[i] = new Worker(workers, text);

Then the main() method starts all threads as

for (int=0; i<5; i++)
  workers[i].start();

then waits for their completion and exits. It must not control the threads in any way or send any other information to them. Each thread will compete for the display access and once it is granted, the thread prints out only one character of the string (namely the next one in order) and then starts to compete for the display access again for printing the next character. If all characters of the string are printed, the thread exits. This way each of the threads will execute the same algorithm and print just a few string characters.

You can consider printing a string character as a critical part of the thread code. Thus, you need to implement a mechanism providing mutual exclusion as described in Section 18. You are not allowed to use any synchronization approaches (semaphores, locks, etc.) described in Section 6. Each of the threads should be able to communicate with other threads by sending messages to them. The communication must be purely distributed in the sense that no other shared object is available to threads besides of the array workers specified above. The constructor of your thread class Worker (that extends Java API class Thread) must then take just two parameters, namely the array of thread objects created by the main method and the string to print:

class Worker extends Thread
{
  private Worker[] workers;
  private String text;
  public class Worker(Thread[] w, String s)
  {
    workers = (Worker[]) w;
    text = s;
  }
}

In order the threads could communicate, add a public method putMessage to the class Worker. This method should accept messages from other threads in the format of your choice and put them into, say, ArrayList. Therefore, before printing a character each thread sends messages to all other threads letting them know that it intends to print string character at index i (i=0 at the beginning) along with a timestamp (take the timestamp from the system clock). Then the threads waits for responses from other threads and upon getting them all decides what to do. Namely, it examines time stamp of its own requests for printing this character at index i with others time stamps. If its own time stamp is the smallest one, the thread enters its critical section in which it prints the character and sleeps for a random time (0- 3 sec). Otherwise, the thread understands that someone else will print this character, it goes to sleep for a random time (0-3 sec) and by waking up starts to compete for character at index i+1 until all string characters are printed.

I hope you got an idea of what is required, so if something is not specified in this description, do it on your own way. In this case include description of your approach into the source file as a comment. Important restriction is that all threads are the instances of the same class Worker, so they all run the same algorithm implemented in their run() method. No other shared object is allowed, no other parameters in the thread constructor, and thread communication must be only in terms of messages via the method putMessage.

Name the project Ex3_3

1. (25 points)   Implement a distributed Java algorithm of electing a leader in a circle ring (see Chapter 18). The main program creates 10 threads P1, P2,...,P10 and assigns a distinct positive IDk to Pk for k=1,2,...,10. After that it waits for all threads to complete and terminates. Hence, each thread only knows its own ID and is unaware how many other threads are created in main() and what are their IDs. The goal of the algorithm is that all threads will agree on the leader whose ID assigned by main() is maximum. Each of the threads should report this number and terminate right away.

To implement the election, each thread Pk can send messages only to one thread Pk+1 mod 10. Every message must have just one number in it. Upon creation Pk sends a message with IDk to Pk+1 mod 10. After that Pk starts listening for messages from Pk-1 mod 10. Upon receiving a message the thread Pk checks the number N in the message and behaves as follows:

o If N is positive and N>IDk then Pk passes N to the next thread Pk+1 mod 10.

o If N is positive and N<IDk then Pk does not pass N and waits for other messages.

o If N is positive and N=IDk then Pk negates N and passes the result to Pk+1 mod 10. Negating the number means that that thread announces itself as a leader and passes that info to other threads.

o If N is negative and |N| is different from IDk then Pk reports the absolute value of N as "Leader is N", sends N to Pk+1 mod 10 and exits.

o Finally, if N is negative and |N|=IDk, then Pk reports the absolute value of N as "Leader is N" and terminates.

Hence, each thread must report the leader once and exit.

Name the project Ex3_4

Comments