0

Ok, so this is my first post. Firstly I've been trying to solve this problem on my own but it's been hours and I'm still lost. I've tried searching the web and nothing specific enough comes up. Here is the requirements for the project:

  1. the class must contain a private instance field of type int and named checkNumber .There should be no explicit initialization where the field is declared.
  2. It must provide a no-parameter constructor that initializes the value of the checkNumber field to 23.
  3. It must provide a public method named setCheckNumber that takes one parameter of type int, sets the value of the private field to the value of the parameter, and has no return value. 5. It must provide another a public method named firstLastCheck that takes an ArrayList as its parameter and returns a boolean value. If the initial or last value in the list is equal to the value in the instance field checkNumber then the method returns true; otherwise it returns false. The method may assume that the parameter is a list containing at least one element; that is, it is not the empty list and it is not null.

Here is what I have so far:

public class BasicsA {
 //fields
 private int checkNumber;
 //constructor to initialize fields
 public BasicsA() {
     //initialize checkNumber
     checkNumber = 23;
 }
 //method to take input and change checkNumber
public void setCheckNumber(int nextNumber) {
    checkNumber = nextNumber;
}
//method to return boolean value
public boolean firstLastCheck(ArrayList<Integer>) {

        }
}

Now, I have tried changing all sorts of things. Like using firstLastCheck(nameofArrayVariable). Usually it wants an identifier, but if I get it working it simply cannot return an Array int as boolean. It also confuses me that there is no instruction to first create the array or define it as a field. Also how can it check the first and last array elements when there is no elements in the array yet either? You would think the checkNumber would pass the new int into the array, but it doesn't say to do this.

Sorry I hope I haven't overcomplicated this.

EDIT:

public boolean firstLastCheck(ArrayList<Integer> listInt) {
        if(listInt.get(0) == checkNumber || listInt.get(listInt.size() - 1) == checkNumber) {
        return true;
    } else {
    return false;
    }
}

So how can I check this using the BlueJ IDE? I don't understand what it wants?

6
  • "If the initial or last value in the list is equal to the value in the instance field checkNumber then the method returns true; otherwise it returns false.". You don't return an array list as a boolean. You check if the first or last element in the given array list matches your private variable and if it does, return true, otherwise return false. Commented Feb 19, 2016 at 23:14
  • Geeze I don't know how I forgot about that specification. I did see that and was thinking how to refer to checkNumber, the return boolean value threw me off. Thankyou I'll keep trying. Commented Feb 19, 2016 at 23:18
  • I've seen the edit of your question, and if you check my answer above I'm telling you how to check this code in the main() function. Commented Feb 19, 2016 at 23:37
  • Sorry Miquel, thank you for the help but I mean to call the method using the IDE, without doing anymore to the source code. I may just be confused though, maybe we aren't supposed to call that method in that way. I will try your way to at least check that it works. Commented Feb 19, 2016 at 23:41
  • So did you mean debbuging your method? If so, I'm sorry, I've never used the BlueJ IDE so I don't know how to debug with it. Anyway, the way I told you is a pretty good way to check your code, since that's probably what your teachers will do to check it. You can always write more code to check what you are supposed to do, although then you don't deliver it. Commented Feb 19, 2016 at 23:48

2 Answers 2

1

The method should look like this:

public boolean firstLastCheck(ArrayList<Integer> yourList) {

    return list.get(0) == this.checkNumber || list.get(yourList.size() - 1) == this.checkNumber;


}

Then, if you want to check if it works, you could declare an ArrayList inside your main() function, fill it, and finally call the firstLastCheck() method. It'd be something like:

public static void main(String [ ] args){

    ArrayList <int> my_values = new ArrayList<>();

    my_values.add(1);
    my_values.add(2);
    my_values.add(3);
    my_values.add(23); //the last element is equal to checkNumber

    System.out.print("firstLastCheck result: " + firstLastCheck(my_values));

}
Sign up to request clarification or add additional context in comments.

Comments

0

In addition to the type, you need to give the ArrayList a local name. And then you need to check the first and last values. Something like,

public boolean firstLastCheck(ArrayList<Integer> list) {
    return list.get(0) == checkNumber || list.get(list.size() - 1) == checkNumber;
}

4 Comments

Ok, I actually did have the name in there on several attempts, I likely had an error in the return statement though. Ok, I'll go try again! Thanks.
Alright, so it appears that works, but only with ==checkNumber added. Without that it says cannot convert type. So this leads me to another question, when calling this method I can't understand what it wants (syntax), it won't accept an int, how do I enter an index int?
You're missing the conversion type error the compiler is complaining about. The compiler was complaining because if you were using = instead of == (tends to happen), your assigning values and not checking if two variables are equal. So you can not assign an Integer to a Boolean and viceversa. Also with the ==, you can't compare different types.
You also can't return an int as a boolean, you check equality of primitive values with ==.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.