1
votes

So I have to create a class Fletcher16 and implement Checksum. It has to contain 4 Methods:

  1. void update(int b): checksum gets updated with value b
  2. void update(byte[] b, int off, int len): checksum gets updated with len values from array b starting from point off.
  3. void reset(): sets checksum to it's initial Value ( in this case 0 )
  4. 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!

1
Use String method getBytes()? It returns a byte array, but you may need to specify a charset.Jems
Did you think of doing a web search for this?WJS

1 Answers

0
votes

The class is a full working class, hence not abstract.

public class Fletcher16 implements Checksum {

The fields are not global, hence not static.

    private int sum1; // 0 per default.
    private int sum2;
    private int m;
    
    public Fletcher16(int m) {
        this.m = m;
    }

    public Fletcher16() {
        this(256); // Maybe 256 is better as default.
    }

Both update methods seem for one byte and several bytes.

    @Override
    public update(int b) {
        ...
    }

    @Override
    public update(byte[] ba, int off, int len) {
        ... for ... update(ba[i]) ...

    }

Reseting means start fresh

    @Override
    public reset() {
        sum1 = 0;
        sum2 = 0;
    }

getValue for the checksum

    @Override
    public long getValue() ... // long?

And no need to repeat the definition of combineValues; just use it in either update or getValue.

public static void main(String[] args) {
    byte[] ba = args[0].getBytes(Charset.defaultCharset());
    if (args.length > 1) {
        int m = Integer.parseInt(args[1]);
        Checksum cs = new Fletcher16(m);
        cs.update(ba);
        cs. ...
    } else {
        ...
    }
}