In this program you are required to write a Java program to

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.

Solution

Change the input file location in the main method then copy paste the below code in Refrigerator.java file.

Compile the class using javac.

Run the file using java.

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Refrigerator {

  

   private int [] storage;

  

   //creates a refrigrator with default 20 capacity

   public Refrigerator() {

       this.storage = new int [21];              

   }

  

   //creates a refrigrator with specified capacity

   public Refrigerator(int capacity) {

       this.storage = new int [capacity];              

   }

  

   public boolean isEmpty(){

      

       boolean isEmpty = true;

      

       for(int i: storage){

           if (i > 0){

               isEmpty = false;

           }

           break;

       }

      

       return isEmpty;

   }

  

   public void insertItem(int itemNumber){

      

       int count = storage[itemNumber];

       count++;

       storage[itemNumber] = count;

   }

  

   public void deleteItem(int itemNumber){

      

       int count = storage[itemNumber];

       if(count > 0){

           count--;

       }

       storage[itemNumber] = count;

   }

  

   public boolean findIt(int itemNumber){

       if(storage[itemNumber] > 0){

           return true;

       }else{

           return false;

       }

   }

  

   public static Refrigerator combine(Refrigerator r1, Refrigerator r2){

      

       int r1L = r1.getCapacity();

       Refrigerator r = new Refrigerator(r1L);

       for(int i= 0; i < r1L; i++){

           r.getStorage()[i] = r1.getStorage()[i] + r2.getStorage()[i];

       }

      

       return r;

   }

  

   public static Refrigerator commonItems(Refrigerator r1, Refrigerator r2){

      

       int r1L = r1.getCapacity();

       int r2L = r2.getCapacity();

      

       Refrigerator r = new Refrigerator(r2L);

      

       for(int i= 0; i < r1L; i++){

           if((r1.getStorage()[i] < r2.getStorage()[i])){

               r.getStorage()[i] = r1.getStorage()[i];

           }else if ((r2.getStorage()[i] < r1.getStorage()[i])){

               r.getStorage()[i] = r2.getStorage()[i];

           }

       }

      

       return r;

      

   }

   public int getCapacity(){

       return this.storage.length;

   }

  

   public int [] getStorage(){

       return this.storage;

   }

  

  

  

   public void outputRefrigerator(){

      

       for(int i = 1; i< storage.length; i++){

           if(i > 0){

               for(int j = 0; j < storage[i]; j++){

                   System.out.print(i+\" \");

               }

           }

       }

       System.out.println();

   }

  

   public static void main(String[] args) throws IOException {

      

       //Creating an array of 15 refrigerators

       //However 16 is written below because, index 0 is not used      

       Refrigerator [] R = new Refrigerator [16];

      

       for(int i = 1; i < R.length; i++){

           R[i] = new Refrigerator();

       }

      

       BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(\"/Users/ansharma/Documents/input.txt\")));

      

       String line = null;

      

       while((line = bufferedReader.readLine()) != null){

           String [] tokens = line.split(\" \");

           char choice = tokens[0].charAt(0);

           switch(choice){

           case \'E\':

               int refNum = Integer.parseInt(tokens[1]);

               R[refNum].isEmpty();

               System.out.println(\"Is empty operation performed on Refrigerator: \"+refNum);

               R[refNum].outputRefrigerator();

              

               break;

           case \'I\':

               int itemNum1 = Integer.parseInt(tokens[1]);

               int refNum1 = Integer.parseInt(tokens[2]);

               R[refNum1].insertItem(itemNum1);

               System.out.println(\"Inserted Item \"+itemNum1+\"in Refrigerator: \"+refNum1);

               R[refNum1].outputRefrigerator();

               break;

           case \'D\':

               int itemNum2 = Integer.parseInt(tokens[1]);

               int refNum2 = Integer.parseInt(tokens[2]);

               R[refNum2].deleteItem(itemNum2);

               System.out.println(\"Deleted Item \"+itemNum2+\"in Refrigerator: \"+refNum2);

               R[refNum2].outputRefrigerator();

               break;

           case \'F\':

               int itemNum3 = Integer.parseInt(tokens[1]);

               int refNum3 = Integer.parseInt(tokens[2]);

              

               System.out.println(\"Find Item \"+itemNum3+\"in Refrigerator: \"+refNum3+\" result: \"+ R[refNum3].findIt(itemNum3));

               R[refNum3].outputRefrigerator();

               break;

           case \'C\':

               int r1 = Integer.parseInt(tokens[1]);

               int r2 = Integer.parseInt(tokens[2]);

               int r3 = Integer.parseInt(tokens[3]);

               R[r3] = Refrigerator.combine(R[r1], R[r2]);

               System.out.println(\"Refrigerator \"+r1+\" and \"+r2+\" combined to create \"+r3);

               R[r3].outputRefrigerator();

               break;

           case \'M\':

               int r11 = Integer.parseInt(tokens[1]);

               int r21 = Integer.parseInt(tokens[2]);

               int r31 = Integer.parseInt(tokens[3]);

               R[r31] = Refrigerator.commonItems(R[r11], R[r21]);

               System.out.println(\"Refrigerator \"+r11+\" and \"+r21+\" commonItemed to create \"+r31);

               R[r31].outputRefrigerator();

               break;

           case \'O\':

               int toBePrinted = Integer.parseInt(tokens[1]);

               System.out.println(\"Output operation performed on refrigerator \"+toBePrinted);

               R[toBePrinted].outputRefrigerator();

               break;

           default:

               break;

           }

       }

      

       bufferedReader.close();

      

   }

}

In this program, you are required to write a Java program to implement a data structure, called, a \
In this program, you are required to write a Java program to implement a data structure, called, a \
In this program, you are required to write a Java program to implement a data structure, called, a \
In this program, you are required to write a Java program to implement a data structure, called, a \
In this program, you are required to write a Java program to implement a data structure, called, a \
In this program, you are required to write a Java program to implement a data structure, called, a \
In this program, you are required to write a Java program to implement a data structure, called, a \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site