×

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:

  1. 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.
  2. 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.
  3. 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.

Related Topics

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.

Collection Programs in Java

Collection Programs in Java Collection in Java provides a way to manipulate or store a group of objects. Each object in a collection is called element. Collection programs in Java mainly...

4 minutes read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.

Java Try Keyword

The try block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java try block has...

3 minutes read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.

Insertion Sort in Java

Insertion Sort in Java Insertion sort in Java is a simple sorting algorithm that works in the same way as we hold cards in hand. Insertion sort does the sorting element-by-element,...

3 minutes read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.

Class definition in Java

The class definition in Java Java is an object-oriented programming language. We essentially know that programming languages based on object-oriented paradigms have classes and objects in their concepts as main, which...

6 minutes read.

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Difference Between replace() and replaceall() in Java

In this article, you will be acknowledged about the replace() and replaceall() methods in java. Also, most importantly you will be acknowledged the differences between them along with the example...

4 minutes read.

Java LinkedHashMap

LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are: It contains the values based on the keys. It maintains the order of insertion. It contains unique...

5 minutes read.