You are interested in keeping track of the team members and competition infor- mation for your school’s annual entries in computer science programming com- petitions. Each team consists of exactly four team members. Every year your team competes in two competitions. As an initial start for your database, create a class named Team that has the following instance variables:
// Name for the team
String teamName;
// Names for each team members.
String name1, name2, name3, name4;
// Info on each competition
Competition competition1, competition2;
Note that there is a much better way to represent the team members and competi- tions using arrays; this is covered in a subsequent chapter. The class should also have a method that outputs all the names of all team members and the competition information to the console.
The Competition class contains variables to track the following:
String: Name of the competition, Name of the winning team, Name of the runner-up Integer: Year of the competition
Implement the Team and Competition classes with appropriate constructor, accessor, and mutator methods. In entering data for past competitions, you note that an entry is usually very similar to the previous year’s entry. To help with the data entry, create a deep copy constructor for the Team class. Test your copy constructor by creating a copy of an existing team object, changing the competition information for the copy, and outputting the data for the original and the copy. The original object should be unchanged if your deep copy constructor is working properly.

I am having trouble with this portion

--To help with the data entry, create a deep copy constructor for the Team class. Test your copy constructor by creating a copy of an existing team object, changing the competition information for the copy, and outputting the data for the original and the copy. The original object should be unchanged if your deep copy constructor is working properly.

Recommended Answers

All 4 Replies

public class Team {

    String teamName;
    String name1, name2, name3, name4;
    Competition competition1, competition2;

    public Team()
    {
        teamName= "none yet";
        name1 = "none yet";
        name2 = "none yet";
        name3 = "none yet";
        name4 = "none yet";
        competition1 = null;
        competition2 = null;
    }
    public Team(Team team)
    {
        if(team !=null)
        {
        this.teamName = team.teamName;
        this.name1 = team.name1;
        this.name2 = team.name2;
        this.name3 = team.name3;
        this.name4 = team.name4;
        this.competition1 =team.competition1;
        this.competition2 =team.competition2;
        }
        else
        {
            System.out.println("error");
            System.exit(0);
        }   
    }
    public Team(String teamName, String name1, String name2, String name3, String name4,Competition competition1, Competition competition2)
    {
        set_teamName(teamName);
        set_name1(name1);
        set_name2(name2);
        set_name3(name3);
        set_name4(name4);
        set_competition(competition1,competition2);
    }
    public void set_teamName(String teamName)
    {
        this.teamName = teamName;
    }
    public void set_name1(String name1)
    {
        this.name1 = name1;
    }
    public void set_name2(String name2)
    {
        this.name2 = name2;
    }
    public void set_name3(String name3)
    {
        this.name3 = name3;
    }
    public void set_name4(String name4)
    {
        this.name4 = name4;
    }
    public String get_teamName()
    {
        return teamName;
    }
    public String get_name1()
    {
        return name1;
    }
    public String get_name2()
    {
        return name2;
    }
    public String get_name3()
    {
        return name3;
    }
    public String get_name4()
    {
        return name4;
    }
    public void set_competition(Competition competition1,Competition competition2)  
    {
        this.competition1 = competition1;
        this.competition2 = competition2;
    }


    public Competition get_competition1()
    {
        return competition1;
    }
    public Competition get_competition2()
    {
        return competition2;
    }
    public String toString()
    {
        return "Team name: "+teamName+ "\nName 1: "+name1+"\nName 2: "+name2+ "\nName 3: "+name3+"\nName 4: "+name4+ 
                "\n\nCompetition 1: "+get_competition1()+
                "\n\nCompetition 2: "+get_competition2();
    }

    public void final_print()
    {
        Team b = new Team("The Legends","Ross","Scott","Viktor","Josh",
                new Competition("Best Djs Awards: ","The ballers" ,"The legends",1989),
                new Competition("Best soccer players awards: ", "The legends","the ballers",1990));
        Team f = new Team(b);
        //f.set_competition(competition1, competition2);
        f.set_name1("Ross");
        f.set_name2("blah");
        f.set_teamName("ballerist");
        f.set_name3("blah");
        f.set_name4("blah");
//      b.competition1.set_comp_name("hehe");
//      b.competition1.set_runner_up("");
//      b.competition1.set_winners("yay");
//      b.competition1.set_year(1000);
//      b.competition2.set_comp_name("blah");
//      b.competition2.set_runner_up("blah");
//      b.competition2.set_winners("yay");
//      b.competition2.set_year(2000);

        f.competition1.set_comp_name("blah");
        f.competition1.set_runner_up("blah");
        f.competition1.set_winners("yay");
        f.competition1.set_year(2000);
        f.competition2.set_comp_name("blah");
        f.competition2.set_runner_up("blah");
        f.competition2.set_winners("yay");
        f.competition2.set_year(2000);

        //System.out.println(a);
        //System.out.println(b);
        System.out.println(b);
        System.out.println(f);
//      System.out.println(competition1);
//      System.out.println(competition2);
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
Team c = new Team();
c.final_print();



    Competition Class-----------
    public class Competition {

    String comp_name, winners, runner_up;
    int year;

    public Competition()
    {
        comp_name = "no name yet";
        winners = "no name yet";
        runner_up = "no name yet";
        year =0;
    }

    public Competition(String newcomp_name, String newwinners, String newrunner_up, int newyear)
    {
        set_comp_name(newcomp_name);
        set_winners(newwinners);
        set_runner_up(newrunner_up);
        set_year(newyear);
    }

    public void set_comp_name(String newcomp_name)
    {
        comp_name = newcomp_name; 
    }
    public void set_winners(String newwinners)
    {
        winners = newwinners;
    }
    public void set_runner_up(String newrunner_up)
    {
        runner_up = newrunner_up;
    }
    public void set_year(int newyear)
    {
        year = newyear;
    }
    public String get_comp_name()
    {
        return comp_name;
    }
    public String get_winners()
    {
        return winners;
    }
    public String get_runner_up()
    {
        return runner_up;
    }
    public int get_year()
    {
        return year;
    }
    public String toString()
    {
        return "Competition name: "+get_comp_name()+" Winners: "+get_winners()+" Runners-up: "+ get_runner_up()+" Year: "+get_year();
    }

When you work with reference types (classes) when you say MyClass newClass = oldClass what you are doing is copying the reference. This means that newClass and oldClass actually point to the same memory.

What you would need to do in this case is instantiate a new object and copy the values that this class contains.

So if MyClass contains three values: int MyInteger, double MyDouble, double MySecondDouble you would need to perform the following...

MyClass newClass = new MyClass();
newClass.MyInteger = oldClass.MyInteger;
newClass.MyDouble = oldClass.MyDouble;
newClass.MySecondDouble = oldClass.MySecondDouble;

If you had a constructor for them...

MyClass newClass = new MyClass(oldClass.MyInteger, oldClass.MyDouble, oldClass.MySecondDouble);

or if this class also had a copy constructor ;)

MyClass newClass = new MyClass(oldClass);

In the second two cases, the code for these constructors would have to contain the last three lines of the first example to work properly.

It is important to know that things like integers, doubles, floats (any struct type) etc are value types. This means when you do int myInt = oldInt it is doing a bitwise copy of the value, not a copy of the memory reference.

This is the difference between a shallow copy and deep copy.

Your copy constructor seems to make an ordinary (shallow) copy of the old Team's values. As Ketsuekiame explained your new Team still refers to the same Competition objects as the original Team object. That's just an ordinary "shallow" copy. For a deep copy you must create new Competition objects (that are copies of the old ones) for youir new Team object.
The names are a slightly different case - because String objects are immutable (can never change) it makes no difference whether your new Team refers to the same name objects as the old Team, or makes copies of them.

And don't use the idea of copy constructors, that's C++ thinking.
Java has the Cloneable interface and its system to created deep copies of things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.