Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions

Question

Compare the current team to the ideal team.

Answered: 1 week ago

Question

a. Do team members trust each other?

Answered: 1 week ago

Question

How do members envision the ideal team?

Answered: 1 week ago