Page 1 of 1

About Question enthuware.ocajp.i.v7.2.863 :

Posted: Tue Jan 15, 2013 3:51 pm
by lordnovas
Hey Guys,

I'm not sure what the answer notes are talking about when they say the following:
x is primitive int.You cannot call any methods on a primitive. so this.x.setInt(...) or this.y.setInt(...) don't make any sense.
I know the way they have created the "setInt" method is weird but from the looks of it they are simply creating a method within the "setValues" method and then setting the primitive values of the instance variables x & y to equal whatever int is passed to the method. However they say that you can't call any methods on a primitive so I'm a bit confused. Please help

The Code in question that's calling a method on a primitive is below:
//add the following method to data class:

Code: Select all

 public void setValues(int x, int y){   
this.x.setInt(x);   this.y.setInt(y); }  

//Then add the following statement: 

d.setValues(2, 2);
This is the entire code for the question:

Code: Select all

class Data {
    private int x = 0, y = 0;
    public Data(int x, int y){
        this.x = x; this.y = y;
    }
}
public class TestClass {
    public static void main(String[] args) throws Exception {
        Data d = new Data(1, 1);
        //add code here
    }
}
Which of the following options when applied individually will change the Data object referred to by the variable d to contain 2, 2 as values for its data fields?

Re: About Question enthuware.ocajp.i.v7.2.863 :

Posted: Tue Jan 15, 2013 4:06 pm
by admin
I am not sure I understand your question correctly, specially the part where you say, "they are simply creating a method within the "setValues" method" because you can't create method within a method in Java.

By "You cannot call any methods on a primitive.", it means exactly that. Primitives do not have any methods. So you can't call methods on primitive like you do for objects. Therefore, this.x.setInt(x);   this.y.setInt(y); are invalid because x and y are primitive ints. They don't have any methods.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.863 :

Posted: Tue Jan 15, 2013 4:22 pm
by lordnovas
Ohhhhh, sorry for being a bit confusing but you hit what I was getting at. Basically all objects like String, ArrayList, or whatever I create have methods already. However Primitives don't have any methods because their not objects. I guess I was over thinking the problem or something.

Thanks for the perspective.

Cheers
j-

Re: About Question enthuware.ocajp.i.v7.2.863 :

Posted: Mon Nov 03, 2014 9:55 am
by aditya
Hi Admin
can you please explain in details why
d = new Data(2, 2);
option above is wrong.?

my understanding says : by using d = new Data(2, 2); we actually now tell d object to call Data constructor and set respective values.
Please correct me where i am missing. :?

Re: About Question enthuware.ocajp.i.v7.2.863 :

Posted: Mon Nov 03, 2014 8:15 pm
by admin
Did you read the explanation provided with this option? It says, "This will create a new Data object and will not change the original Data object referred to be d."

The question asks you to change the values inside the Data object already created in the code. It doesn't want you to create a new Data object.

Re: About Question enthuware.ocajp.i.v7.2.863 :

Posted: Thu Jul 22, 2021 3:23 pm
by enthunoob
I think option 5 wouldn't point to the same object anyway as it would require to recompile the project. So as d will be pointing to a different object anyway, I prefer option 2 :D