I really need help on these 12 questions about java. 1) How can you tell a metho
ID: 3728336 • Letter: I
Question
I really need help on these 12 questions about java.
1) How can you tell a method is a constructor?
2) Would public void cheese() be considered a constructor?
3) Does it make sense to have private or void accessor method?
4) Would public void setName() be a good mutator declaration?
5) How can you tell the difference between instance and class variables?
6) Can we write name = name; and what would it mean?
7) How can you tell which version of the constructor is being called?
8) What does the . operator do for objects?
9) Can you use a loop to implement calcSubTotal?
10) Can you tell when and where we do the recursion in run()?
11) What does this refer to?
12) Give the code to implement public void setName(String name) { … }.
Explanation / Answer
1. Constrcutrs are special type of methods, Methods will initialize variables or execute code when we call with object. constructors will be invoked using new operator even constrcutor initalizes variables and eecute the block of code as method.
2.
2. No, if the method having same name as class it must have return type else it will be treated as constructor
3. yes private is acces modifier and if we used this it puts scope of the accessbilty on methods or constrcuotrs or variables. but void is the return type it specifies the type of the returning value
4. yes it is good mutator because we are not getting private files value but we are setting private field value.
5 Instance variable and class variables are declared in class level, instance variables values are not shared by any other object but class variables are shared by all the objects of that class
ex :
private int a; // example for instance variable
private static int a; // example for class variable
6. yes we can write it assigns the value of RHS to LHS. in case of primitive data type in case of object like obj1=obj2 it copies the reference the obj1
int a=10, b;
b = a; now b is having value 10;
7. if we dont write constructor then system will call its default constructor.
8. .(dot) it is a access operator which will be used to access methods or variables of objects Ex: obj.variable, obj.method();
9. ye we can use loop to implement calSubTotal by using operator like sum+=value[i]; sum/=value[i];
11. this pointer is pointer which referes to the current object. it refers the instance methods or construcotr
12. public void setName(String name)
{
this.name = name;
}