Question
In this program, you are required to write a Java program to implement a data structure, called, a Refrigerator and test your implementation. We all
In this program, you are required to write a Java program to implement a data structure, called, a "Refrigerator" and test your implementation. We all know that a refrigerator is a cooling device that holds many items, say N.
A refrigerator object is stored as an array of integers where the value of the element at index i contains the number of occurrences of the ith item in that refrigerator. For example, the collection of items {1,3,5,4,7,3,1,5,4,3,2,8,4,3,9} forms a perfectly valid refrigerator in which item 3 appears four times. This specific refrigerator array will look like...
Index --- 1 2 3 4 5 6 7 8 9 10 11 12 -----
Value --- 2 1 4 3 2 0 1 1 1 0 0 0 -----
You are required to implement the following operations on a Refrigerator.
Constructor: Initializes the refrigerator to be an empty one. (All values in the array = 0)
is_empty: Returns true if the refrigerator is completely empty; false, otherwise.
insert_item: Takes in an item and places it in the refrigerator.
delete_item: Takes in an item and deletes one occurrence of that item (if it exists) in the refrigerator.
find_it: Takes in an item and returns true if the item is present in the refrigerator at least once; false, otherwise.
combine: Takes in two refrigerators and returns the combined refrigerator. For example, if one had 3 MILKs and the other had 5 MILKs, then the combination has 8 MILKs.
common_items: Takes in two refrigerators and returns the common items in them as the refrigerator. For example, if one had 3 MILKs and the other had 5 MILKs, then the common number of MILKs in the result refrigerator is 3.
output_refrigerator: Outputs the items of the refrigerator. Multiple occurrences are output appropriately to show how many times that item occurs in the refrigerator. For example, the output for the above refrigerator is ...
1 1 2 3 3 3 3 4 4 4 5 5 7 8 9
To test that your implementation works, write a main method as instructed below. All input MUST BE read from an input file whose name is given by the user (no hard coding of the file names in your program is allowed). The user also provides the name of the output file where the output of your program will be produced.
Create an array of refrigerators (the first input data is the maximum number of refrigerators), called R. R[i] refers to ith refrigerator. Just for the hack of it, do not use R[0]. The second data item in the input file is the maximum number of different items stored in a refrigerator.
Beyond this, the input file contains the operation to be performed, one line per operation. Let's assume that a single character read from input file identifies which of the above seven operations needs to be performed (Constructor is invoked automatically). Let these characters respectively be,
E I D F C M O
i.e., D indicates "delete an element" operation, M indicates "perform the common-items operation", etc.
For example,
I 1 3 -- Insert item 1 in refrigerator 3.
D 3 6 -- Delete item 3 from refrigerator 6.
E 2 -- Is R[2] empty ?
F 5 7 -- Is item 5 in R[7] ?
C 6 4 5 -- R[5] := Combination of R[6] and R[4].
M 5 3 2 -- R[2] := Common items of R[5] and R[3]
O 1 -- Output the contents of R[1].
The main program just reads in the operation codes corresponding to the operations to be performed, and uses the class to perform them.
It is very important that after doing each operation, you mention in the output, what operation was performed and what are the contents of the altered refrigerator, wherever appropriate; or what is the result of the query. The output must be completely self-explanatory and must be produced in an output file whose name is provided by the user.
1) A well-documented source code. At the least, you must specify the input-output behavior of each method and the meaning of all variables used in the class.
2) The input file containing the test data
3) The output file
INPUT TXT FILE
7 // Maximum Number of refrigerators 12 // Maximum Number of items E 1 E 11 I 2 1 I 3 1 I 4 1 I 4 1 I 7 1 I 8 1 I 7 1 I 1 1 I 3 1 I 5 1 I 6 1 I 6 1 I 4 1 I 6 2 I 8 2 I 9 2 I 11 2 I 11 2 I 12 2 I 1 2 I 3 2 I 2 2 I 3 2 I 1 2 I 5 2 I 3 2 E 2 D 11 2 D 10 1 D 6 1 F 12 1 F 4 2 F 2 2 F 1 1 F 10 2 C 1 2 3 M 1 2 4 C 3 4 5 M 3 4 6 C 1 1 7 O 1 O 2 O 3 O 4 O 5 O 6 O 7
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started