-2

I have this program that doesn't work, they ask me to return an array with barraCaramelo, chicles and cuponesRestantes. Each of this products cost coupons barraCaramelo (10 coupons), chicles(3 coupons), and cuponesRestantes are the coupons that remains.

If someone can solve this will do me a enormous favor, I don't really understand how this line (public static int[] candyCalculator(int num_coupons)) works, they ask me to create a public static void main(String args[]) but I don't know how to do it. Thanks

      public static int[] candyCalculator(int num_coupons) {
        int barraCaramelo = num_coupons / 10;//this cost 10 coupons
        int cuponesChicles = num_coupons % 10;
        int chicles = cuponesChicles / 3;// cost 3
        int cuponesRestantes = cuponesChicles % 3;//coupons that remain
        int[] arr=new int[3];
        arr[0]=barraCaramelo;
        arr[1]=chicles;
        arr[2]=cuponesRestantes;
        return arr;

    }
    public static void main(String args[]){
        num_coupons=10;
        int[] a=candyCalculator();
        for (int i=0;i<4;i++){
            System.out.print(a[i]);
    }
 
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Pau
  • 1

4 Answers4

2

I think we're mixing two kind of variables here and it's important to know what is what.

It's very important we know for sure the context of your issue before going any further, as it stands we can't tell if the request is for barraCaramelo (quantity) or barraCaramelo (cost in coupons).

Now, regarding your question of how this line works:

  public static int[] candyCalculator(int num_coupons) {

This is what we call a "method declaration", means your program is declaring a part of the code to perform a designated task, which can then be called from other parts of the code. For what it looks like you want to print each line of the array, but it's not entirely clear.

ItsLhun
  • 96
  • 1
  • 3
2

You should pass a parameter to your method call like,

int[] a = candyCalculator(10);

This loop,

for (int i=0;i<4;i++){
            System.out.print(a[i]);
    }

will give an indexOutOfBounds Exception as your array only has 3 elements therefore the condition should be corrected to,

for (int i=0;i<3;i++){
                System.out.print(a[i]);
        }

As for your question regarding the method public static int[] candyCalculator(int num_coupons)

  1. public is an access modifier which specifies who can access this method.enter image description here

  2. static means that this method can be accessed without creating an instance of a class. (See this answer)

  3. int[] is the return type of the method.

  4. candyCalculator is the method name.

  5. int num_coupons is the parameter passed, where int is the datatype and num_coupons is the parameter name.

ArbitraryChoices
  • 243
  • 1
  • 11
0

You have many mistake in your code. Compare the following which is runnable with your code.


public class Main
{
        public static int[] candyCalculator(int num_coupons) {
        int barraCaramelo = num_coupons / 10;//this cost 10 coupons
        int cuponesChicles = num_coupons % 10;
        int chicles = cuponesChicles / 3;// cost 3
        int cuponesRestantes = cuponesChicles % 3;//coupons that remain
        int[] arr=new int[3];
        arr[0]=barraCaramelo;
        arr[1]=chicles;
        arr[2]=cuponesRestantes;
        return arr;

    }
    public static void main(String args[]){
        int num_coupons=10;
        int[] a=candyCalculator(num_coupons);
        for (int i=0;i<3;i++)
            System.out.println(a[i]);
    }
}

Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55
0

Main method

You already have a main method:

public static void main(String args[]){
    num_coupons=10;
    int[] a=candyCalculator();
    for (int i=0;i<4;i++){
        System.out.print(a[i]);
}

num_coupons

You set the value of num_coupons to ten here but, at least your code does not contain its declaration. You could declare it in your main method as a local variable, like

public static void main(String args[]){
    int num_coupons=10;
    int[] a=candyCalculator();
    for (int i=0;i<4;i++){
        System.out.print(a[i]);
}

Index out of bounds

Since candyCalculator creates an array of three items, the last index being 2, your for cycle in your main will crash and burn when it reaches the value of 3 and try to find the value of that index in your array. Let's make your loop fit your array's length:

public static void main(String args[]){
    int num_coupons=10;
    int[] a=candyCalculator();
    for (int i=0;i<a.length;i++){
        System.out.print(a[i]);
}

Passing a parameter

candyCalculator expects you to pass an integer to it, but you have empty paranthesis there, which means that you do not provide the parameter it expects to get. Let's pass the proper value:

public static void main(String args[]){
    int num_coupons=10;
    int[] a=candyCalculator(num_coupons);
    for (int i=0;i<a.length;i++){
        System.out.print(a[i]);
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175