Question: Write a Java program that computes a simple checksum of 8-bit integers. This program is based upon the calculation of the checksum value of a
-
Write a Java program that computes a simple checksum of 8-bit integers. This program is based upon the calculation of the checksum value of a IPv4 header, defined by RFC791.
-
Time the execution of your program, as well as two other programs provided by the professor.
This program should conform to the following specification:
- Program name: checksum.java
- Reads 10 non-negative integers from standard input (stdin), with each integer value in the range of 0..2^8-1 (I.e., 0..255).
- Stores the 6th input integer into a variable called "checksum", and resets this input value to zero (0).
- Stores the sum of the integers read from stdin into a variable called "sum".
- Performs integer division on this sum using 2^8 as the divisor to yield both a quotient and a remainder. These values are then stored in the variables "quotient" and "remainder", respectively.
- Adds the values of "quotient" and "remainder" together, and stores this value into the variable "sum".
- Subtracts this new value of "sum" from 2^8-1, and stores the result in a variable called "complement".
- Outputs the value of "checksum" and "complement" to standard output (System.out).
- If the value of "checksum" and "complement" are not the same, outputs the string "Error Detected!" to standard error (stderr).
Starter code:
- - Define a constant with an appropriate name and defined the value.
-- Use a for loop
import java.util.Scanner; class checksum { public static void main(String args[]) { final int max_int = 255; // The maximum size for the input int count = 10; // The number of integers to read from stdin int sum = 0; // Note that the "sum" might exceed max_int int checksum = 0; // The value of the 6th input integer int quotient; // The result of evaluating the assignment: quotient = sum / (max_int + 1); int remainder; // The result of evaluating the assignment: remainder = sum % (max_int + 1 ); int complement; // The result of evaluationg the assignment: complement = max_int - sum; Scanner stdin = new Scanner(System.in); System.out.printf("Stored Checksum: %d, Computed Checksum: %d ", checksum, complement); if (checksum != complement ) { System.err.printf("Error Detected! "); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
