Write down a Java program that calculates the Merkle root for the following dataset:
7246
9716
2017
6329
2831
1171
5690
7739
9463
8363
Java Program to Calculate Merkel Root from a dataset:
Merkel Root : Merkel Root is the final created hash from the input dataset, it is a Blockchain technology and is used to summarize all the transactions in a block. Currently used with Bitcoin it produces overall digital fingerprint of the entire set of transactions, and provide a very efficient process for verifying if a transaction is included in a block or not.
/*Program starts here*/
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
public class MerkleTrees {
// Dataset list for capturing the data inputs
List<String> txnList;
// Merkle Root Variable ( String Type )
String root;
//Constructor
public MerkleTrees(List<String> txnList) {
this.txnList = txnList;
root = "";
}
//Merkle_tree execution with void return type used to set
root.
public void merkle_tree() {
List<String> tempTxnList = new
ArrayList<String>();
//Loop for capturing the dataset
for (int i = 0; i < this.txnList.size(); i++) {
tempTxnList.add(this.txnList.get(i));
}
List<String> newTxnList = getNewTxnList(tempTxnList);
while (newTxnList.size() != 1) {
newTxnList = getNewTxnList(newTxnList);
}
this.root = newTxnList.get(0);
}
/Return Node Hash List, Private access specifier.
private List<String> getNewTxnList(List<String>
tempTxnList) {
List<String> newTxnList = new
ArrayList<String>();
int index = 0;
while (index < tempTxnList.size()) {
// left
String left = tempTxnList.get(index);
index++;
// right
String right = "";
if (index != tempTxnList.size()) {
right = tempTxnList.get(index);
}
// sha2 hex value
String sha2HexValue = getSHA2HexValue(left + right);
newTxnList.add(sha2HexValue);
index++;
}
return newTxnList;
}
//Returning hex string
public String getSHA2HexValue(String str) {
byte[] cipher_byte;
//Exception handeling with Try block
try{
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(str.getBytes());
cipher_byte = md.digest();
StringBuilder sb = new StringBuilder(2 * cipher_byte.length);
for(byte b: cipher_byte) {
sb.append(String.format("%02x", b&0xff) );
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
//Get Root, String return type
public String getRoot() {
return this.root;
}
}
/*Program ends here*/
Write down a Java program that calculates the Merkle root for the following dataset: 7246 9716...
In Java, Write a program without using a build in square-root operator to compute square-root of a number that is known to have an integer square root
Write a Java program that calculates the average rainfall for three months. The program should ask the user to enter the name of each month such as June or July, and the amount of rain (in inches) that fell each month. The program should display a message similar to the following: The average rainfall for June, July, and August is 6.72 inches
java
CountDigits.java Write a program that calculates and prints, the sum and product of all the digits in an integer value read from the keyboard.
write a java program that calculates the average of n odd numbers, where the value of n is given by the user.
/*Write a Java program that generates 1000 random integers, * calculates the average and prints two counts, one for the * numbers lower than the average and one for the numbers larger than the average. */ Use java thanks
Write a complete java program to calculates the total of all the values in even positions in an array. Note If the input value for the array {1,2,3,4,5,6,7} the output should be: Problems Javadoc Declaration Console <terminated> ch7 [Java Application]/Library/Java/JavaVirtual Machines/jdk-1 The Total of the vaules in the even postion is : 16
#1 Write a java program which calculates the area of either a Triangle or a Rectangle. Use Scanner to take an input field to tell you if the area to be calculated is for a Triangle or is for a Rectangle. Depending on that input - the program must call either the Triangle method or the Rectangle method. Each of those methods must receive 2 input fields from the user - one for base and one for height in order...
In Java. Use the concept of LOOPS: 2.Write a program that calculates the amount a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should display a table showing the salary for each day, and then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the...
Write down a Cre program on computer that calculates the grades of the stud f the students for the marks by using "if else statements "for the following conditions. Marks 60 Grade "D" Marks > 60 &&70 Grade "C" Marks > 70 &&80 Grade "B" Marks > 80 && 90 Grade "A" Marks> 90 && -100 Grade "A+"
Write a small JAVA program that continually reads in user values and calculates the average of all values. The program should first ask the user for the number of values to be entered. Upon storing this value, the program should proceed to read in that amount of values. What will be the best strategy here? a suggestion would be only calculating the average after you know you have read in all values. You can expect to use some type of...