User-defined classes.

Illegal expressions, constructors,

Page 1 of 1

5 Replies - 1461 Views - Last Post: 21 October 2007 - 03:28 PM Rate Topic: -----

#1 entschieden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-October 07

User-defined classes.

Posted 20 October 2007 - 07:15 PM

Hello,

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.

Is This A Good Question/Topic? 0
  • +

Replies To: User-defined classes.

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5883
  • View blog
  • Posts: 12,765
  • Joined: 16-October 07

Re: User-defined classes.

Posted 20 October 2007 - 08:17 PM

View Postentschieden, on 20 Oct, 2007 - 10:15 PM, said:

a)Illustrate.incrementCount();
d)Illustrate.x++;


Hint, static methods are often called "class methods." So, Illustrate.incrementCount(); works, because it's static. Illustrate.x++; fails in two ways, it's not static, so a class instance is required. Also, it's private, so the question is moot.


View Postentschieden, on 20 Oct, 2007 - 10:15 PM, said:

Where is the third constructor?


I'm with you on this one, I only see those two constructors.
Was This Post Helpful? 0
  • +
  • -

#3 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: User-defined classes.

Posted 20 October 2007 - 08:19 PM

View Postentschieden, on 20 Oct, 2007 - 07:15 PM, said:

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.


I'm assuming b) is supposed to be "Illustrate.z++;", otherwise it's wrong as well. I think you need to re-read the section on access modifiers (public, private, protected, package/default) and scope modifiers(e.g. static). Once you have a good grasp on those, it will be very obvious why d is wrong and a is not. This is a very important concept to learn as it correlates directly to the object oriented principle of encapsulation.

View Postentschieden, on 20 Oct, 2007 - 07:15 PM, said:

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?


I don't see a third constructor for Illustrator. Count has one though. Is that what you meant?

This post has been edited by Programmist: 20 October 2007 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

#4 entschieden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-October 07

Re: User-defined classes.

Posted 21 October 2007 - 02:28 PM

View Postbaavgai, on 20 Oct, 2007 - 08:17 PM, said:

View Postentschieden, on 20 Oct, 2007 - 10:15 PM, said:

a)Illustrate.incrementCount();
d)Illustrate.x++;


Hint, static methods are often called "class methods." So, Illustrate.incrementCount(); works, because it's static. Illustrate.x++; fails in two ways, it's not static, so a class instance is required. Also, it's private, so the question is moot.


View Postentschieden, on 20 Oct, 2007 - 10:15 PM, said:

Where is the third constructor?


I'm with you on this one, I only see those two constructors.



Hi baavgai,

Thanks for your reply. Static methods are often called class methods.
Right the static method belongs to a class rather than the object of a class. In the classDefinition section, private int x is does not have the modifier 'static'. While all the others have the 'static' modifier. Now I understand. It doesn't have a class instance. Great makes sense now.

By the way I accidentally left out part of the possible answers. They all should have 'illustrate' at the beginning.

As for the constructors, let's see... I think another person commented on that. Thanks again.

E.
Was This Post Helpful? 0
  • +
  • -

#5 entschieden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-October 07

Re: User-defined classes.

Posted 21 October 2007 - 02:49 PM

View PostProgrammist, on 20 Oct, 2007 - 08:19 PM, said:

View Postentschieden, on 20 Oct, 2007 - 07:15 PM, said:

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.


I'm assuming b) is supposed to be "Illustrate.z++;", otherwise it's wrong as well. I think you need to re-read the section on access modifiers (public, private, protected, package/default) and scope modifiers(e.g. static). Once you have a good grasp on those, it will be very obvious why d is wrong and a is not. This is a very important concept to learn as it correlates directly to the object oriented principle of encapsulation.

View Postentschieden, on 20 Oct, 2007 - 07:15 PM, said:

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?


I don't see a third constructor for Illustrator. Count has one though. Is that what you meant?


Programmist,

Yes illustrate.x++ is the illegal statement because in the classDefinition it doesn't have a static modifier. I must remember to disable emoticons! I wasn't trying to be cute or anything.

Access modifiers? Yes I should brush up on those topics. From baavgai's post I recalled why illustrate.x++ is the illegal statement. Since the access modifiers determine accessibility of the members based on the type of modifier, x does not have a 'static' modifier, so it does not get access.

You're right I should go over access modifiers, scope modifiers, and encapsulation. I read over my notes again and see why illustrate.xx++ is the illegal statement.

As for the three constructors, you said the third is count? You're referring to public class illustrate right? Not the simple example of a constructor in class count?
Well if you are referring to public class illustrate, I was expecting a constructor to accept a value as an argument. Maybe I'll go read up on constructors again to understand how the count works as a constructor.

Thanks.
E.
Was This Post Helpful? 0
  • +
  • -

#6 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: User-defined classes.

Posted 21 October 2007 - 03:28 PM

There are two constructors in the Illustrate class and one in the Count class. If you don't know where the ones is in the Count class, Google "default constructors in Java" and you should find something.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1