-4

I hope all is well. I've recently solved an algorithm in Coding Bat (Java - Array1 - firstLast6):

Problem

*Given an array of ints, return true if 6 appears as either the first or last element in the array. The array will be length 1 or more.

firstLast6([1, 2, 6]) → true

firstLast6([6, 1, 2, 3]) → true

firstLast6([13, 6, 1, 2, 3]) → false*

My Solution

public boolean firstLast6(int[] nums) {
  
  if ( (nums[0] == 6) || (nums[nums.length - 1]) == 6 ) {
    
    return true;
    
  }
  
  return false;
  
}

This is the correct solution. However, it's one thing to solve the problem in Coding Bat, but I want to be able to call this boolean method in my VS Code editor in the main method. My research thus hasn't produced a solid answer to my question.

In the main method:

  1. Boolean method call: How would you call the boolean method that has an array (nums) as a parameter? I'm stuck on the syntax for this part.

  2. Print out statement: Using "System.out.println()" print out the true or false result?

VS Code - Full Layout

public class ReturnStatements {

    public static void main(String[] args) {

        // Method call
        
        // Print out statement outputing true or false.

    }
    
    public static boolean firstLast6(int[] nums) {

        if ( nums[0] == 6 || nums[nums.length - 1] == 6 ) {

            return true;

        }

        return false;
    }

}
Rashad Nelson
  • 27
  • 1
  • 5
  • 2
    "Boolean method call: How would you call the boolean method that has an array (nums) as a parameter?" the same way you would call a method with any other type of parameter. Unless you're asking [how to pass an array literal](https://stackoverflow.com/questions/19533625/how-to-pass-a-function-an-array-literal) – Federico klez Culloca May 30 '22 at 14:56
  • 1
    "Print out statement: Using `System.out.println()` print out the true or false result?" the same way you would use `println` to print anything else. – Federico klez Culloca May 30 '22 at 14:56
  • 1
    side-note: `if (somethingThatIsTrue) return true; else return false;` can be simplified to `return somethingThatIsTrue;` – thinkgruen May 30 '22 at 14:58
  • 2
    As these are questions about basic Java syntax, I suggest to read about that. There are tons of articles about that. Unfortunately, this is not appropriate for SO. – Seelenvirtuose May 30 '22 at 14:58
  • [How do I declare and initialize an array in Java?](https://stackoverflow.com/q/1200621/12567365). Once you have created your array, you can use it in your call to `firstLast6()`. See also the Java tutorials for [arrays](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) and for [calling methods](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html). – andrewJames May 30 '22 at 15:05

2 Answers2

0

You can do:

public static void main(String[] args) {
    System.out.println(fistLast6(new int[] {1, 2, 6}));
}

And @thinkgruen's suggestion:

public static boolean firstLast6(int[] nums) {
    return nums[0] == 6 || nums[nums.length - 1] == 6;
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 1
    Thank you, this is greatly appreciated. I went back to added in a declared boolean variable (just for my own readability). – Rashad Nelson May 31 '22 at 14:28
0
public class ReturnStatements {

    public static void main(String[] args) {

        // Method call
        boolean toFirstLast6 = firstLast6(new int[] {1, 2, 6});

        // Print out statement outputing true or false 
        System.out.println(toFirstLast6);

    }

    public static boolean firstLast6(int[] nums) {

        if ( nums[0] == 6 || nums[nums.length - 1] == 6 ) {

            return true;

        }

        return false;
    }

}
Rashad Nelson
  • 27
  • 1
  • 5