-
Help needed before 25/1/01!!!!
Can anyone out there explain what constructors and accessors actually do before
my exam on 25/1/01???
No-one, even qualified programmers, seem to be able to give me an intelligible
answer.
If constructors pull together the other components such as accessors and
variables etc, then why do we need methods?
All answers not including foreign terminology are welcome.
-
Re: Help needed before 25/1/01!!!!
si
"Chris reid" <christopher.reid@easington.gov.uk> wrote:
>
>Can anyone out there explain what constructors and accessors actually do
before
>my exam on 25/1/01???
>
>No-one, even qualified programmers, seem to be able to give me an intelligible
>answer.
>
>If constructors pull together the other components such as accessors and
>variables etc, then why do we need methods?
>
>All answers not including foreign terminology are welcome.
-
Re: Help needed before 25/1/01!!!!
Constructors do two very important things (probably more). First, they set
aside memory in your computer where the variables and methods will be stored.
Second, they allow the programmer to initialize any variables that he or
she wants the object to have from the start. (Sorry about how long this
post is. I hope it helps.)
For example, let's pretend that we have a class called Dinosaur (the full
code for this class is at the end of this post). We want our dinosaurs to
have some methods and some variables. Let's say that our dinosaur class
has an eat method and a sleep method (because dinosaurs get grumpy without
enough food or rest, and God knows we don't want grumpy dinosaurs on our
hands). Let's also say that our dinosaur class has two variables, height
and weight, that help describe the dinosaur. So this class describes some
abstract notion of dinosaurs. We know that every dinosaur eats and sleeps
and has some weight and height, but we don't know anything about any particular
dinosaur.
To start talking about a particular dinosaur, let's call him Tim, we have
to instantiate a dinosaur. Instantiate is just a fancy word for "create
an instance of". To do this we write the following code somewhere in our
program (for instance, in the main method):
Dinosaur tim = new Dinosaur();
Now we have a dinosaur in our program, called tim who can eat and sleep
and has some height and weight. To really understand constructors, we need
to look more closely at that line of code. The first word "Dinosaur" tells
our program what type of creature tim is going to be, so that the computer
knows what he can and can't do and how much space to put aside for him.
The word "tim" is simply how we will refer to this instance throughout the
rest of the program. If we want tim to eat, we will say tim.eat(); or some
such thing. The other side of the equals sign is far more interesting.
The word "new" is a Java keyword that lets the compiler know that the next
word is going to be a constructor for tim. So, the word "Dinosaur()" is
the constructor for tim. This means that somewhere in the Dinosaur class
we will have a method called Dinosaur() that will probably look like this:
public Dinosaur()
{
// Maybe we do something here, maybe not
}
So now we have a dinosaur named tim who has some height and some weight
and can eat and sleep, but we still don't know exactly what a constructor
does (except that before we called the constructor, tim didn't exist, and
now he does). Right now, tim has a variable for his height and weight, but
they haven't been set to anything (which in Java means that they are equal
to 0). It is very strange to have a dinosaur that doesn't have any height
and weight, so we should do something about that. We could type
tim.height = 100;
tim.weight = 100;
but what we really want is for the program to give tim a height and weight
as soon as he comes into existence. We do this with the constructor:
public Dinosaur()
{
height = 100;
weight = 100;
}
Now, as soon as tim comes into existence, he has a height and a weight.
We have constructed him properly (thus the name constructor).
However, there is a slight problem with this way of doing things. To
understand this problem, we've got to look at Betty. Who's Betty? Another
dinosaur, of course. So, our program has the following code in it somewhere:
Dinosaur betty = new Dinosaur();
Now, betty has been created and tim has someone to play with. But wait,
when a dinosaur is constructed, his or her height and weight is set to 100.
Betty is a girl dinosaur, her height and weight shouldn't be 100, her height
should be 75 and her weight should be 60 (but people will tell her that she
doesn't look a pound over 50). How do we remedy this? Well, I guess we
could change our code in the Dinosaur class so that the constructor sets
a new dinosaur's height to 75 and weight to 60, but then tim's height and
weight will be wrong when we create him. So what do we do? We provide arguments
to the constructor:
Dinsoaur betty = new Dinosaur(75,60);
This line of code requires another constructor in the Dinosaur class.
So the Dinosaur class will have these two constructors:
// the constructor that tim will use
public Dinosaur()
{
height = 100;
weight = 100;
}
// the constructor that betty will use
public Dinosaur(int h, int w)
{
height = h;
weight = w;
}
Now we can create a default dinosaur, with the new Dinosaur() constructor,
whose height and weight will be 100, or, using the second constructor, we
can create a dinosaur whose height and weight is anything that we tell it
to be. How does the program know which constructor you want to use? It
looks at the arguments you supply and it finds the one whose arguments match
the type of arguments you supplied. That's why we had to write public Dinosaur(int
h, int w) as the head of the second constructor, so that we could pass it
two integers.
Hopefully, now you understand what constructors do. They create a new
instance of a data type (like Dinosaur) and initialize important variables.
On a more basic level, they set aside memory space in the computer and tell
the computer what values to store into that space.
You also asked about accessors. To explain accessors, we can look to
Tim and Betty again for an explaination.
In the Dinosaur class we declared two variables height and weight. This
is different from when we initialized them (gave them values) in the constructors.
Declaring these variables happens inside the class, but outside of any method.
The declaration simply tells the computer what type of data is going to
be put in height and weight. In the Dinosaur class, height and weight are
integers, so we would write:
int height;
int weight;
// all of the constructors and methods go down here
Do you remember earlier when I said that one of the ways that we could
have set tim's height and weight was to simply type
tim.height = 100;
tim.weight = 100;
Now, while this is perfectly valid, it is bad programming practice. Why
is it bad programming practice? Because we don't want a program that can
screw with a dinosaur's height and weight willy-nilly. If a dinosaur found
it's height and weight changing for no reason it might get upset (wouldn't
you?). To make sure that we don't mess with any of our dinosaurs' heights
and weights (and to make sure that nobody who uses our code does either)
we should declare height and weight to be private variables, like so:
private int height;
private int weight;
Private variables work just like regular variables, except you can only
access them from within the class. That way, other parts of the program
can't screw with them. The only problem now is that sometimes you may have
a legitimate reason for needing to access these variables from other parts
of the program. For instance, let's pretend that your program needs to check
a dinosaur's weight before it lets the dinosaur eat. Before we made the
weight variable private, the code might have looked like this:
if(tim.weight < 80)
{
tim.eat();
}
Since we made weight a private variable, however, this would cause an
error when compiling. So, we create what are called accessor methods. These
are methods in the Dinosaur class that do nothing but return the variable
in question. For instance, the Dinosaur class might have the following methods:
public int getWeight()
{
return weight;
}
public int getHeight()
}
return height;
}
And the original program might look like this:
if(tim.getWeight() < 80)
{
tim.eat();
}
This is how you get around the private restriction. Just remember that
accessor functions and private variables are not required by the language,
they are simply good programming practice. Constructors, on the other hand,
are required for the program to run correctly.
Well, I hope all of this helped. Sorry if it is too simplistic for you.
I had no idea how much you do or don't know about Java, so I tried to make
it very easy to understand. Good luck on your test. The full code for the
program that I was talking about is below...
// This is the Dinosaur class
public class Dinosaur
{
private int height;
private int weight;
// the constructor that tim will use
public Dinosaur()
{
height = 100;
weight = 100;
}
public Dinosaur(int h, int w)
{
height = h;
weight = w;
}
public int getWeight()
{
return weight;
}
public void eat()
{
System.out.println("Eating...");
}
public void sleep()
{
System.out.println("Sleeping...");
}
}
// This class is the class that runs your program.
public class DinoProgram
{
public static void main(String[] args)
{
Dinosaur tim = new Dinosaur();
Dinosaur betty = new Dinosaur(75,60);
tim.sleep();
betty.sleep();
if(tim.getWeight() < 80)
{
tim.eat();
}
if(betty.getWeight() < 80)
{
betty.eat();
}
}
}
-
Re: Help needed before 25/1/01!!!!
Shayne,
You should write a beginning Java book! Not only was that explanation easy
to understand, it was fun to read!
Mike
"Shayne" <soneill@mindspring.com> wrote:
>
>Constructors do two very important things (probably more). First, they
set
>aside memory in your computer where the variables and methods will be stored.
> Second, they allow the programmer to initialize any variables that he or
>she wants the object to have from the start. (Sorry about how long this
>post is. I hope it helps.)
>
> For example, let's pretend that we have a class called Dinosaur (the
full
>code for this class is at the end of this post). We want our dinosaurs
to
>have some methods and some variables. Let's say that our dinosaur class
>has an eat method and a sleep method (because dinosaurs get grumpy without
>enough food or rest, and God knows we don't want grumpy dinosaurs on our
>hands). Let's also say that our dinosaur class has two variables, height
>and weight, that help describe the dinosaur. So this class describes some
>abstract notion of dinosaurs. We know that every dinosaur eats and sleeps
>and has some weight and height, but we don't know anything about any particular
>dinosaur.
> To start talking about a particular dinosaur, let's call him Tim, we
have
>to instantiate a dinosaur. Instantiate is just a fancy word for "create
>an instance of". To do this we write the following code somewhere in our
>program (for instance, in the main method):
>
>Dinosaur tim = new Dinosaur();
>
> Now we have a dinosaur in our program, called tim who can eat and sleep
>and has some height and weight. To really understand constructors, we need
>to look more closely at that line of code. The first word "Dinosaur" tells
>our program what type of creature tim is going to be, so that the computer
>knows what he can and can't do and how much space to put aside for him.
>The word "tim" is simply how we will refer to this instance throughout the
>rest of the program. If we want tim to eat, we will say tim.eat(); or some
>such thing. The other side of the equals sign is far more interesting.
>The word "new" is a Java keyword that lets the compiler know that the next
>word is going to be a constructor for tim. So, the word "Dinosaur()" is
>the constructor for tim. This means that somewhere in the Dinosaur class
>we will have a method called Dinosaur() that will probably look like this:
>
>public Dinosaur()
>{
> // Maybe we do something here, maybe not
>}
>
> So now we have a dinosaur named tim who has some height and some weight
>and can eat and sleep, but we still don't know exactly what a constructor
>does (except that before we called the constructor, tim didn't exist, and
>now he does). Right now, tim has a variable for his height and weight,
but
>they haven't been set to anything (which in Java means that they are equal
>to 0). It is very strange to have a dinosaur that doesn't have any height
>and weight, so we should do something about that. We could type
>
> tim.height = 100;
> tim.weight = 100;
>
> but what we really want is for the program to give tim a height and weight
>as soon as he comes into existence. We do this with the constructor:
>
>public Dinosaur()
>{
> height = 100;
> weight = 100;
>}
>
> Now, as soon as tim comes into existence, he has a height and a weight.
> We have constructed him properly (thus the name constructor).
> However, there is a slight problem with this way of doing things. To
>understand this problem, we've got to look at Betty. Who's Betty? Another
>dinosaur, of course. So, our program has the following code in it somewhere:
>
>Dinosaur betty = new Dinosaur();
>
> Now, betty has been created and tim has someone to play with. But wait,
>when a dinosaur is constructed, his or her height and weight is set to 100.
> Betty is a girl dinosaur, her height and weight shouldn't be 100, her height
>should be 75 and her weight should be 60 (but people will tell her that
she
>doesn't look a pound over 50). How do we remedy this? Well, I guess we
>could change our code in the Dinosaur class so that the constructor sets
>a new dinosaur's height to 75 and weight to 60, but then tim's height and
>weight will be wrong when we create him. So what do we do? We provide
arguments
>to the constructor:
>
>Dinsoaur betty = new Dinosaur(75,60);
>
> This line of code requires another constructor in the Dinosaur class.
> So the Dinosaur class will have these two constructors:
>
>// the constructor that tim will use
>public Dinosaur()
>{
> height = 100;
> weight = 100;
>}
>
>// the constructor that betty will use
>public Dinosaur(int h, int w)
>{
> height = h;
> weight = w;
>}
>
> Now we can create a default dinosaur, with the new Dinosaur() constructor,
>whose height and weight will be 100, or, using the second constructor, we
>can create a dinosaur whose height and weight is anything that we tell it
>to be. How does the program know which constructor you want to use? It
>looks at the arguments you supply and it finds the one whose arguments match
>the type of arguments you supplied. That's why we had to write public Dinosaur(int
>h, int w) as the head of the second constructor, so that we could pass it
>two integers.
> Hopefully, now you understand what constructors do. They create a new
>instance of a data type (like Dinosaur) and initialize important variables.
> On a more basic level, they set aside memory space in the computer and
tell
>the computer what values to store into that space.
>
> You also asked about accessors. To explain accessors, we can look to
>Tim and Betty again for an explaination.
> In the Dinosaur class we declared two variables height and weight. This
>is different from when we initialized them (gave them values) in the constructors.
> Declaring these variables happens inside the class, but outside of any
method.
> The declaration simply tells the computer what type of data is going to
>be put in height and weight. In the Dinosaur class, height and weight are
>integers, so we would write:
>
>int height;
>int weight;
>
>// all of the constructors and methods go down here
>
> Do you remember earlier when I said that one of the ways that we could
>have set tim's height and weight was to simply type
>
> tim.height = 100;
> tim.weight = 100;
>
> Now, while this is perfectly valid, it is bad programming practice.
Why
>is it bad programming practice? Because we don't want a program that can
>screw with a dinosaur's height and weight willy-nilly. If a dinosaur found
>it's height and weight changing for no reason it might get upset (wouldn't
>you?). To make sure that we don't mess with any of our dinosaurs' heights
>and weights (and to make sure that nobody who uses our code does either)
>we should declare height and weight to be private variables, like so:
>
>private int height;
>private int weight;
>
> Private variables work just like regular variables, except you can only
>access them from within the class. That way, other parts of the program
>can't screw with them. The only problem now is that sometimes you may have
>a legitimate reason for needing to access these variables from other parts
>of the program. For instance, let's pretend that your program needs to
check
>a dinosaur's weight before it lets the dinosaur eat. Before we made the
>weight variable private, the code might have looked like this:
>
>if(tim.weight < 80)
>{
> tim.eat();
>}
>
> Since we made weight a private variable, however, this would cause an
>error when compiling. So, we create what are called accessor methods.
These
>are methods in the Dinosaur class that do nothing but return the variable
>in question. For instance, the Dinosaur class might have the following
methods:
>
>public int getWeight()
>{
> return weight;
>}
>
>public int getHeight()
>}
> return height;
>}
>
> And the original program might look like this:
>
>if(tim.getWeight() < 80)
>{
> tim.eat();
>}
>
> This is how you get around the private restriction. Just remember that
>accessor functions and private variables are not required by the language,
>they are simply good programming practice. Constructors, on the other hand,
>are required for the program to run correctly.
>
> Well, I hope all of this helped. Sorry if it is too simplistic for you.
> I had no idea how much you do or don't know about Java, so I tried to make
>it very easy to understand. Good luck on your test. The full code for
the
>program that I was talking about is below...
>
>// This is the Dinosaur class
>public class Dinosaur
>{
> private int height;
> private int weight;
>
> // the constructor that tim will use
> public Dinosaur()
> {
> height = 100;
> weight = 100;
> }
>
> public Dinosaur(int h, int w)
> {
> height = h;
> weight = w;
> }
>
> public int getWeight()
> {
> return weight;
> }
>
> public void eat()
> {
> System.out.println("Eating...");
> }
>
> public void sleep()
> {
> System.out.println("Sleeping...");
> }
>}
>
>// This class is the class that runs your program.
>public class DinoProgram
>{
> public static void main(String[] args)
> {
> Dinosaur tim = new Dinosaur();
> Dinosaur betty = new Dinosaur(75,60);
>
> tim.sleep();
> betty.sleep();
>
> if(tim.getWeight() < 80)
> {
> tim.eat();
> }
> if(betty.getWeight() < 80)
> {
> betty.eat();
> }
> }
>}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|