Questions
How to run the following code in ecclipse IDE? MQTT PUBLISH - SUBSCRIBE MODEL CODE IN...

How to run the following code in ecclipse IDE?

MQTT PUBLISH - SUBSCRIBE MODEL CODE IN JAVA

(slide 29)public class Publisher
{
public static final String BROKER_URL = "tcp://broker.mqttdashboard.com:1883";
private MqttClient client;

public Publisher()
{

String clientId = Utils.getMacAddress() + "-pub";
try
{
client = new MqttClient(BROKER_URL, clientId);
}
catch (MqttException e)
{
e.printStackTrace();
System.exit(1);
}
}
}
//connecting client (slide 30)

MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(false);
options.setWill(client.getTopic("home/LWT"), //helps detecting failures of other clients
"I'm gone".getBytes(), 2, true);

client.connect(options);

//the busniness logic (slide 31)
public static final String TOPIC_TEMPERATURE = "home/temperature";

//...
while (true)
{
publishBrightness();
Thread.sleep(500);
publishTemperature();
Thread.sleep(500);
}
//...

private void publishTemperature() throws MqttException {
final MqttTopic temperatureTopic = client.getTopic(TOPIC_TEMPERATURE);

final int temperatureNumber = Utils.createRandomNumberBetween(20, 30);
final String temperature = temperatureNumber + "°C";

temperatureTopic.publish(new MqttMessage(temperature.getBytes()));
}

//publishBrightness() will be implemented the same way publishTemperature() is


//implementing the subscribing client

public class SubscribeCallback implements MqttCallback(slide 32)
{

@Override
public void connectionLost(Throwable cause) {}

@Override
public void messageArrived(MqttTopic topic, MqttMessage message)
{
System.out.println("Message arrived. Topic: " + topic.getName() + " Message: " + message.toString());

if ("home/LWT".equals(topic.getName()))
{
System.err.println("Sensor gone!");
}
}

@Override
public void deliveryComplete(MqttDeliveryToken token) {}

}

\\make it known to the MqttClient before connecting (slide 34)

mqttClient.setCallback(new SubscribeCallback());
mqttClient.connect();
mqttClient.subscribe("home/#");

In: Computer Science

Write a C program which adds up all the numbers between 1 and 50. Use any...

Write a C program which adds up all the numbers between 1 and 50. Use any type of loop

In: Computer Science

Write a JavaScript program to implement the Least-Squares Linear Regression algorithm shown below, for an arbitrary...

Write a JavaScript program to implement the Least-Squares Linear Regression algorithm shown below, for an arbitrary set of ( x, y ) data points entered by the user.

Least-Squares Linear Regression: • Linear regression basically means finding the ( equation for the ) straight line that is the closest “fit” to a set of ( x, y ) data points.

• Least-squares is a means of identifying the “best” fit between the line and the data. For each data point, the distance between the point and the line is calculated and squared, and the total of these squared distances is added up over all the data points. The straight line that yields the minimum value for this sum of squares is defined as the best fit.

• calculate the correlation coefficient, r, which is a metric for how well the line fits the data, and if the correlation is positive or negative. This value always lies between -1.0 and 1.0 inclusive. The closer it is to 1.0, the stronger the positive correlation; The closer to -1.0 the stronger the negative correlation; and if the coefficient is close to zero, then it shows a weak correlation ( if any ) between x and y.

The Algorithm: 1. Read in a series of ( x, y ) data pairs. While doing so, calculate the following sums:

• N = number of data pairs.

• Sx = ∑ x = Summation ( total ) of all X values.

• Sy = ∑ y = Summation ( total ) of all Y values.

• Sxx = ∑ x2 = Summation ( total ) of all X2 values.

• Syy = ∑ y2 = Summation ( total ) of all Y2 values.

• Sxy = ∑ xy = Summation ( total ) of all X∙Y values.

2. Then calculate: • Slope, m = ( N ∙ Sxy – Sx ∙ Sy ) / ( N ∙ Sxx – Sx ∙ Sx ) • Intercept, b = ( Sy – m ∙ Sx ) / N

3. Report the best-fit line as y = m x + b

4. Finally calculate and report the correlation coefficient, r, as:

• ? = ? ∙ ???−?? ∙ ?? /√( ? ∙ ???−?? ∙ ?? ) ∙ ( ? ∙ ???−?? ∙ ?? )

In: Computer Science

What's the AST(Abstract Syntax Tree) for the expression a+b*c-4 according to the following grammar, and what’s...

What's the AST(Abstract Syntax Tree) for the expression a+b*c-4 according to the following grammar, and what’s left most derivation of this expression?

expression expression + term | expression - term | term

term term * factor | term / factor | factor

factor identifier | constant | ( expression )

In: Computer Science

Solve Quadratic Equation IN JAVA (static methods, parameters, arguments, Math functions) Write a program that will...

Solve Quadratic Equation IN JAVA (static methods, parameters, arguments, Math functions)

Write a program that will print the real-valued solutions of a quadratic equation

ax^2+bx+c=0

For example:

Enter coefficient a:  1
Enter coefficient b:  10
Enter coefficient c:  2
The solutions are -0.2041684766872809, and -9.79583152331272

For another example:

Enter coefficient a:  1
Enter coefficient b:  2
Enter coefficient c:  3
The equation does not have real solution

The program should use the following methods.

  1. getCoeff(), which takes a String message as a parameter. It will print the message and returns the user's input (a double value)
  2. hasRealSolution(), which takes the three double parameters, a, b and c, and returns true if

    b^2−4ac>=0

  3. solution1() and solution2(), both take the three coefficients, a, b, and c, and returns the two solutions according to

    x = −b ± (√(b^2 − 4ac)) / (2a)

  4. main(), which prompts the user for the three coefficients, and call the other methods to find the solutions.

Notice that other than getCoeff() and the main() methods, methods should not interact with the user.

In: Computer Science

Write a program IN C that reads all integers that are in the range of 0...

Write a program IN C that reads all integers that are in the range of 0 to 100, inclusive from an input file named: a.txt and counts how many occurrences of each are in the file. After all input has been processed, display all the values with the number of occurrences that were in are in the input file.

Note: The program ignores any number less than 0 or greater than 100.

Note: Do not display zero if a number is not in the file.

Hints: An array of size 101 is good enough. A number in the file plays the role of an index.


For example: Suppose the content of the file: a.txt is as follows:
99 2 99

3

-12 80 12 33

3 99 100 1234 84


The display output is:
2 has occurred: 1 times

3 has occurred: 2 times

12 has occurred: 1 times

33 has occurred: 1 times

80 has occurred: 1 times

84 has occurred: 1 times


99 has occurred: 3 times

100 has occurred: 1 times


Note that this is just a sample dialog. Your program should work for any number of data values and you MUST match the sample dialog as well.

In: Computer Science

Show that vertex cover is in NP.

Show that vertex cover is in NP.

In: Computer Science

Consider the following CFG with starting variable S and Σ = {1, 2, 3, 4, 5,...

Consider the following CFG with starting variable S and Σ = {1, 2, 3, 4, 5, 6, 7,

8, 9, 0}:

S → X Y Z

X → 1 | 2

Y → W | ε

Z → Z Z | W

W → 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0

a. [20 marks] Create a derivation tree for your student number. - 84521004

b. [20 marks] Is this grammar ambiguous or unambiguous? Briefly explain (Remember the string is 84521004)

why.

In: Computer Science

implement the above in c++, you will write a test program named create_and_test_hash.cc . Your programs...

implement the above in c++, you will write a test program named create_and_test_hash.cc . Your programs should run from the terminal like so:
./create_and_test_hash <words file name> <query words file name> <flag> <flag> should be “quadratic” for quadratic probing, “linear” for linear probing, and “double” for double hashing. For example, you can write on the terminal:
./create_and_test_hash words.txt query_words.txt quadratic You can use the provided makefile in order to compile and test your code. Resources have been posted on how to use makefiles. For double hashing, the format will be slightly different, namely as follows:
./create_and_test_hash words.txt query_words.txt double <R VALUE> The R value should be used in your implementation of the double hashing technique discussed in class and described in the textbook: hash2 (x) = R – (x mod R). Q1. Part 1 (15 points) Modify the code provided, for quadratic and linear probing and test create_and_test_hash. Do NOT write any functionality inside the main() function within create_and_test_hash.cc. Write all functionality inside the testWrapperFunction() within that file. We will be using our own main, directly calling testWrapperFunction().This wrapper function is passed all the command line arguments as you would normally have in a main. You will print the values mentioned in part A above, followed by queried words, whether they are found, and how many probes it took to determine so. Exact deliverables and output format are described at the end of the file. Q1. Part 2 (20 points) Write code to implement double_hashing.h, and test using create_and_test_hash. This will be a variation on quadratic probing. The difference will lie in the function FindPos(), that has to now provide probes using a different strategy. As the second hash function, use the one discussed in class and found in the textbook hash2 (x) = R – (x mod R). We will test your code with our own R values. Further, please specify which R values you used for testing your program inside your README. Remember to NOT have any functionality inside the main() of create_and_test_hash.cc
You will print the current R value, the values mentioned in part A above, followed by queried words, whether they are found, and how many probes it took to determine so. Exact deliverables and output format are described at the end of the file. Q1. Part 3 (35 points) Now you are ready to implement a spell checker by using a linear or quadratic or double hashing algorithm. Given a document, your program should output all of the correctly spelled words, labeled as such, and all of the misspelled words. For each misspelled word you should provide a list of candidate corrections from the dictionary, that can be formed by applying one of the following rules to the misspelled word: a) Adding one character in any possible position b) Removing one character from the word c) Swapping adjacent characters in the word Your program should run as follows: ./spell_check <document file> <dictionary file>

You will be provided with a small document named document1_short.txt, document_1.txt, and a dictionary file with approximately 100k words named wordsEN.txt. As an example, your spell checker should correct the following mistakes. comlete -> complete (case a) deciasion -> decision (case b) lwa -> law (case c)

Correct any word that does not exist in the dictionary file provided, (even if it is correct in the English language). Some hints: 1. Note that the dictionary we provide is a subset of the actual English dictionary, as long as your spell check is logical you will get the grade. For instance, the letter “i” is not in the dictionary and the correction could be “in”, “if” or even “hi”. This is an acceptable output. 2. Also, if “Editor’s” is corrected to “editors” that is ok. (case B, remove character) 3. We suggest all punctuation at the beginning and end be removed and for all words convert the letters to lower case (for e.g. Hello! is replaced with hello, before the spell checking itself).

Do NOT write any functionality inside the main() function within spell_check.cc. Write all functionality inside the testSpellingWrapper() within that file. We will be using our own main, directly calling testSpellingWrapper(). This wrapper function is passed all the command line arguments as you would normally have in a main

In: Computer Science

C# Only Create a class named Customer that implements IComparable interface. Create 3 Customer class fields:...

C# Only

Create a class named Customer that implements IComparable interface.

Create 3 Customer class fields: Customer number, customer name, and amount due. Create automatic accessors for each field.

Create an Customer class constructor that takes parameters for all of the class fields and assigns the passed values through the accessors.

Create a default, no-argument Customer class constructor that will take no parameters and will cause default values of (9, "ZZZ", 0) to be sent to the 3-argument constructor.

Create an (override) Equals() method that determines two Customers are equal if they have the same Customer number.

Create an (override) GetHashCode() method that returns the Customer number.

Create an (override) ToString() method that returns a string containing the general Customer information (eg: CreditCustomer 1 russell AmountDue is $4,311.00 Interest rate is 0.01). Display the dollar amounts in currency format.

Implement CompareTo to compare object customer numbers for >, <, == to implement sorting for the array of objects.

Create a CreditCustomer class that derives from Customer and implements IComparable interface.

Create a class variable named Rate using an automatic accessor.

Create an CreditCustomer class constructor that takes parameters for the Customer class fields customer number, name, amount, and rate percent that sets the Rate CreditCustomer variable to the rate percentage. Pass the id number, name and amount back to the base Customer class constructor.

Create a default, no-argument CreditCustomer class constructor that will take no parameters and will cause default values of (0, "", 0, 0) to be sent to the 4-argument CreditCustomer constructor.

Create an (override) ToString() method that returns a string containing the general Customer information (eg: CreditCustomer 1 russell AmountDue is $4,311.00 Interest rate is 0.01 Monthly payment is $179.63). Display the dollar amounts in currency format.

Implement CompareTo to compare CreditCustomer objects based on customer numbers for >, <, == to implement sorting for the array of objects.

In Main:

Create an array of five CreditCustomer objects.

Prompt the user for values for each of the five Customer object; do NOT allow duplicate Customer numbers and force the user to reenter the Customer when a duplicate Customer number is entered.

CreditCustomer objects should be sorted by Customer number before they are displayed.

When the five valid Customers have been entered, display them all, display a total amount due for all Customers, display the same information again with the monthly payment for each customer. See the input/output example shown below.

Create a static GetPaymentAmounts method that will have the current Credit customer object as a parameter and returns a double value type. Each CreditCustomer monthly payment will be 1/24 of the balance (amount due). The computed monthly individual customer payment will be returned for each CreditCustomer object in the object array.

Internal Documentation.

Note that you will be overriding three object methods in the Customer class and one in the CreditCustomer class. Don't forget about IComparable.

An example of program output might look like this:

Enter customer number 3
Enter name johnson
Enter amount due 1244.50
Enter interest rate .10
Enter customer number 2
Enter name jensen
Enter amount due 543.21
Enter interest rate .15
Enter customer number 2
Sorry, the customer number 2 is a duplicate.
Please reenter
5
Enter name swenson
Enter amount due 6454.00
Enter interest rate .11
Enter customer number 1
Enter name olson
Enter amount due 435.44
Enter interest rate .20
Enter customer number 4
Enter name olafson
Enter amount due 583.88
Enter interest rate .25

Summary:

CreditCustomer 1 olson AmountDue is $435.44 Interest rate is 0.2
CreditCustomer 2 jensen AmountDue is $543.21 Interest rate is 0.15
CreditCustomer 3 johnson AmountDue is $1,244.50 Interest rate is 0.1
CreditCustomer 4 olafson AmountDue is $583.88 Interest rate is 0.25
CreditCustomer 5 swenson AmountDue is $6,454.00 Interest rate is 0.11

AmountDue for all Customers is $9,261.03

Payment Information:

CreditCustomer 1 olson AmountDue is $435.44 Interest rate is 0.2
Monthly payment is $18.14
CreditCustomer 2 jensen AmountDue is $543.21 Interest rate is 0.15
Monthly payment is $22.63
CreditCustomer 3 johnson AmountDue is $1,244.50 Interest rate is 0.1
Monthly payment is $51.85
CreditCustomer 4 olafson AmountDue is $583.88 Interest rate is 0.25
Monthly payment is $24.33
CreditCustomer 5 swenson AmountDue is $6,454.00 Interest rate is 0.11
Monthly payment is $268.92
Press any key to continue . . .


Declaring a child class:

public class Fiction : Book //for extending classes, you must use a single colon between the derived class name and its base class name
{
private:
//put your private data members here!
public:
//put your public methods here!
}

NOTE: when you instantiate an object of Fiction child class, you will inherit all the data members and methods of the Book class

In: Computer Science

The following questions are to be done in JAVA. 1) If an array is not considered...

The following questions are to be done in JAVA.

1) If an array is not considered circular, the text suggests that each remove operation must shift down every remaining element of the queue. An alternative method is to postpone shifting until rear equals the last index of the array. When that situation occurs and an attempt is made to insert an element into the queue, the entire queue is shifted down so that the first element of the queue is in the first position of the array. What are the advantages of this method over performing a shift at each remove operation? What are the disadvantages?

2) what does the following code fragment do to the queue q?

ObjectStack s = new ObjuectStack();

while (!q.isEmpty())

s.push(q.remove());

while (!s.isEmpty())

q.insert(s.pop());

3)Describe how you might implement a queue using two stacks. Hint: If you push elements onto a stack and then pop them all, they appear in reverse order. If you repeat this process, they're now back in order.

Thank you for all the help.

In: Computer Science

The college IT department manager no longer wants to use spreadsheets to calculate grades. The manager...

The college IT department manager no longer wants to use spreadsheets to calculate grades. The manager has asked you to create a program that will input the teachers' files and output the students' grades.

Write a Ruby program named format file.rb, which can be run by typing ruby widgets.rb.

In your Ruby environment, the program must read an input file formatted in CSV format, named input.csv. Each record contains data about a student and their corresponding grades.

The data will look similar to the following:

Student Name, assignment 1, assignment 2, assignment 3, assignment 4

John Adams, 90, 91, 99, 98

Paul Newman, 90, 92, 93, 94

Mary Smith, 95, 96, 99

Be careful to follow the output format exactly, including spacing. The output of your program must look like the following:

Student Assignment Average

John Adams    94.5

In: Computer Science

THIS IS IN PYTHON 3.0 Let's call the first function power(a,b). Use the built-in power function...

THIS IS IN PYTHON 3.0

Let's call the first function power(a,b). Use the built-in power function a**b in python to write a second function called testing(c) that tests if the first function is working or not.

I already have the function power(a,b), which is as follows:

def power(a, b):

    if (b == 0): return 1

    elif (int(b % 2) == 0):

        return (power(a, int(b / 2)) *

               power(a, int(b / 2)))

    else:

        return (a * power(a, int(a / 2)) *

                   power(a, int(b / 2)))

In: Computer Science

C programming Program must use FileIO to read in a file formatted like below and print...

C programming

Program must use FileIO to read in a file formatted like below and print it to the screen;

#include<stdio.h>

#include<stdio.h>

int main)int argc, char * argv[])
{
    printf("hello\n");
    return 0;
}

it then must check if there are balanced Parentheses and balanced brackets.  

If it is balanced it must display "Code is balanced" and if its not balanced it must display "code is not balanced"

In: Computer Science

Just have a quick question about IT in general. If there are any good study techniques/websites/youtube...

Just have a quick question about IT in general. If there are any good study techniques/websites/youtube videos that can help explain how to use Python on a Macbook, I could really use the help. I'm currently running OSX 10.10.5 on my Mac, and am using the Python Crash Course book by Eric Matthes to reference.

In: Computer Science