Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Solve the following in java: Background Images and other electronic data often contain parity bits. These bits (which can be 0 or 1) are not

Solve the following in java:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Background Images and other electronic data often contain parity bits. These bits (which can be 0 or 1) are not part of the original data, but instead are used for error detection. The parity bits are selected in a way that when some of the original file is tampered with or damaged, it can be detected by comparing the current data in the file to the parity bits. There are many different techniques for doing this, but in this assignment we will just look at a simple solution. Suppose we have an image where each "pixel" in the image can be one of two values 0 or 1 like a bit or you can also think of 0 as black and 1 as white). We might have an original image that looks like this 0100010 0010100 0001000 0010100 0100010 Here, we have 5 rows and 7 columns. In order to detect if anything has been changed from our original image, we will add a row and a column of parity bits. We will add the new column at the very right and the new row at the very bottom. For the column of parity bits, we will make a particular bit be 1 if there are currently an odd number of 1s in that column and 0 if there are currently an even number of Is in that column. We will do the same thing for the row of parity bits. If we apply that technique to the image above, we get this result (the red numbers are the parity bits) 01000100 00101000 00010001 00101000 01000100 00010000 Notice that the image now has an even number of Os and Is in every row and column. In fact, this will be true for any image after adding parity information. Next, suppose we received this image that we knew contained parity bits applied by this technique 00110000 00111001 00011100 00111001 00110000 00111100 Again, we color the parity bits red so we can see them better. Suppose that we want to check to make sure that this image hasn't been altered. For each row and column, we record whether the number of 1s (NOT including the parity bits) is even or odd. We also record what we EXPECT the parity bit to be for that row or column (remembering a parity bit is 1 if there are an odd number of ls already and even otherwise) row 0: even we expect 0 for the parity bit row 1: odd-> we expect 1 for the parity bit row 2: odd--we expect for the parity bit (error: the parity bit says 0) row 3: oddwe expect 1 for the parity bit row 4: even we expect 0 for the parity bit column 0: even we expect 0 for the parity bit column 1: even->we expect 0 for the parity bit column 2: even we expect 0 for the parity bit (error: the parity bit says 1) column 3: odd--> we expect 1 for the parity bit column 4: oddwe expect 1 for the parity bit column 5: odd-->we expect 1 for the parity bit column 6: even-we expect 0 for the parity bit From this anal ysis, we can see that the bit at row 2, column 2 has been swapped. It currently shows as a 0, but we know it must be a1 because of the information in the parity bits. Thus, we can correct our image to be this (the corrected image without the parity bits listed): 0011000 0011100 0011110 0011100 0011000 Note that this simplistic technique can ONLY be used to detect if a single bit has been modified. It cannot be used to detect if more than one bit has changed, since several problems could cause the same switch in parity bits Assignment Description: Write a program that asks a user for an image file (.txt). The format of this file is discussed below, but part of the file has an image with elements that are either 0 or as well as a row and column of parity bits Your program will first detect if any bits of the original image have been tampered with, by comparing the parity bits to the Os and 1s in the image. Your program will then correct an error, if there is one, and will display the corrected image using character substitutions (from user input) for Os and 1s in the image There are four sample input files for you to download from Canvas smiley.txt (used above-contains one error) smileyErrors.txt (contains multiple errors) flower.txt (contains one error) lowerNoErrors.txt (contains no errors) Each input file will have the following format RowsxColumns (blank 1ine) the image, using 0s and 1s, including the parity row and column Here, Rows is how many rows are in the image (including the parity row) and Columns is how many columns (again, including the parity column). You may assume that Rows and Clumns are both even. Also, note that there is an x between the Rows and the Columns, so that the first line might look like 26x76 Unfortunately, Text-based File /O is not currently covered in the zyBook, but there is a reference file posted in Canvas (6 files.pdf) under Files-)Java Reference or Modules Canvas Textbook (Java). Since File I/O is not discussed in Lecture until Tuesday of Week 6, please read this document and use the code / pseudocode below to help in completing the initial portion of the program to load the contents of the file into a 2D array -Ask user for file name and store in a String (we will assume the file exists Open an input stream from the file: Scanner inFile-new Scanner(new File(filena me)); Read in the first line of the file (Example 26x76): String size-in File.nextLine0 -Parse line and use first number (26) for the number of rows and second number (76) for the number of columns in your 2D array -Read past the blank line in the file: inFile.nextLine0; Use a nested loop to read in each line from the file, parse each number in the row, andl 2D array Here is a sample run of the program: (Continued on next page) Enter name of input file: smiley.txt ff at ROW 16 Off at CoL 27 Position (16 27) was modified--correcting What character would you like to use for white? + What character would you like to use for black? - Process another file? (Yes/No): YES Enter name of input file: Output for "smileyErrors.txt" Process another file? (Yes/No): yes Enter name of input file: smileyErrors.txt Off at ROW 11 Off at ROW 16 Off at ROW 22 Off at ROW 23 Off at ROW 24 Off at COL 27 Off at COL 45 Off at COL 56 Multiple errors were detected. Image may be damaged. Output for "flower.txt" (Image not shown to save space in this document) Process another file? (Yes/No): yes Enter name of input file: flower.txt Off at ROW 3 Off at COL 16 Position (3, 16) was modified...correcting What character would you like to use for white? + What character would you like to use for black? - Requirements: 1. To simplify grading, the output must look EXACTLY like the output above. 2. Your program must store the Os and 1s from the image in a two-dimensional array. 3. After reading the file into the array, count the number of ls in each row and each column been modified. Print a message saying which position you are changing, and change it in your array then there are likely multiple changes that have been made to the original image. With our parity check technique, there is sometimes no way to tell where the errors are, since several different problems could flip the same parity bits. If you do find that multiple rows and columns are off, display the error shown in the output above and ask if they wish to process another file. Do the same if only a single row or a single column has an odd count. (Do not display the image.) 6. If every row and column has an even count of 1s, then the image has no errors. Print that message as your result. 7. If the image had either one (now corrected) or no errors, then ask the user for a character to substitute for white (the 1s) and a character to substitute for black (the 0s). Loop through your array to print the image using that substitution. DO NOT print the parity row and column 8. Ask the user if they wish to process another file if they answer Yes, YES, yes, etc. (ignore case) repeat the program. 9. Thoroughly test with ALL files. May want to create your own ASCII art test file and see if it works! Background Images and other electronic data often contain parity bits. These bits (which can be 0 or 1) are not part of the original data, but instead are used for error detection. The parity bits are selected in a way that when some of the original file is tampered with or damaged, it can be detected by comparing the current data in the file to the parity bits. There are many different techniques for doing this, but in this assignment we will just look at a simple solution. Suppose we have an image where each "pixel" in the image can be one of two values 0 or 1 like a bit or you can also think of 0 as black and 1 as white). We might have an original image that looks like this 0100010 0010100 0001000 0010100 0100010 Here, we have 5 rows and 7 columns. In order to detect if anything has been changed from our original image, we will add a row and a column of parity bits. We will add the new column at the very right and the new row at the very bottom. For the column of parity bits, we will make a particular bit be 1 if there are currently an odd number of 1s in that column and 0 if there are currently an even number of Is in that column. We will do the same thing for the row of parity bits. If we apply that technique to the image above, we get this result (the red numbers are the parity bits) 01000100 00101000 00010001 00101000 01000100 00010000 Notice that the image now has an even number of Os and Is in every row and column. In fact, this will be true for any image after adding parity information. Next, suppose we received this image that we knew contained parity bits applied by this technique 00110000 00111001 00011100 00111001 00110000 00111100 Again, we color the parity bits red so we can see them better. Suppose that we want to check to make sure that this image hasn't been altered. For each row and column, we record whether the number of 1s (NOT including the parity bits) is even or odd. We also record what we EXPECT the parity bit to be for that row or column (remembering a parity bit is 1 if there are an odd number of ls already and even otherwise) row 0: even we expect 0 for the parity bit row 1: odd-> we expect 1 for the parity bit row 2: odd--we expect for the parity bit (error: the parity bit says 0) row 3: oddwe expect 1 for the parity bit row 4: even we expect 0 for the parity bit column 0: even we expect 0 for the parity bit column 1: even->we expect 0 for the parity bit column 2: even we expect 0 for the parity bit (error: the parity bit says 1) column 3: odd--> we expect 1 for the parity bit column 4: oddwe expect 1 for the parity bit column 5: odd-->we expect 1 for the parity bit column 6: even-we expect 0 for the parity bit From this anal ysis, we can see that the bit at row 2, column 2 has been swapped. It currently shows as a 0, but we know it must be a1 because of the information in the parity bits. Thus, we can correct our image to be this (the corrected image without the parity bits listed): 0011000 0011100 0011110 0011100 0011000 Note that this simplistic technique can ONLY be used to detect if a single bit has been modified. It cannot be used to detect if more than one bit has changed, since several problems could cause the same switch in parity bits Assignment Description: Write a program that asks a user for an image file (.txt). The format of this file is discussed below, but part of the file has an image with elements that are either 0 or as well as a row and column of parity bits Your program will first detect if any bits of the original image have been tampered with, by comparing the parity bits to the Os and 1s in the image. Your program will then correct an error, if there is one, and will display the corrected image using character substitutions (from user input) for Os and 1s in the image There are four sample input files for you to download from Canvas smiley.txt (used above-contains one error) smileyErrors.txt (contains multiple errors) flower.txt (contains one error) lowerNoErrors.txt (contains no errors) Each input file will have the following format RowsxColumns (blank 1ine) the image, using 0s and 1s, including the parity row and column Here, Rows is how many rows are in the image (including the parity row) and Columns is how many columns (again, including the parity column). You may assume that Rows and Clumns are both even. Also, note that there is an x between the Rows and the Columns, so that the first line might look like 26x76 Unfortunately, Text-based File /O is not currently covered in the zyBook, but there is a reference file posted in Canvas (6 files.pdf) under Files-)Java Reference or Modules Canvas Textbook (Java). Since File I/O is not discussed in Lecture until Tuesday of Week 6, please read this document and use the code / pseudocode below to help in completing the initial portion of the program to load the contents of the file into a 2D array -Ask user for file name and store in a String (we will assume the file exists Open an input stream from the file: Scanner inFile-new Scanner(new File(filena me)); Read in the first line of the file (Example 26x76): String size-in File.nextLine0 -Parse line and use first number (26) for the number of rows and second number (76) for the number of columns in your 2D array -Read past the blank line in the file: inFile.nextLine0; Use a nested loop to read in each line from the file, parse each number in the row, andl 2D array Here is a sample run of the program: (Continued on next page) Enter name of input file: smiley.txt ff at ROW 16 Off at CoL 27 Position (16 27) was modified--correcting What character would you like to use for white? + What character would you like to use for black? - Process another file? (Yes/No): YES Enter name of input file: Output for "smileyErrors.txt" Process another file? (Yes/No): yes Enter name of input file: smileyErrors.txt Off at ROW 11 Off at ROW 16 Off at ROW 22 Off at ROW 23 Off at ROW 24 Off at COL 27 Off at COL 45 Off at COL 56 Multiple errors were detected. Image may be damaged. Output for "flower.txt" (Image not shown to save space in this document) Process another file? (Yes/No): yes Enter name of input file: flower.txt Off at ROW 3 Off at COL 16 Position (3, 16) was modified...correcting What character would you like to use for white? + What character would you like to use for black? - Requirements: 1. To simplify grading, the output must look EXACTLY like the output above. 2. Your program must store the Os and 1s from the image in a two-dimensional array. 3. After reading the file into the array, count the number of ls in each row and each column been modified. Print a message saying which position you are changing, and change it in your array then there are likely multiple changes that have been made to the original image. With our parity check technique, there is sometimes no way to tell where the errors are, since several different problems could flip the same parity bits. If you do find that multiple rows and columns are off, display the error shown in the output above and ask if they wish to process another file. Do the same if only a single row or a single column has an odd count. (Do not display the image.) 6. If every row and column has an even count of 1s, then the image has no errors. Print that message as your result. 7. If the image had either one (now corrected) or no errors, then ask the user for a character to substitute for white (the 1s) and a character to substitute for black (the 0s). Loop through your array to print the image using that substitution. DO NOT print the parity row and column 8. Ask the user if they wish to process another file if they answer Yes, YES, yes, etc. (ignore case) repeat the program. 9. Thoroughly test with ALL files. May want to create your own ASCII art test file and see if it works

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

explain the need for human resource strategies in organisations

Answered: 1 week ago

Question

describe the stages involved in human resource planning

Answered: 1 week ago