Java Forum / General / September 2006
Can we over-load "+" in Java?
Shawn - 14 Sep 2006 14:15 GMT Hi,
Suppose I have following classes:
class Person { ...
}
class Team { //how to have a member variable which holds an array of objects of Person?
... }
Person person1= new Person("John Smith"); Person person2= new Person("Linda King"); Person person3= new Person("Ed Johnson");
//here is what I want: Team BestTeam; BestTeam = person1 + person2; //now an object of class Team was created and this team has the two people
BestTeam = BestTeam + person3; //now Ed Johnson joined the team
BestTeam = BestTeam - person3; //now Ed Johnson left the team
Can we overload "+" and "-" to achieve the effect above? If not(which is my feeling), how can we achieve something similar? If we use a method (say,join(), left()), please give me full implementation of the method?
Thank you very much. I greatly appreciate it.
Paul Tomblin - 14 Sep 2006 14:34 GMT In a previous article, Shawn <shaw@nospam.com> said:
>my feeling), how can we achieve something similar? If we use a method >(say,join(), left()), please give me full implementation of the method? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Oh, I almost forgot it was September until I see the spate of "do my homework for me" posts.
Why don't you ask your professor or TA for help? That's what you're paying them for. Or better yet, drop the course now, because you're obviously unsuited for computer science.
 Signature Paul Tomblin <ptomblin@xcski.com> http://xcski.com/blogs/pt/ So logically, if she weighs the same as a duck, she's made of wood, and therefore a witch.
Shawn - 14 Sep 2006 15:02 GMT > In a previous article, Shawn <shaw@nospam.com> said: >> my feeling), how can we achieve something similar? If we use a method [quoted text clipped - 7 lines] > paying them for. Or better yet, drop the course now, because you're > obviously unsuited for computer science. This morning I was thinking about "Data abstraction"--building higher and higher level of abstracted data and providing its relevant operation, like "+" for person means "forming a team if no team created yet" or "join the team", "-" for person means "leaving the team".
My impression is no overloading in Java. Instead, other methods (like join(), left()) have to be provided. Since I am not clear how it will work, I asked for full implementation of the methods, if possible.
Ralf Seitner - 14 Sep 2006 15:14 GMT Shawn schrieb:
> Hi, > [quoted text clipped - 8 lines] > //how to have a member variable which holds an array of objects of > Person? [...] Hi! You do not know, how to use arrays? Read the Java Tutorial. http://java.sun.com/docs/books/tutorial/
> Can we overload "+" and "-" to achieve the effect above? If not(which is > my feeling), how can we achieve something similar? If we use a method > (say,join(), left()), please give me full implementation of the method? I wont give you such an implementation, and I think there are two things, user hate here: 1. top posting 2. OPs asking for a full implementation.
But I give you a hint. Simply use a List (java.util.List) and there are methods like add() and remove(). As of Java 1.5 there are generics, so you can add a member like: private List<Person> team;
But please read a book. And have a look at the API.
bye, Ralf
Shawn - 14 Sep 2006 15:33 GMT > But I give you a hint. Simply use a List (java.util.List) and there are > methods like add() and remove(). > As of Java 1.5 there are generics, so you can add a member like: > private List<Person> team; I want "team" to be an object of class Team, not a reference to a list which holds individual Person objects.
With the newly created data, instances of Team class, I will provide its relevant operation, like "merge teams", "split teams into several smaller teams" etc, just similar to operation in the real world.
After that, I will create another new data, Department class, which can have several teams. I will also provide Department's operations.
Am I thinking correctly in OO spirit or I am completely wrong?
Thank you very much.
Oliver Wong - 14 Sep 2006 15:51 GMT >> But I give you a hint. Simply use a List (java.util.List) and there are >> methods like add() and remove(). [quoted text clipped - 12 lines] > > Am I thinking correctly in OO spirit or I am completely wrong? I don't see anything particularly anti-OO. Why don't you show us what code you've got so far and we can comment on your design?
Here's a start:
class Person { }
class Team { Set<Person> members = new HashSet<Person>; }
- Oliver
Ralf Seitner - 14 Sep 2006 15:57 GMT Shawn schrieb:
>> But I give you a hint. Simply use a List (java.util.List) and there >> are methods like add() and remove(). [quoted text clipped - 3 lines] > I want "team" to be an object of class Team, not a reference to a list > which holds individual Person objects. Hi! I thought the list I called team to be a member of class Team, so that the persons, who belong to that team can be stored there. I just used that information you gave. You wanted to use an array as a member of class Team, but an Array has a fixed size, a List has not, so it might be better to use a List here, instead of the Array you mentioned. In some way, you will have to store, which persons belong to a team. Maybe List is not the best (depends to your purposes), but maybe it is. By using a collection, you can easily add or remove items. You can create a method public void joinTeam(Person p) { team.add(p); } bye, Ralf
Shawn - 14 Sep 2006 19:03 GMT Hi, Following is the my code. It works fine. I still appreciate any comments.
Thank you.
============Person.java==========
public class Person { private String sName; public Person() { //empty } public Person(String s) { this.sName = s; } public String getName() { return sName; } }
=========Team.java============= import java.util.*;
public class Team { private Person leader; private Vector members; //Vector is not good for this, but I am familiar with it. So just for now. public Team() { this.leader = new Person(); this.members = new Vector(); } public void setTeamLeader(Person p) { this.leader = p; } public Person getLeader() { return leader; } public void addMemeber(Person p) { this.members.addElement(p); } public void removeMember(Person p) { this.members.removeElement(p); } public void showMembers() { Person temP; temP = this.getLeader(); System.out.println("The team leader is: " + (String)temP.getName()); for (int i=0; i < members.size(); i++) { temP = (Person)members.elementAt(i); System.out.println(temP.getName()); } } }
=========Demo.java========== public class Demo { public static void main(String[] args) { Team DreamTeam = new Team(); Person person1 = new Person("John"); Person person2 = new Person("Ed"); Person person3 = new Person("Linda"); DreamTeam.addMemeber(person1); DreamTeam.addMemeber(person2); DreamTeam.addMemeber(person3); DreamTeam.setTeamLeader(person2); DreamTeam.showMembers(); DreamTeam.removeMember(person1); DreamTeam.showMembers(); } }
Shawn - 14 Sep 2006 19:26 GMT Hi, Following is my code. My "primitive" data structure is Person, then one level up, Team, then one more level up, Department. The program works fine.
I feel OO is fun, because at the level of Team, it doesn't care how Person class methods are implemented and at the level of Department, it doesn't even care about how Team and Person's methods are implemented, only if interface is fixed.
Thank you very much for your any feedbacks.
============Person.java==========
public class Person { private String sName; public Person() { //empty } public Person(String s) { this.sName = s; } public String getName() { return sName; } }
=========Team.java============= import java.util.*;
public class Team { private Person leader; private Vector members; //Vector is not good for this, but I am familiar with it. So just for now. public Team() { this.leader = new Person(); this.members = new Vector(); } public void setTeamLeader(Person p) { this.leader = p; } public Person getLeader() { return leader; } public void addMemeber(Person p) { this.members.addElement(p); } public void removeMember(Person p) { this.members.removeElement(p); } public void showMembers() { Person temP; temP = this.getLeader(); System.out.println("The team leader is: " + (String)temP.getName()); for (int i=0; i < members.size(); i++) { temP = (Person)members.elementAt(i); System.out.println(temP.getName()); } } }
=========Department.java======= import java.util.*;
public class Department { private Vector teams; public Department() { teams = new Vector(); } public void addTeam(Team t) { teams.addElement(t); } public void removeTeam(Team t) { teams.removeElement(t); } public void showAllMemebers() { System.out.println("Our department has the following members:"); Team temT; for (int i= 0; i < teams.size(); i++ ) { temT = (Team)teams.elementAt(i); temT.showMembers(); } } }
=========Demo.java========== public class Demo { public static void main(String[] args) { Team dreamTeam = new Team(); Person person1 = new Person("John"); Person person2 = new Person("Ed"); Person person3 = new Person("Linda"); dreamTeam.addMemeber(person1); dreamTeam.addMemeber(person2); dreamTeam.addMemeber(person3); dreamTeam.setTeamLeader(person2); dreamTeam.showMembers(); dreamTeam.removeMember(person1); dreamTeam.showMembers(); Team secondTeam = new Team(); Person person4 = new Person("Lee"); Person person5 = new Person("Tom"); secondTeam.addMemeber(person4); secondTeam.addMemeber(person5); secondTeam.setTeamLeader(person4); secondTeam.showMembers(); Department newDept = new Department(); newDept.addTeam(dreamTeam); newDept.addTeam(secondTeam); newDept.showAllMemebers(); } }
Oliver Wong - 14 Sep 2006 22:10 GMT > Thank you very much for your any feedbacks. > [quoted text clipped - 7 lines] > //empty > } Does it make sense to allow for a person without a name? If not, I'd get rid of the empty constructor.
> public Person(String s) > { [quoted text clipped - 14 lines] > private Vector members; //Vector is not good for this, but I am > familiar with it. So just for now. I recommend using a Set rather than a Vector or a List. Set doesn't allow duplicates, which means a person maybe no be a member of the same team twice, which intuitively makes more sense to me.
> public Team() > { > this.leader = new Person(); Similarly here, it might be better to let leader be "null" to present no leader, rather than have a person with no name.
> this.members = new Vector(); > } [quoted text clipped - 102 lines] > } > } So far looks good, but where's your method for merging teams or splitting them?
- Oliver
Shawn - 15 Sep 2006 13:38 GMT >> Thank you very much for your any feedbacks. >> [quoted text clipped - 10 lines] > Does it make sense to allow for a person without a name? If not, I'd > get rid of the empty constructor. Thank you for your feedback. I am confused here: first time, I didn't provide default constructor. But I have constructor public Person(String s). Compiler said that it is an error. So I added the code above and it is fine.
My impression is that once you have an non-default constructor, you must provide default constructor. Is this true?
I fully agree using Set instead of Vector. I used Vector because it is the first thing taught in my Java book. I know Set as a concept(no duplicates). I am curious what HashSet is good about.
> So far looks good, but where's your method for merging teams or > splitting them? I just want to practice "abstraction barrier" concept -- level up from Person to Team to Department. At each level, I don't need to worry about how the lower level methods are implemented. An analogy is from gates to transistors to logic circuits to CPU.
Ralf Seitner - 15 Sep 2006 13:47 GMT >>> ============Person.java========== >>> [quoted text clipped - 10 lines] > s). Compiler said that it is an error. So I added the code above and it > is fine. Hi! You provide a default constructor, as it is there (see above). If you have another constructor, but define no default constructor you cant use the default constructor anymore. The compiler just said, there is an error, because you did Person leader = new Person() in the default constructor of class Team and did not have a default constructor in class Person.
> My impression is that once you have an non-default constructor, you must > provide default constructor. Is this true? only if you want to have a default constructor.
bye, Ralf
Rogan Dawes - 15 Sep 2006 13:53 GMT >>> Thank you very much for your any feedbacks. >>> [quoted text clipped - 18 lines] > My impression is that once you have an non-default constructor, you > must provide default constructor. Is this true? The reason the compiler complained is that you are calling the Person constructor, without any parameters ( leader = new Person(); ), and there was no such constructor.
As Oliver said, it makes no sense to have a Person without a name, so it is better to have
leader = null;
to indicate that a leader has not yet been configured.
If you never call "new Person()", then you won't need a default constructor for that class.
> I fully agree using Set instead of Vector. I used Vector because it is > the first thing taught in my Java book. I know Set as a concept(no > duplicates). I am curious what HashSet is good about. HashSet is simply an implementation of the Set interface, with certain properties. Another implementation is TreeSet, which guarantees to return the entries according to a particular order (default Comparator, or user-supplied one).
>> So far looks good, but where's your method for merging teams or >> splitting them? [quoted text clipped - 3 lines] > how the lower level methods are implemented. An analogy is from gates to > transistors to logic circuits to CPU.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|