Just a few questions about User-Defined Classes. This seems like a general enough question, I hope.
Here is some code which I've already been tested on. I'm trying to understand why I got some of the answers wrong.
public class Illustrate
{
private int x;
private static int y;
public static int count;
public static int z;
public Illustrate()
{
x=1;
}
public Illustrate(int a)
{
x=a;
}
public void print()
{
System.out.println("x="+ x +",y="+ y +",count="+count);
}
public static void incrementCount()
{
count++;
}
}
First we were asked which statements were illegal:
a)Illustrate.incrementCount();
b).z++;
c)Illustrate.count++;
d)Illustrate.x++;
I chose 'a' but the correct answer was 'd'. I don't really understand why 'd' is right and 'a' is correct.
Nextly, We were asked how many constructors are in the class definition above. At the time I was rushing to finish the quiz so maybe I didn't think it through,
First of all, constructors guarantee that data members are initialized when the object is declared, it automatically executes when the class object is created, the name of the constructor is the name of the class, more than one contructor can be in a class, and the default constructor is one without parameters.
Here is a simple class definition:
class Count {
public static void main(String args[])
throws java.io.IOException
{
int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + " chars.");
}
}
This one seems easy enough to understand. The first line defines the class 'count'. Similarly public class illustrate
defines the class illustrate. In the case of the constructors in the public class illustrate, public Illustrate()
seems to me to be the default constructor. Is that right?
Since the constructor must have the same name as the class, I only see two constructors there:
public Illustrate()
public Illustrate(int a)
Where is the third constructor?
Thanks for your help ahead of time.
E.

New Topic/Question
Reply


MultiQuote




|