Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I need help with this assignment I need to help with FloodFill.java In the A1_Python_code.zip file, you will find a working version of the

Hi, I need help with this assignment I need to help with FloodFill.java

image text in transcribed

image text in transcribed  

In the A1_Python_code.zip file, you will find a working version of the flood fill algorithm written in Python. The module index2.py contains the definition of a Python class named Index2 that represents a two-dimensional index. The module flood_fill.py contains a runnable Python program made up of several functions that flood fills a two-dimensional list. Your assignment is to translate the Python file flood_fill.py into the Java class FloodFill.java. The only file that you should modify is FloodFill.java . The file Index2.java defines a type that you will use to represent a two-dimensional index, and the file FloodFillUtil.java defines a class that provides methods to draw a two-dimensional array and read a two-dimensional array from a file. The file in the package princeton.introcs defines a class that can be used for drawing simple images. One of the methods has already been translated for you: The Python function print_array has been translated to the Java method printArray The main method of FloodFill.java has already been implemented for you, but will not run because the main method calls the missing methods of the class. You do not need to document your code for this assignment. Later assignments will have you writing lots of documentation. Hints The assignment is straightforward if you keep the following in mind: The main method should run successfully if you translate the code correctly. If it does not run successfully, then you have done something incorrectly. DO NOT modify the main method so that your code words; modify your code so that the main method works. The Python program uses an underscore to separate words in multiword function names (for example, test_array_not_empty) You should use the Java convention of lower camelcase variable and method names (for example, testArrayNotEmpty ) The Python program is made up of several functions. Java has no functions. You should translate the Python functions into public static methods in this assignment. The order and types of the parameters matter when translating the Python functions to Java methods. the type of the two-dimensional index is Index2 the type of the two-dimensional array is int[][] the type of the elements in the array is int In Python, a two-dimensional list is a list where the elements of the list are other lists. For example, a 5 5 grid of values can be represented by a list containing 5 sublists where each sublist contains 5 values. To obtain the number of rows in a two-dimensional list t we can write rows = len(t) If there is at least one row, then the number of columns can be obtained by writing cols = len(t[0]) (the number of elements in the first row). In Java, we can use a two-dimensional array instead of a list of lists. A two-dimensional array int ( ) ( ) is an array where the elements are arrays of int the number of rows in a two-dimensional array arr is equal to arr.length this is equal to the number of arrays in arr the number of columns is equal to the length of the first array in the array if there is one this is equal to arr[0].length if arr[0] exists In Python, a function can validate the arguments to the function and then raise an exception if an argument has an invalid value. In Java, a method can throw an exception when an invalid argument is detected. In this assignment, you should throw an IllegalArgumentException when an invalid argument is detected. The Python program uses a class named Index2 to represent a two-dimensional index. An Index2 object has attributes named respectively. The Java class Index2.java defines a class that is very similar to the Python class of the same name. row and col to represent the row index and column index, In Java, you can create an Index2 object by using the new operator and a constructor. For example, to create an Index2 object representing row and column indexes of 1 and 3 we can write Index2 i = new Index2 (1, 3) . To get the row and column indexes from an Index2 object we can write int r = i.row and int c = i.col (similar to how you access the indexes in the Python program) Pay close attention when translating the flood_fill function. The Python function says that your Java method should include the line FloodFillUtil.draw(arr); where indicated. This is needed to allow the Java program to draw the array. Running the program If you successfully translate the Python code to Java, then running the FloodFill.java program should draw a picture. The default starting array has Os (the constant BORDER ) on the first and last rows and columns, and 1s (the constant INTERIOR) everywhere else. The INTERIOR values are replaced with REPLACEMENT Array values equal to BORDER INTERIOR and REPLACEMENT are drawn in black, white, and yellow, respectively. The following image shows the starting array (left) and the flood filled array (right): If you uncomment the line //arr = FloodFillUtil.readArray("face.txt"); in the main method, then the program flood fills the interior of a face. The following image shows the starting array (left) and the flood filled array (right): If you uncomment the line //arr = FloodFillUtil.readArray("random.txt"); in the main method, then the program flood fills the interior of an array containing a semi-random sequence of Os and 1s. The following image shows the starting array (left) and the flood filled array (right): AA Extra notes the mapping of values to colors is defined in FloodFillUtil.java the main method always start the flood fill at row=3, col=3 (the fourth row and column) changing the starting indexes may affect what gets filled the face and random arrays are defined in the files face.txt and random.txt you can create your own files if you want; save them in the a1 folder the number of rows and columns can be different, but the program always draws into a square canvas

Step by Step Solution

3.30 Rating (147 Votes )

There are 3 Steps involved in it

Step: 1

Sure Based on the hints provided it seems like you need assistance with implementing the Flood Fill ... 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

Step: 3

blur-text-image

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

Exploring Marketing Research

Authors: Barry J. Babin, William G. Zikmund

11th Edition

1305263529, 9781305263802, 1305263804, 9781305831247 , 978-1305263529

More Books

Students also viewed these Programming questions

Question

Which has the higher frequency: X-rays or gamma rays?

Answered: 1 week ago

Question

What is the entropy or pseudo R 2 ?

Answered: 1 week ago