Question

In: Computer Science

This program should be written in Java language. Given the uncertainty surrounding the outbreak of the...

This program should be written in Java language.

Given the uncertainty surrounding the outbreak of the Coronavirus disease (COVID-19) pandemic, our federal government has to work tirelessly to ensure the distribution of needed resources such as medical essentials, water, food supply among the states, townships, and counties in the time of crisis.

You are a software engineer from the Right Resource, a company that delivers logistic solutions to local and state entities (schools, institutions, government offices, etc). You are working on a software solution to check that given a set of resources, whether they can be proportioned and distributed equally to k medical facilities. Resources are represented as an array of positive integers. You want to see if these resources can be sub-divided into k facilities, each having the same total sum (of resources) as the other facilities. For example with resources = {1,2,3,4,5,6} we can sub-divide them into k = 3 medical facilities, each having the same total sum of 7: {1,6}, {2,5}, and {3,4}.

STARTER CODE

Write a solution method, canDistribute, that returns whether it is possible (true or false) that a given set of resources divides into k equal-sum facilities. You will solve this problem using a recursion:

public class HomeworkAssignment5_2 {    
   public static void main(String[] args) {
      // just like any problems, whatever you need here, etc.
   }
}
class Solution {
   // YOUR STYLING DOCUMENTATION GOES HERE
   public boolean canDistribute(int[] resources, int groups) { 
      // YOUR CODE HERE 
   }
}

EXAMPLES

input: {3,4,5,6}, 2

output: true

Explanation: {3,6}, {4,5}

input: {1}, 1

output: true

Explanation: {1}

input: {1, 3, 2, 3, 4, 1, 3, 5, 2, 1}, 5

output: true

Explanation: {3,2}, {4,1}, {5}, {2,3}, {1,3,1}

input: {1}, 4

output: false

Explanation: cannot split further with a value of 1 in resource.

CONSTRAINTS / ASSUMPTIONS

  • 0 < resources.length() < 1000.
  • 0 < medical facilities < 1000.
  • A sub-divided facility is never empty.
  • The solution method returns false if resources cannot be evenly distributed, true otherwise.
  • There may be more than one ways to sub-divide a given set of resources into k facilities. Doesn't matter. Your solution should return true in that case.
  • You will use a Recursion for this. Failure to do so received -4 points.

HINT

  • Note: you may choose to solve this problem in your own way or use the following pseudo code.
  • Check for all boundary conditions to return result immediately if known, e.g., resources.length() = 1 and k = 1, return true, resources.length() = 1 and k = 4, return false, etc.
  • Find the purported allocation for each group, i.e., allocation = SUM(resources)/k.
  • Sort the resources in either ascending or descending order.
  • Create another solution method of your choice to enable recursion.
  • Remaining Pseudo Code:
    1. Create an empty array of integers with k elements. This is your "memory buffer". At the end of your recursion, you want this memory buffer be filled with equally allocated values, allocation.
    2. Pick the highest resource (one with the highest integral value) in your resources.
    3. Check if the selected resource is <= allocation:
      • If yes, add that value to the first available element in your memory buffer of k elements. Go to #4.
      • If not, move on to other elements in your memory buffer to see if there's available space to accommodate it.
      • If there's no available space to accommodate the selected resource, it is impossible for resources to be allocated equally.
    4. Advance to the next highest resource. Repeat Step #3. If all resources have been considered and there is no more next highest, go to Step #5.
    5. Check if every element in your memory buffer has the same value. If so you have an evenly allocated distribution - return true. False otherwise.

Solutions

Expert Solution

import java.util.Arrays;

public class HomeworkAssignment5_2 {
public static void main(String[] args) {
// just like any problems, whatever you need here, etc.
   Solution solution = new Solution();
   System.out.println(solution.canDistribute(new int[]{3,4,5,6}, 2));
   System.out.println(solution.canDistribute(new int[]{1}, 1));
   System.out.println(solution.canDistribute(new int[]{1, 3, 2, 3, 4, 1, 3, 5, 2, 1}, 5));
   System.out.println(solution.canDistribute(new int[]{1}, 4));
}
}
//There may be more than one ways to sub-divide a given set of resources into k facilities. Doesn't matter. Your solution should return true in that case.
class Solution {
// YOUR STYLING DOCUMENTATION GOES HERE
//   The solution method returns false if resources cannot be evenly distributed, true otherwise.
public boolean canDistribute(int[] resources, int groups) {
//   resources.length() = 1 and k = 1, return true, resources.length() = 1 and k = 4, return false, etc.
   int sum = Arrays.stream(resources).sum();   
if (sum % groups > 0) return false;
// Find the purported allocation for each group, i.e., allocation = SUM(resources)/k.
int allocation = sum / groups;
// Sort the resources in either ascending or descending order.
Arrays.sort(resources);
int subDivided = resources.length - 1;
if (resources[subDivided] > allocation) return false;
while (subDivided >= 0 && resources[subDivided] == allocation) {
subDivided--;
groups--;
}
// Create another solution method of your choice to enable recursion.
return find(new int[groups], subDivided, resources, allocation);
}
public boolean find(int[] groups,int subDivided,int[] resources,int allocation) {
//   A sub-divided facility is never empty.
       if (subDivided < 0) return true;
//       Create an empty array of integers with k elements
       int v = resources[subDivided];
       for (int i = 0; i < groups.length; i++) {
//           Check if the selected resource is <= allocation:
//           If yes, add that value to the first available element in your memory buffer of k elements. Go to #4.
       if (groups[i] + v <= allocation) {
       groups[i] += v;
       if (find(groups, subDivided - 1, resources, allocation)) return true;
       groups[i] -= v;
       }
       if (groups[i] == 0) break;
       }
       return false;
}
}


Related Solutions

Java problem Given the uncertainty surrounding the outbreak of the Coronavirus disease (COVID-19) pandemic, our federal...
Java problem Given the uncertainty surrounding the outbreak of the Coronavirus disease (COVID-19) pandemic, our federal government has to work tirelessly to ensure the distribution of needed resources such as medical essentials, water, food supply among the states, townships, and counties in the time of crisis. You are a software engineer from the Right Resource, a company that delivers logistic solutions to local and state entities (schools, institutions, government offices, etc). You are working on a software solution to check...
this should be written in Java- Problem: Min/Max Search by Index Develop a program that, given...
this should be written in Java- Problem: Min/Max Search by Index Develop a program that, given a sequence S of integers as input, produces as output two sequences of positive integers, the first of which indicates all those positions in S at which S's minimum value occurs and the second of which indicates all those positions at which S's maximum value occurs. Positions are numbered starting at zero (0). Facts ● Scanner has a method that returns a boolean indicating...
This program is to be written in Java Language. Thank you A College has conducted a...
This program is to be written in Java Language. Thank you A College has conducted a student survey. Students were asked to rate their satisfaction with remote learning courses. Students rated their satisfaction on a scale of 1 to 5 (1 = "I hate it", 5 = "I love it"). The student responses have been recorded in a file called "StudentResponses.txt". Each line of the file contains one student response. Program 1 You are to write a program that reads...
Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
This program is written in Java and should be modularized in methods in one class. This...
This program is written in Java and should be modularized in methods in one class. This program will calculate the Gross Earnings, FICA tax (Medicare and Social Security taxes), Federal Tax Withheld, and Net Amount of the payroll check for each employee of a company. The output must contain numbers with 2 decimal places. The user input must be validated – if incorrect data is entered, send an error message to the user. INPUT The application must be able to...
The solution should be written in Java. Your POSmain program should take three file names from...
The solution should be written in Java. Your POSmain program should take three file names from command line arguments. The first file contains a list of products and their prices; the second and third files are lists of items in two shopping carts of two customers. The POSmain program should first read the price file, then read each of the cart files to load a list of items in a shopping cart and store them in a ShoppingCart objects. The...
Given the uncertainty surrounding refunds, what alternative accounting approaches might Groupon consider for handling refunds?
Given the uncertainty surrounding refunds, what alternative accounting approaches might Groupon consider for handling refunds?
Language is Java Design and write a Java console program to estimate the number of syllables...
Language is Java Design and write a Java console program to estimate the number of syllables in an English word. Assume that the number of syllables is determined by vowels as follows. Each sequence of adjacent vowels (a, e, i, o, u, or y), except for a terminal e, is a syllable. However, the minimum number of syllables in an English word is one. The program should prompt for a word and respond with the estimated number of syllables in...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT