For this assignment you are to write both the server and client
applications for a Knock-Knock joke system based on the Java TCP
socket client-server example that has been discussed in lectures.
The joke protocol goes like this:
Client: "Tell me a joke."
Server: "Knock knock!"
Client: "Who's there?"
Server: "Witches."
Client: "Witches who?"
Server: "Witches the way to the store."
Client: "Groan."
The Client
The client code should connect to the joke server and allow the user to type in responses, like in the above example. To initiate the joke server, an initial request line must be fed to the server. The input sentences are sent to the server and processed and responded to by the server. The client code should then print the server response and allow for the next input from the user. Once the punchline has been delivered, the user will react and the client code will terminate the connection and the application.
The Server
The server code will wait for a client to connect on the designated port. When a client connection is made and upon receipt of an acceptable initial request, the server will make an initial response of "Knock, Knock!" to the client. Your server should have at least 5 jokes stored up (pairs of names and corresponding punchlines) and randomly choose one to feed to the client when the appropriate request line is received from the client. Once the punchline is delivered, a reaction from the client is accepted and the server is then able to terminate the connection and wait for a new client request.
If the expected response is not given, an appropriate error/prompting message is to be sent back to the client app in order to get the user running the client to put in the expected next line of the joke protocol. For debugging and/or logging purposes, the server should, at least, print to the console some indication of which joke has been selected.
Example:
Server: "Knock knock!"
Client: "What do you want?"
Server: "No, no, you're supposed to say 'Who's
there?'"
In: Computer Science
Change the following code to where it adds an image(leave the image blank, I can add the image) to the question and to generate the questions and answers in a different order each time you begin the quiz. I have 10 questions but due to chegg not allowing me to post all of my code I shorted it to one question.
<!DOCTYPE html>
<html lang="en" xmlns="">
<head>
<meta charset="utf-8" />
<title>World Cup Quiz</title>
</head>
<body>
<style>
#flashcards001 {
background-color: white;
border: 4px solid black;
width: 400px;
height: 400px;
border-radius: 10px;
padding: 10px;
margin: 10px;
}
.buttons001 {
background-color: blue;
color: white;
padding: 5px;
margin: 10px;
border-radius: 10px;
width: 100px;
}
.buttons002 {
background-color: lightblue;
color: black;
padding: auto;
margin: auto;
border-radius: auto;
width:auto;
}
#text001 {
text-align: center;
}
#text002 {
text-align: left;
}
#green001 {
color: green;
}
#red001 {
color: red;
}
#img{
alight:
centerl
</style>
<div id="frame001">
<div id="text001">
<h2>World Cup Quiz</h2>
Question: <text id="number001">0</text><br
/>
Score: <text id="score001">0</text>
<hr />
<div id="message001">Click Begin to start</div><br
/>
</div>
<div id="text002">
<div id="question001"></div>
<div id="option001"></div>
<div id="option002"></div>
<div id="option003"></div>
<div id="option004"></div>
<div id="answer001"></div>
<div id="text001">
<div id="disappear001"><button class="buttons001"
onclick="begin001()">Begin</button></div>
</div>
<div id="next001"></div>
</div>
</div>
<script>
var q = ["What team is this? <br /><br />", "What team
is this?<br /><br />", "What team is this?<br
/><br />", "What team is this?<br /><br />",
"What team is this?<br /><br />", "What team is
this?<br /><br />", "What team is this?<br
/><br />", "What team is this?<br /><br />",
"What team is this?<br /><br />", "What team is
this?<br /><br />"];
var a1 = ["<button class=buttons002
onclick=q1c()>England</button>",
"<button class=buttons002
onclick=q2c()>Croatia</button>",
"<button class=buttons002
onclick=q3c()>Spain</button>",
"<button class=buttons002
onclick=q4i()>Netherlands</button>",
"<button class=buttons002
onclick=q5i()>Chile</button>",
"<button class=buttons002
onclick=q6i()>Autralia</button>",
"<button class=buttons002
onclick=q7i()>Cameroon</button>",
"<button class=buttons002
onclick=q8i()>Mexico</button>",
"<button class=buttons002
onclick=q9i()>Colombia</button>",
"<button class=buttons002
onclick=q10i()>Greece</button>"];
var a2 = ["<button class=buttons002
onclick=q1i()>Japan</button>",
"<button class=buttons002
onclick=q2i()>Greece</button>",
"<button class=buttons002
onclick=q3i()>Uruguay</button>",
"<button class=buttons002 onclick=q4c()>Costa
Rica</button>",
"<button class=buttons002
onclick=q5c()>Italy</button>",
"<button class=buttons002
onclick=q6c()>Switzerland</button>",
"<button class=buttons002
onclick=q7i()>Ecuador</button>",
"<button class=buttons002
onclick=q8i()>France</button>",
"<button class=buttons002
onclick=q9i()>Honduras</button>",
"<button class=buttons002
onclick=q10i()>Brazil</button>"];
var a3 = ["<button class=buttons002
onclick=q1i()>Argentina</button>",
"<button class=buttons002 onclick=q2i()>Bosnia and
Herzegovina</button>",
"<button class=buttons002
onclick=q3i()>Iran</button>",
"<button class=buttons002
onclick=q4i()>Nigeria</button>",
"<button class=buttons002
onclick=q5i()>Germany</button>",
"<button class=buttons002
onclick=q6i()>Ghana</button>",
"<button class=buttons002
onclick=q7c()>USA</button>",
"<button class=buttons002
onclick=q8c()>Belgium</button>",
"<button class=buttons002
onclick=q9i()>Algeria</button>",
"<button class=buttons002
onclick=q10i()>Russia</button>"];
var a4 = ["<button class=buttons002 onclick=q1i()>Kora
Republic</button>",
"<button class=buttons002
onclick=q2i()>Poland</button>",
"<button class=buttons002
onclick=q3i()>Egypt</button>",
"<button class=buttons002
onclick=q4i()>Iceland</button>",
"<button class=buttons002
onclick=q5i()>Serbia</button>",
"<button class=buttons002
onclick=q6i()>Portugal</button>",
"<button class=buttons002
onclick=q7i()>Panama</button>",
"<button class=buttons002
onclick=q8i()>Morocco</button>",
"<button class=buttons002
onclick=q9c()>Tunisia</button>",
"<button class=buttons002
onclick=q10c()>Peru</button>"];
var c = ["Correct", "Correct", "Correct", "Correct", "Correct",
"Correct", "Correct", "Correct", "Correct", "Correct"];
var i = ["Incorrect", "Incorrect", "Incorrect", "Incorrect",
"Incorrect", "Incorrect", "Incorrect", "Incorrect", "Incorrect",
"Incorrect"];
var n = 0;
n++;
var s = 0;
s++;
function begin001() {
disappear001.innerHTML = "";
message001.innerHTML = "";
question001.innerHTML = q[0];
option001.innerHTML = a1[0];
option002.innerHTML = a2[0];
option003.innerHTML = a3[0];
option004.innerHTML = a4[0];
number001.innerHTML = n++;
}
function q1c() {
answer001.innerHTML = "<div id=green001>" + c[0] +
"</div>";
option001.innerHTML = "";
option002.innerHTML = "";
option003.innerHTML = "";
option004.innerHTML = "";
next001.innerHTML = "<button class=buttons001
onclick=new002()>Next</button>";
score001.innerHTML = s++;
}
function q1i() {
answer001.innerHTML = "<div id=red001>" + i[0] +
"</div>";
option001.innerHTML = "";
option002.innerHTML = "";
option003.innerHTML = "";
option004.innerHTML = "";
next001.innerHTML = "<button class=buttons001
onclick=new002()>Next</button>";
}
function end001() {
message001.innerHTML = "End of Quiz.";
question001.innerHTML = "";
option001.innerHTML = "";
option002.innerHTML = "";
option003.innerHTML = "";
option004.innerHTML = "";
next001.innerHTML = "<div id=text001>" + "<button
class=buttons001 onclick=repeat001()>Repeat</button>" +
"</div>";
answer001.innerHTML = "";
}
function repeat001() {
location.reload();
}
</script>
</body>
</html>
In: Computer Science
Python 3.7.4
Costume = {'label': str, 'price': int, 'sizes': [str]}
''' C5. Define a function `most_expensive_costume` that consumes a list of costumes and produces a string representing the label of the costume with the highest price. In the event of a tie, give the label of the item later in the list. If there are no costumes, return the special value None. '''
''' C6. Define a function `find_last_medium` that consumes a list of costumes and produces the label of the last costume that is available in a medium. If no medium costumes are found, produce the special value `None`. '''
''' C7. Define a function `find_first_small` that consumes a list of costumes and produces the label of the first costume that is available in a small. If no small costumes are found, produce the special value `None`. '''
In: Computer Science
C Language
NO ARRAY UTILIZATION OR SORTING
Create a .txt file with 20 integers in the range of 0 to
100. There may be repeats. The numbers must
not be ordered/sorted. The task is to find and print the two
smallest numbers. You must accomplish
this task without sorting the file and without using arrays for any
purpose.
It is possible that the smallest numbers are repeated – you
should print the number of occurrences of
the two smallest numbers.
The reading of the numbers, the comparisons, the printing
of the smallest numbers should all be in
separate functions. You MUST employ PASS BY REFRENCE for this
problem. The counting of the two
smallest numbers occurrences functionality must be implemented with
static counter.
In: Computer Science
This is an assignment for python.
Download the code “
GradeBook.py
”
You will be modifying the code to do the following:
1)
create an empty dictionary named “FinalAverages”
2) In one for loop you will zip all lists together and get their individual members on each iteration. You can name
these what ever you want.
3) on each iteration: Calculate the WEIGHTED average using the weights provided, and then add a new dictionary
value to the dictionary “
FinalAverages
”. The KEY for this dictionary will be the name on this iteration, and the
VALUE will be the average of the student.
4)
the whole
dictionary,
and
then
ONLY ”Jack”
5) BONUS: if you create a function that takes an input of the 4 lists, and an output of the appropriate dictionary:
adding 1 point. Your code must then use this function to create the appropriate dictionary, apply the weights
(Store them in that function), and then output the
values you got.
Gradebook.py is:
Students = ['Bill', 'Sue', 'Janet', 'Cindy', 'Ray', 'Jack', 'Barbra', 'Matt', 'Joey', 'Adam', 'Becky', 'Mona'] Exam1 = [100, 80, 26, 45, 89, 65, 92, 75, 76, 15, 80, 50] Exam2 = [88, 95, 55, 60, 81, 25, 70, 100, 95, 70, 72, 10] Exam3 = [65, 92, 100, 80, 26, 92, 55, 60, 80, 55, 60, 75] scaleExamOne = 0.3 scaleExamTwo = 0.3 scaleExamThree = 0.4
In: Computer Science
Explain the reference count garbage collection and its disadvantages. How are the disadvantages handled?
In: Computer Science
Hello, this is for C#. Initialize variable balance to 1000 dollars. Prompt the player to enter a wager. Check that wager is less than or equal to balance, and if it’s not, have the user reenter wager until a valid wager is entered. After a correct wager is entered, run one game of craps. If the player wins, increase balance by wager and display the new balance. If the player loses, decrease balance by wager, display the new balance, check whether balance has become zero and, if so, display the message “Sorry. You busted!”
In: Computer Science
So, I have 2 Window Server 2016 Virtual Machines and I am not sure how to create 2 NICs for each server. Can someone explain how to create it?
In: Computer Science
Question: Rewrite the program in C which computes the tax due based on a tax table. Instead of using the if/else structure, use the switch statement. /* Pre : salary is predefined *Post: Returns the tax due for 0.0 <= salary <= 150,000.00; returns -1.0 if salary is outside of table range. */
double comp_tax(double salary)
{
double tax;
if (salary < 0.0)
tax = -1.0;
else if (salary < 15000.00)
tax = 0.15 * salary;
else if (salary < 30000.00)
tax = (salary - 15000.00) * 0.18 + 2250.00 ;
else if (salary < 50000.00)
tax = (salary - 30000.00) * 0.22 + 5400.00;
else if (salary < 80000.00)
tax = (salary - 50000.00) * 0.27 + 11000.00;
else if (salary < 150000.00)
tax = (salary - 80000.00) * 0.33 +21600.00;
else
tax = -1.0; return (tax);
}
In: Computer Science
You are given an array as below:
IFS=", " #set IFS to be a comma.
MYARRAY=(101,102,103,105)
Which of the following structure can be used to expand an array?
${MYARRAY[]}
${MYARRAY[?]}
${MYARRAY[@]}
${MYARRAY[!]}
Which of the following is a test statement that can be used with while or if or elif statements?
#Initialized variables are as below
i=0 #variable i initialized to be 0
[$i -lt 10]
{$i -lt 10}
($i -lt 10)
[i -lt 10]
Which of the following variable stores the first
argument given to a shell program command?
#
2
1
3
You executed a command as below on a terminal
$grep -n "hello" myfile.txt
What is the value of exit status variable "?" if the
command returns an empty output?
none
0
1
2
A variable '!' used by a program is used to store the
number of argument to a shell command.
T/F
-f is an operator to check if a regular file exist in a directory. It is used as:
if test -f myfile.txt
then
echo "file exists"
else
echo "file does not exist"
fi
True
False
In: Computer Science
Search online to find an incident of a cybercrime (not included in the readings). Please describe the event. What are the lessons learned? How can these lessons be implemented in the United States? Write a 1 to 2 page paper answering these questions.
In: Computer Science
Student
Attribute |
Data type |
(student) id |
String |
firstName |
String |
lastName |
String |
courses |
Array of Course objects |
Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table)
In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method that calculates GPA from the courses’ grades and returns it.
In: Computer Science
What is a clausal form? Explain the general format of a clausal form.
In: Computer Science
1. Define a class called Odometer that will be used to track fuel and mileage for an automobile. The class should have instance variables to track the miles driven and the fuel efficiency of the vehicle in miles per gallon. Include a mutator method to reset the odometer to zero miles, a mutator method to set the fuel efficiency, a mutator method that accepts miles driven for a trip and adds it to the odometer’s total, and an accessor method that returns the number of gallons of gasoline that the vehicle has consumed since the odometer was last reset.
Use your class with a test program that creates several trips with different fuel efficiencies. You should decide which variables should be public, if any.
2. Write a grading program for a class with the following grading policies:
There are three quizzes, each graded on the basis of 10 points.
There is one miterm exm, graded on the basis of 100 points.
There is one finl exm, graded on the basis of 100 points.
The fnal exm counts for 40% of the grade. The miterm counts for 35% of the grade. The three quizzes together count for a total of 25% of the grade. (Do not forget to convert the quiz scores to percentages before they are averaged in.)
Any grade of 90 or more is an A, any grade of 80 or more (but less than 90) is a B, any grade of 70 or more (but less than 80) is a C, any grade of 60 or more (but less than 70) is a D, and any grade below 60 is an F. The program should read in the student’s scores and output the student’s record, which consists of three quiz scores and two exm scores, as well as the student’s overall numeric score for the entire course and final letter grade.
Define and use a class for the student record. The class should have instance variables for the quizzes, midterm, final, overall numeric score for the course, and final letter grade. The overall numeric score is a number in the range 0 to 100, which represents the weighted average of the student’s work. The class should have methods to compute the overall numeric grade and the final letter grade. These last methods should be void methods that set the appropriate instance variables. Your class should have a reasonable set of accessor and mutator methods, an equals method, and a toString method, whether or not your program uses them. You may add other methods if you wish.
3. Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have four constructor methods: one for each instance variable (assume zero degrees if no value is specified and Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set to zero degrees Celsius). Include the following: (1) two accessor methods to return the temperature—one to return the degrees Celsius, the other to return the degrees Fahrenheit—use the following formulas to write the two methods, and round to the nearest tenth of a degree:
DegreesC=5(degreesF−32)/9DegreesF=(9(degreesC)/5+32);
DegreesC=5(degreesF−32)/9DegreesF=(9(degreesC)/5+32);
(2) three mutator methods: one to set the value, one to set the scale (F or C), and one to set both; (3) three comparison methods: an equals method to test whether two temperatures are equal, one method to test whether one temperature is greater than another, and one method to test whether one temperature is less than another (note that a Celsius temperature can be equal to a Fahrenheit temperature as indicated by the above formulas); and (4) a suitable toString method. Then write a driver program (or programs) that tests all the methods. Be sure to use each of the constructors, to include at least one true and one false case for each of the comparison methods, and to test at least the following temperature equalities: 0.0 degrees C = 32.0 degrees F, –40.0 degrees C = –40.0 degrees F, and 100.0 degrees C = 212.0 degrees F.
In: Computer Science
Desc: Encrypts or decrypts a file.
Input: The user supplies the character '1' to encrypt, or '2' to decrypt via the keyboard.
The user also supplies the name of the source file via the keyboard.
Output: If the user wants to encrypt, the text in input file is encrypted and the encrypted text is
stored in "encrypted.txt". The original file is not changed.
If the user wants to decrypt, the text in input file is decrypted and the decrypted text is
stored in "decrypted.txt". The original file is not changed.
-----------------------------------------------------------------------------------------------------------------------
Figure 18-1. Specifications for the Encryption case study.
Architectural design
-----------------------------------------------------------------------------------------------------------------------
print("1. Encrypt a file")
print("2. Decrypt a file")
choice=readChar()
switch (choice)
{
case '1': encrypt(); break
case '2': decrypt(); break
}
-----------------------------------------------------------------------------------------------------------------------
Figure 18-2. Pseudocode design of the main method for the Encryption case study.
Implementation
-----------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
class Encryption
{
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in);
char choice='1';
System.out.println("1. Encrypt a file");
System.out.println("2. Decrypt a file");
choice=keyboard.nextLine().charAt(0);
switch (choice)
{
case '1': encrypt(); break;
case '2': decrypt(); break;
}
}
public static void encrypt()
{
System.out.println("Method encrypt: to be completed");
}
public static void decrypt()
{
System.out.println("Method decrypt: to be completed");
}
}
-----------------------------------------------------------------------------------------------------------------------
Detailed design
-----------------------------------------------------------------------------------------------------------------------
//Desc : Encrypts a file.
//Input: The user supplies the name of a disk file via the keyboard and the file must exist.
//Output: The text in the specified file encrypted and written to "encrypted.txt".
-----------------------------------------------------------------------------------------------------------------------
Figure 18-4. The specifications for encrypt.
-----------------------------------------------------------------------------------------------------------------------
print("Enter file name")
s=readLine()
f=open(s)
g=open("encrypted.txt")
while (f.hasChar())
{
ch = f.readChar()
ch=convert(ch)
g.print(ch)
}
f.close()
g.close()
-----------------------------------------------------------------------------------------------------------------------
Figure 18-5. The pseudocode design for encrypt.
Implementation again
-----------------------------------------------------------------------------------------------------------------------
public static void encrypt() throws FileNotFoundException
{
Scanner keyboard=new Scanner(System.in);
System.out.print("Enter file name: ");
String s=keyboard.nextLine();
Scanner input = new Scanner(new File(s));
input.useDelimiter(""); //line 14
PrintWriter f = new PrintWriter("encrypted.txt");
while (input.hasNext())
{
char ch = input.next().charAt(0);
ch=convert(ch);
f.print(ch);
}
input.close();
f.close();
}
-----------------------------------------------------------------------------------------------------------------------
Figure 18-6. The code for encrypt.
Testing again
public static char convert (char ch)
{
return 'a';
}
-----------------------------------------------------------------------------------------------------------------------
//Desc : Convert a character to another secret character.
//Return: The predecessor of ch in the Unicode set.
public static char convert (char ch)
{
return (char)(ch-1);
}
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
//Desc : Decrypts a file.
//Input: The user supplies the name of a disk file via the keyboard and the file must exist.
//Output: The text in the specified file decrypted and written to "decrypted.txt".
//Throw: FileNotFoundException if the input source file does not exist, or decrypted.txt cannot be created
public static void decrypt() throws FileNotFoundException
{
Scanner keyboard=new Scanner(System.in);
char ch='a';
System.out.print("Enter file name: ");
String s=keyboard.nextLine();
Scanner input = new Scanner(new File(s));
input.useDelimiter("");
PrintWriter f = new PrintWriter("decrypted.txt");
while (input.hasNext())
{
ch = input.next().charAt(0);
ch=inverseConvert(ch);
f.print(ch);
}
input.close();
f.close();
}
//Desc : Convert a character to another character.
//Return: The successor of ch in the Unicode set.
public static char inverseConvert(char ch)
{
return (char)(ch+1);
}
-----------------------------------------------------------------------------------------------------------------------
Modify the Encryption program so that it uses the following encryption algorithm:
Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A)
Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9 to 8, 8 to 7, … 1 to 0, 0 to 9)
Everything else unchanged.
Note:
You must modify the specifications (API) of the methods involved as well.
Hand in:
Encryption.java
In: Computer Science