Question
Need help on a Java easy question. I'm new to Java. I have a question on a java problem, I know it is a bit
Need help on a Java easy question.
I'm new to Java. I have a question on a java problem, I know it is a bit easy, but I cannot figure it our on my own.
The problem is: We wanna make expectedKilo of butter. We have small pieces (1 kilo each) and big pieces (5 kilos each). Assume we always use big pieces before small pieces, return -1 if it is impossible to be done. And the return is the total pieces of small butters used.
There are some restrictions for this problem, like if the sum kilos of small and big ones is not exactly expectedKilo, it also returns -1. The more detailed you can see in the following tests:
I've already implemented a little bit for the method as below, can you help me with this? If possible, can you use less "for loop"? Because I wanna get as smaller cognitive complexity as possible. Thank you.
import org.junit.jupiter.api.Test; A4 import static org.junit.jupiter.api.Assertions.*; class ButterMakerTest { @Test void should_throw_if_numbers_of_pieces_is_small_than_0() {...} @Test void should_work_for_normal_cases() { assertEquals( expected: 4, ButterMaker.getSmallButterUsage( smallButterCount: 4, bigButterCount: 1, expectedKilo: 9)); assertEquals( expected: -1, ButterMaker.getSmallButterUsage( smallButterCount: 4, bigButterCount: 1, expectedKilo: 10)); assertEquals( expected: 2, ButterMaker.getSmallButterUsage( smallButterCount: 4, bigButterCount: 1, expectedKilo: 7)); assertEquals( expected: 2, ButterMaker.getSmallButterUsage( smallButterCount: 6, bigButterCount: 2, expectedKilo: 7)); assertEquals( expected: 0, ButterMaker.getSmallButterUsage( smallButterCount: 4, bigButterCount: 1, expectedKilo: 5)); assertEquals( expected: 4, ButterMaker.getSmallButterUsage( smallButterCount: 4, bigButterCount: 1, expectedKilo: 4)); assertEquals( expected: 4, ButterMaker.getSmallButterUsage( smallButterCount: 5, bigButterCount: 4, expectedKilo: 9)); assertEquals( expected: 3, ButterMaker.getSmallButterUsage( smallButterCount: 9, bigButterCount: 3, expectedKilo: 18)); assertEquals( expected: -1, ButterMaker.getSmallButterUsage( small ButterCount: 3, bigButterCount: 1, expectedKilo: 9)); assertEquals( expected: -1, ButterMaker.getSmallButterUsage( smallButterCount: 1, bigButterCount: 2, expectedKilo: 7)); assertEquals( expected: 1, ButterMaker.getSmallButterUsage( smallButterCount: 1, bigButterCount: 2, expectedKilo: 6)); assertEquals( expected: 0, ButterMaker.getSmallButterUsage( smallButterCount: 1, bigButterCount: 2, expectedkilo: 5)); assertEquals( expected: 5, ButterMaker.getSmallButterUsage( smallButterCount: 6, bigButterCount: 1, expectedKilo: 10)); assertEquals( expected: 6, ButterMaker.getSmallButterUsage( smallButterCount: 6, bigButterCount: 1, expectedKilo: 11)); assertEquals( expected: -1, ButterMaker.getSmallButterUsage( smallButterCount: 6, bigButterCount: 1, expectedKilo: 12)); assertEquals( expected: -1, ButterMaker.getSmallButterUsage( smallButterCount: 6, bigButterCount: 1, expectedKilo: 13)); public class ButterMaker { /** We want to make {@code expectedkilo) of butter. We have We have small pieces ...*/ public static int getSmallButterUsage int smallButterCount, int bigButterCount, int expectedkilo) { // TODO: Please implement the method: // 0) { return -1; } return count; // --end-> } }Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started