×

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

Java Linters

When it comes to programming, everyone makes mistakes. Errors are bad for developers since they are difficult to handle. But handling as many as possible errors will bring out the...

6 minutes read.

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

How to Convert int to long in Java

How to Convert int to long in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.

Pancake Sorting in Java

In the pancake sorting method, the array must be sorted using just one operation, which is: Flip the arrays arr at index 0 to index j by using flipArr(arr, h). Other sorting...

3 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Java try catch

Java try catch There can be statements that can cause exceptions, and the exception leads to abnormal termination of the program. To avoid that abnormal termination, those statements that look potent...

4 minutes read.

Java instance variable

An instance in object-oriented programming (OOP) is a particular implementation of any object. Each realized version of an item, which might vary in several ways, is an instance. Instantiation is...

3 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

Special Operators in Java

An operator in Java is a special symbol. It is used to perform operations on two or more variables.  We all know basic operations in Java. There are 8 types...

5 minutes read.

XNOR operator in Java

The opposite of the binary equivalent of XOR is given by XNOR. Truth table: XYXNOR001010100111 If the bits are the same, it returns 1, else it returns 0. Examples:  Input: 10 20 Output : 1 A Binary...

3 minutes read.

Java Numbers

We use primitive data types like byte, int, long, double, etc to work with the numbers, When we need objects, we use wrapper class like Integer, Double, Long, Byte, etc....

2 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.

Perfect Number in Java

The concept of a perfect number in Java will be defined in this chapter, along with creating Program code that determine whether a specific number is perfect or not. Additionally,...

4 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

Java Program to generate binary numbers

A binary tree can create binary numbers ranging from 1 to n. Every node in a tree, the right, and left nodes, has two children, as is common knowledge. The...

3 minutes read.