So I have to create a class Fletcher16 and implement Checksum. It has to contain 4 Methods:
- void update(int b): checksum gets updated with value b
- void update(byte[] b, int off, int len): checksum gets updated with len values from array b starting from point off.
- void reset(): sets checksum to it's initial Value ( in this case 0 )
- void long getValue(): returns the current Value of checksum. For this sum1 and sum2 need to get tucked together with the Method combineValues(int value1, int value2) (this one is already done)
My Question: What the hell am I supposed to do? I know that for the tests I get an array with 1 or 3 arguments. If it's 3 arguments then args[0] = m, args[1] = message, args[2] = checksum. It then returns true if the checksum is correct or false if the checksum is wrong. If it's just 1 argument this is supposed to represent m. You can then get the message with the Scanner from the input. In this case the Method is supposed to return the checksum.
But how do I calculate it? How do I calculate the checksum from a String? The definition we were given was:
Fletcher16 (data)
Input: Array data of 8-Bit-Integer
Output: 16-Bit-Checksum to data
sum1 := 0
sum2 := 0
for i := 0 to length (data) do
sum1 := (sum1 + data[i]) modulo M
sum2 := (sum2 + sum1) modulo M
endfor
checksum := sum1 following sum2
return checksum
I also don't quite understand what the 4 Methods are supposed to.. can anybody give me a hint? My current code is almost empty but here it is:
import java.util.Scanner;
import java.util.zip.Checksum;
public abstract class Fletcher16 implements Checksum {
private static int sum1=0;
private static int sum2=0;
private static int m;
public Fletcher16(int m){
this.m = m;
}
public Fletcher16(){
m = 255;
public update(int b){
}
public update(byte[] b, int off, int len){
}
public reset()
public long getValue()
private static short combineValues(int value1, int value2) {
return (short) ((value1 & 0x00ff) | ((value2 << 8) & 0xff00));
I am sorry if this looks really weird or has some really bad explanation this is the first time I am using stackoverflow :/ Thanks in advance!