Jump to content

School Coding Help Java

Bee Bus Hardware

I am in a Comp Sci class in school and we are doing Java. We had to make a program where the user inputs a number which indicates how many friends are coming to a meetup or party. The program needs to figure out how many granola bars, apples and bear paws to buy. a maximum of 2 packs of granola bars can be bought (Each pack has 12) 10 bear paws (1 per pack) and an infinite amount of apples. each person is meant to get 1 snack.

 

Can anyone tell me if there is any way to refine this. P.S it already works perfectly fine I just want to see if there is an easier way to do it.

//Start

import java.util.Scanner;
public class ictone {
    public static void main(String[] args) {

        //Variables and Scanner

        int peers;

        Scanner sc = new Scanner(System.in);

        //User Input Code

        System.out.println("How many of Chaim's peers are coming:");
        peers = sc.nextInt();

        //Granola Bar Code

        if (peers < 13) {
            System.out.println("Chaim needs to buy 1 Granola Bar pack.");
        } else if (peers < 25) {
            System.out.println("Chaim needs to buy 2 Granola Bar packs.");
        }

        //Bear Paw Code


        if ((peers > 25) && (peers < 35)) {
           System.out.println("Chaim needs to buy 2 Granola Bar packs and "+(peers - 25)+ " Bear Paws");
       }


        //Apples Code
        if (peers > 34){
            System.out.println("Chaim needs to buy 2 Granola Bar packs, 10 Bear Paws and "+(peers - 34)+ " apples.");
        }
    }
}

//End

 

Link to comment
Share on other sites

Link to post
Share on other sites

You aren't handling a peer number of 25 in your code. If you're submitting this as an assignment, might want to add a greater than or equal to in the if statement in "Bear Paw Code".

37 minutes ago, Bee Bus Hardware said:
  //Bear Paw Code


        if ((peers >= 25) && (peers < 35)) {

 

 

 

There isn't an easier way to do this. Any other methods would have the same logic of if statements.

Edited by RockSolid1106
On 4/5/2024 at 10:13 PM, LAwLz said:

I am getting pretty fucking sick and tired of the "watch something else" responses. It's such a cop out answer because you could say that about basically anything, and it doesn't address the actual complaints. People use it as some kind of card they pull when they can't actually respond to the criticism raised but they still feel like they need to defend some company/person. If you don't like this thread then stop reading it. See how stupid it is? It's basically like telling someone "shut the fuck up". It's not a clever responsive, it doesn't address anything said, and it is rude. 

 ^

 

bruh switch to dark mode its at the bottom of this page

VPN Server Guide

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, RockSolid1106 said:

You aren't handling a peer number of 25 in your code. If you're submitting this as an assignment, might want to add a greater than or equal to in the if statement in "Bear Paw Code".

 

 

There isn't an easier way to do this. Any other methods would have the same logic of if statements.

Alright thanks for the reply I am going to submit it😀

Link to comment
Share on other sites

Link to post
Share on other sites

While the logic is perfectly sound (after the fix recommended by @RockSolid1106), I personally would just do the entire thing with else-if, instead of starting anew and having to check again if the value is above 24. And for stuff like this I've gotten into the habit of just assigning a string value to a result variable instead of repeating System.out.println every time. That way, you only have to do that once and can just refer to most of the output at a single step, instead of repeating everything from the top each time. Something like this.

 

if (peers < 13) {
    result = "1 Granola Bar pack.";
} else if (peers < 25) {
    result = "2 Granola Bar packs.";
} else if (peers < 35) {
    result = "2 Granola Bar packs and " + (peers - 25) + " Bear Paws.";
} else if (peers > 34) {
    result = "2 Granola Bar packs, 10 Bear Paws and " + (peers - 34) + " Apples.";
}

System.out.println("Chaim needs to buy " + result);

And if you want to be really clever, you can initialize the result string variable with "2 Granola Bar packs" from the get-go and just overwrite it if there are fewer than 13 peers or just append the rest if there are more than 24 peers.

And now a word from our sponsor: 💩

-.-. --- --- .-.. --..-- / -.-- --- ..- / -.- -. --- .-- / -- --- .-. ... . / -.-. --- -.. .

ᑐᑌᑐᑢ

Spoiler

    ▄██████                                                      ▄██▀

  ▄█▀   ███                                                      ██

▄██     ███                                                      ██

███   ▄████  ▄█▀  ▀██▄    ▄████▄     ▄████▄     ▄████▄     ▄████▄██   ▄████▄

███████████ ███     ███ ▄██▀ ▀███▄ ▄██▀ ▀███▄ ▄██▀ ▀███▄ ▄██▀ ▀████ ▄██▀ ▀███▄

████▀   ███ ▀██▄   ▄██▀ ███    ███ ███        ███    ███ ███    ███ ███    ███

 ██▄    ███ ▄ ▀██▄██▀    ███▄ ▄██   ███▄ ▄██   ███▄ ▄███  ███▄ ▄███▄ ███▄ ▄██

  ▀█▄    ▀█ ██▄ ▀█▀     ▄ ▀████▀     ▀████▀     ▀████▀▀██▄ ▀████▀▀██▄ ▀████▀

       ▄█ ▄▄      ▄█▄  █▀            █▄                   ▄██  ▄▀

       ▀  ██      ███                ██                    ▄█

          ██      ███   ▄   ▄████▄   ██▄████▄     ▄████▄   ██   ▄

          ██      ███ ▄██ ▄██▀ ▀███▄ ███▀ ▀███▄ ▄██▀ ▀███▄ ██ ▄██

          ██     ███▀  ▄█ ███    ███ ███    ███ ███    ███ ██  ▄█

        █▄██  ▄▄██▀    ██  ███▄ ▄███▄ ███▄ ▄██   ███▄ ▄██  ██  ██

        ▀███████▀    ▄████▄ ▀████▀▀██▄ ▀████▀     ▀████▀ ▄█████████▄

 

Link to comment
Share on other sites

Link to post
Share on other sites

@Bee Bus Hardware There appears to be a mistake

 

System.out.println("Chaim needs to buy 2 Granola Bar packs and "+(peers - 25)+ " Bear Paws");

 

It should be - 24 not - 25.

 

Since when peers = 25, you have 24 granola bars so you need to feed one extra person.  So you need to 1 bear paw.  With peers[25] - 25 = 0 you will leave one person hungry

 

This is true from [25, 34] range, you always leave one person hungry.  e.g. peers = 30, 24 granola, 6 more mouths to feed. 30-25=5 bear paws.  So you leave one person hungry.

 

A comment on the way you comment though, try to keep the line you are commenting on consistent.  It looks a bit funny having some lines with 3 blanks spaces some with 0 and some with 1.

 

On another note, are you suppose to find the minimum amount of snacks to purchase or just enough snacks and in the order of granola, bear paws, apples?

It all determines on the wording of the question, but what I mean is lets say 13 peers.  Are you suppose to buy 2 granola packs?  Or are you suppose to purchase 1 granola pack and 1 apple?  I'm assuming the way you are implementing is correct (given it's a beginners course, so I wouldn't expect that much extra logic at the moment)...but thought I'd ask, as it changes things on how I would implement it personally

 

 

p.s. To anyone who is beginning and asking for help assignment, this is like the perfect example of how to ask for help [Provides code, asks for review, etc].  So thank you for asking properly, it's really a breath of fresh air

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

I would overcomplicate this for no good reason as i always do 😄

 

python version cuz it's shorter

import math

options = [{"name": "granola pack", "max": 2, "portions": 12},
    {"name": "bear paw", "max": 10, "portions": 1},
    {"name": "apple", "max": 420, "portions": 1}]; #almost infinite

while True: 
    n = int(input("peers: "))
    remaining = n
    
    res = "Chaim  needs to buy: "
    for o in options:
        ratio = remaining / o["portions"];
        to_buy = min(math.ceil(ratio), o["max"]) #only whole amounts and no more than max
        remaining -= to_buy * o["portions"];
        res += str(to_buy) + " " + o["name"]
        if(to_buy > 1):
            res += "s"
        if(remaining <= 0):
            break;
        else:
            res += ", "
            
    print(res)

 

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, wanderingfool2 said:

@Bee Bus Hardware There appears to be a mistake

 

System.out.println("Chaim needs to buy 2 Granola Bar packs and "+(peers - 25)+ " Bear Paws");

 

It should be - 24 not - 25.

 

Since when peers = 25, you have 24 granola bars so you need to feed one extra person.  So you need to 1 bear paw.  With peers[25] - 25 = 0 you will leave one person hungry

 

This is true from [25, 34] range, you always leave one person hungry.  e.g. peers = 30, 24 granola, 6 more mouths to feed. 30-25=5 bear paws.  So you leave one person hungry.

 

A comment on the way you comment though, try to keep the line you are commenting on consistent.  It looks a bit funny having some lines with 3 blanks spaces some with 0 and some with 1.

 

On another note, are you suppose to find the minimum amount of snacks to purchase or just enough snacks and in the order of granola, bear paws, apples?

It all determines on the wording of the question, but what I mean is lets say 13 peers.  Are you suppose to buy 2 granola packs?  Or are you suppose to purchase 1 granola pack and 1 apple?  I'm assuming the way you are implementing is correct (given it's a beginners course, so I wouldn't expect that much extra logic at the moment)...but thought I'd ask, as it changes things on how I would implement it personally

 

 

p.s. To anyone who is beginning and asking for help assignment, this is like the perfect example of how to ask for help [Provides code, asks for review, etc].  So thank you for asking properly, it's really a breath of fresh air

Thank you for the input. I tried it and it works but i already submitted it before i saw this. Thanks for the info though, I will use the logic in my next assignment.

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, shadow_ray said:

I would overcomplicate this for no good reason as i always do

Hey lol, you took a very similar approach to what I was going to suggest.

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×