About Question enthuware.ocajp.i.v7.2.902 :
Moderator: admin
-
- Posts: 1
- Joined: Wed Jul 10, 2013 6:14 am
- Contact:
About Question enthuware.ocajp.i.v7.2.902 :
Good day, everyone!
Given:
class Triangle{
public int base;
public int height;
public double area = 0;
public Triangle(int base, int height){
this.base = base; this.height = height;
updateArea();
}
public void updateArea(){
double a = base*height/2;
area = a;
}
public void setBase(int b){ base = b; updateArea(); }
public void setHeight(int h){ height = h; updateArea(); }
}
Which variables are not accessible at class level?
According to explanation:
"class level" means static fields and they can be accessed from anywhere (i.e. static as well as non-static methods) in the class (and from outside the class depending on their accessibility).
But this class has no static fields, which means, that there are no variables accessible at class level at all in this class, which leads us to a conclusion, that all options are correct, am i right?
Given:
class Triangle{
public int base;
public int height;
public double area = 0;
public Triangle(int base, int height){
this.base = base; this.height = height;
updateArea();
}
public void updateArea(){
double a = base*height/2;
area = a;
}
public void setBase(int b){ base = b; updateArea(); }
public void setHeight(int h){ height = h; updateArea(); }
}
Which variables are not accessible at class level?
According to explanation:
"class level" means static fields and they can be accessed from anywhere (i.e. static as well as non-static methods) in the class (and from outside the class depending on their accessibility).
But this class has no static fields, which means, that there are no variables accessible at class level at all in this class, which leads us to a conclusion, that all options are correct, am i right?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.902 :
The question asks about the given code. In the given code there is no static method or block. So the instance fields are accessible everywhere in the given code. Yes, if there were a static method then they would be accessible in those methods.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Wed Dec 02, 2015 5:50 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.902 :
what about the constructor parameters pBase and pHeight ?
Couldn´t it be considered variables?
Couldn´t it be considered variables?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.902 :
Yes, they are variables as well and are accessible only within the constructor.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Fri Dec 08, 2017 6:04 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.902 :
So, shouldn't the correct answer include pBase and pHeight and be like the following?
- b, h, a, pBase, pHeight
- b, h, a, pBase, pHeight
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.902 :
Not necessarily. The options don't claim to include all such variables. Also, there is no option that says, "none of the above" or something like that. That means, the question doesn't want you to think about the variables pBase and pHeight. You need to select the best option among the ones that are given.adribo wrote:So, shouldn't the correct answer include pBase and pHeight and be like the following?
- b, h, a, pBase, pHeight
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Fri Aug 09, 2019 11:21 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.902 :
And if so, what would be the right answer?
Who is online
Users browsing this forum: No registered users and 34 guests