|
-
July 2nd, 2007, 09:23 AM
#1
Calling a class in another class
I have 2 classes, Event and Dear. I have created Event class with two variables which are Date and Type of Event. This class is known as the ADT Event class.
Another class Dear, has variables name, hphone, email, type of relation and event(which is supposed to be a stack).
How do I write the proper constructors, set and get and toString methods for the event part(highlighted) in the 'Dear' class?
Is the code below correct?
Code:
public Dear(String n, int hp, String e, String t, Stack ev)
{
setData(n, hp, e, t, ev);
}
private void setData(String n, int hp, String e, String t, Stack ev)
{
n = name;
hp = hphone;
e = email;
t = tor;
ev = event;
}
Code:
public Stack getEvent(){
return event;
}
Code:
public String toString()
{
return name + "\t" + hphone + "\t" + email + "\t" + tor "\t" + event;
}
-
July 6th, 2007, 02:26 AM
#2
Re: Calling a class in another class
What do you mean by proper? “Proper” for what?
The assignment in the constructor should be the other way around.
Plus, if you don’t want the Stack instances to by synchronized you should try cloning the parameter before assignment. Otherwise any change in the ev parameter after the constructor is called will change the event field of the class.
The toString method appends at the end of the string representation the event field. Being a Stack instance, its toString() doesn’t always looks nice. You should consider parsing the elements of the stack for a more appriopriate output.
-
July 6th, 2007, 04:27 AM
#3
Re: Calling a class in another class
This is a duplicate post - it's already been answered in this thread
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
|
Click Here to Expand Forum to Full Width
|