flour pack problem in Java
Create a method called canPack and give it three int parameters: bigCount, smallCount, and objective.
The bigCount option indicates the number of large flour bags (5 kilos each).
The smallCount argument indicates the number of tiny flour bags (1 kilo each).
The aim parameter specifies the desired number of kilograms of flour required to construct a package.
The kilos of bigCount and smallCount must add up to at least the goal's value. If the specified flour can be used to construct a package, the method should return results as true.
Ensure only completed bags are counted toward the goal amount if the total exceeds the objective. Because every bigCount bag is 5 kilograms and cannot be divided, the function should return false. For instance, if aim = 9, bigCount = 2, and smallcount = 0. But if the goal is 9, bigCount is 1, and smallCount is 5, the function would return true because the objective is equivalent to 1 full bigCount bag and 4 full smallCount bags, and it's acceptable if there are extra bags.
Return false if any of the parameters are negative.
Example:
If the input of bigcounts, smallcounts, and goals is=(1,0,4)
Output: false
In the example, the given count of big bags is 1, and the count of the small bags is 0. And the goal required is 4.
Each big bag should contain 5 kg, and each small bag should contain 1 kilo per bag. So the total will become 5, and the goal is 4, so the condition is false. The goal should be greater or equal to the total storage of the bag.
Java Program for flour pack Problem
Filename: FlourPack.java
//Program for flour pack problem in Java
//importing the packages
import java.io.*;
import java.util.*;
public class FlourPack{
public static void main(String[] args){
//the result is displayed
System.out.println(canPacks (1, 0, 4));
}
public static boolean canPacks (int bigCounts, int smallCounts, int goals){
int totalKilo = (5 * bigCounts) + smallCounts;
//the variable for storing the count and bag values
int counts =0;
int temps = 0;
//the condition for checking the validity of bag storage
if (totalKilo < goals)
{
return false;
}
// This condition will be true at any condition
else if (totalKilo == goals)
{
return true;
}
// If the condition is not valid, i.e., bag capacity is increased
else{
//For loop that retrieves the greatest number in the number 'target' divisible by 5.
for(int i=0;i<goals; i++){
temps = goals - i;
if (temps % 5 == 0){
break;
}
}
//If there are more large bags than the largest number, that is 5 times divisible.
if (bigCounts>= temps/5) {
goals -= temps;
//the condition for checking the remaining the bags
if (smallCounts >= goals) {
return true;
}
//the function will return false
return false;
}
//If there are fewer huge bags than the largest integer, that is 5 times divisible.
else{
goals -= (temps-bigCounts*5);
if (smallCounts >= goals) {
return true;
}
return false;
}
}
}
}
Output
false
Input/Output
1. Since the aim is 5 kilograms and bigCount is 1 (a big bag weighing 5 kilograms), canPacks (1, 0, 5)
should return true.
2. Given that the target is 4 kilos and smallCount is 5 (tiny bags of 1 kilo), canPacks (0, 5, 4)
should return true.
3. Kilograms canPacks (2, 2, 12); should return true since the overall weight of the two huge bags (each weighing 5 kilograms) and two little bags (each weighing 1 kilogram) is 12 kilograms, and the target weight is 12 kilograms.
4. Given that bigCount is negative, the function canPacks (-3, 2, 12) should return false. Here the bag count is negative, which results in a false.
Explanation:
- In input 1, canPacks(1,0,5), the count of big bags is 1, which indicates that it can store 5 kilograms and the small bag count is 0. The required goal is 5 kilograms which is equal to the total storage. Hence it returns true.
- In input 2, canPacks(0,5,4) in the given input, the count of big bags is 0, and the count of small bags is 5, which indicates the total storage as 5 kilograms. The total value is greater than the goal, so it returns true.
- In input 3, canPacks(2,2,12), the values of the big bag count and the small bag are 2, 2, and the total storage bag is 12, equal to the goal value, so it returns true as the output.