SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Defining Classes Ii
Exercise:
Programming Projects
Question:4 | ISBN:9780132830317 | Edition: 5

Question

You are interested in keeping track of the team members and competition information for your school’s annual entries in computer science programming competitions. 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 competitions 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 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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

 Competition Class

public class Competition {
    private String competitionName, winningTeam, secondPlace;

    public Competition(String competitionName, String winningTeam, String secondPlace, int year) {
        this.competitionName = competitionName;
        this.winningTeam = winningTeam;
        this.secondPlace = secondPlace;
        this.year = year;
    }
    private int year;
    
    public Competition() {
        this.competitionName = null;
        this.winningTeam = null;
        this.secondPlace =  null;
        this.year = 0000;
    }
    
    public Competition(Competition competition){
        this.competitionName = competition.getCompetitionName();
        this.winningTeam = competition.getWinningTeam();
        this.secondPlace = competition.getSecondPlace();
        this.year = competition.getYear();
    
    }
    
    public String getCompetitionName() {
        return competitionName;
    }

    public void setCompetitionName(String competitionName) {
        this.competitionName = competitionName;
    }

    public String getWinningTeam() {
        return winningTeam;
    }

    public void setWinningTeam(String winningTeam) {
        this.winningTeam = winningTeam;
    }

    public String getSecondPlace() {
        return secondPlace;
    }

    public void setSecondPlace(String secondPlace) {
        this.secondPlace = secondPlace;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
    
    public String toString(){
       return String.format("%-20s%s%n%-20s%s%n%-20s%s%n%-20s%s%n",
                "Competition Name :", getCompetitionName(),
                "Year :", getYear(),
                "Winning Team ",getWinningTeam(),
                "Second Place :", getSecondPlace());

    }
    
}


Team Class 

public class Team {
    private String teamName;
    private Object teamMembers[];
    private Object competitions[];
    
    public Team(String teamName, Object[] teamMembers, Object[] competitions) {
        this.teamName = teamName;
        this.teamMembers = teamMembers;
        this.competitions = competitions;
    }
    
    public Team() {
        this.teamName = null;
        this.teamMembers = null;
        this.competitions = null;
    }
    
    public Team(Team team){
        this.teamName = team.getTeamName();
        this.teamMembers = new Object[team.teamMembers.length];
        System.arraycopy(team.getTeamMembers(), 0, this.teamMembers,
              0, team.getTeamMembers().length);
        this.competitions = new Object[team.teamMembers.length];
        System.arraycopy(team.getCompetitions(), 0, this.competitions,
                0, team.getCompetitions().length);
        
    }
    
    public String getTeamName() {
        return teamName;
    }

    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }

    public Object[] getTeamMembers() {
        return teamMembers;
    }

    public void setTeamMembers(Object[] teamMembers) {
        this.teamMembers = teamMembers;
    }

    public Object[] getCompetitions() {
        return competitions;
    }

    public void setCompetitions(Object[] competitions) {
        this.competitions = competitions;
    }
    
    public String toString(){
        int x =0;
        String teammembers = "";
        for (Object teamMember : teamMembers) {
            teammembers += teamMember +"    ";
            x++;
            if (x%2 ==0){
            teammembers += "\n";
            }
        }
        String competitiondata = "\n";
        competitiondata += competitions[0].toString()+"\n"+competitions[1].toString();
        return getTeamName()+"\n"+teammembers +competitiondata;
    }
}

Demo

import java.util.Scanner;

public class TeamCompetitionsDemo {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        input.useDelimiter("\n");
        //team 1 using default constructor
        Team team1 = new Team();
        team1.setTeamName("Team 6");
        Object[] teammembers = new Object[]{"Micah", "Jose",
            "Person3", "Vanessa"};
        team1.setTeamMembers(teammembers);
        Competition competition1 = new Competition();
        
        competition1.setCompetitionName("Computer Science Competition ");
        competition1.setWinningTeam("Team 1");
        competition1.setSecondPlace("Team 6");
        competition1.setYear(2011);
        Competition competition2 = new Competition();
        competition2.setCompetitionName("Computer Science Competition 2");
        competition2.setWinningTeam("Team 6");
        competition2.setSecondPlace("Team 2");
        competition2.setYear(2014);
        Object[] competitions = new Object[]{competition1, competition2};
        team1.setCompetitions(competitions);
        System.out.println(team1);
        
        //team 2 using constructor
        Object[] team2members = new Object[]{"Aaron", "Jonny",
            "Hippie", "Tron"};
        Competition team2Competition = new Competition("Ruby Competition ","Team 7","Team 2",1957);
        Competition team2Competition2 = new Competition("PHP Competition ","Team 11", "Team 12",1965);
        Object[] team2Competitions = new Object[]{team2Competition,
            team2Competition2};
        Team team2 = new Team("Team Two", team2members, team2Competitions);
        System.out.println(team2);
        
        //CopyConstructor used to create similar team object and competition
        //Slight Details changed in team 3 do not effect team 1
        Team team3 = new Team(team1);
        Object[] team3Members = new Object[]{"Micah", "Jose",
            "Rafael", "Vanessa"};
        team3.setTeamMembers(team3Members);
        Competition team3Competition = new Competition(competition1);
        team3Competition.setYear(2020);
        Competition team3Competition2 = new Competition(competition2);
        team3Competition2.setCompetitionName("Data Analysis Competition");
        team3Competition2.setYear(2027);
        Object[] team3Competitions = new Object[]{team3Competition, team3Competition2};
        team3.setCompetitions(team3Competitions);
        System.out.println(team3);
        
    }

}


Sample out put

 


//run:
//Team 6
//Micah    Jose    
//Person3    Vanessa    
//
//Competition Name :  Computer Science Competition 
//Year :              2011
//Winning Team        Team 1
//Second Place :      Team 6
//
//Competition Name :  Computer Science Competition 2
//Year :              2014
//Winning Team        Team 6
//Second Place :      Team 2
//
//Team Two
//Aaron    Jonny    
//Hippie    Tron    
//
//Competition Name :  Ruby Competition 
//Year :              1957
//Winning Team        Team 1
//Second Place :      Team 2
//
//Competition Name :  PHP Competition 
//Year :              1965
//Winning Team        Team 11
//Second Place :      Team 12
//
//Team 6
//Micah    Jose    
//Rafael    Vanessa    
//
//Competition Name :  Computer Science Competition 
//Year :              2020
//Winning Team        Team 1
//Second Place :      Team 6
//
//Competition Name :  Data Analysis Competition
//Year :              2027
//Winning Team        Team 6
//Second Place :      Team 2
//
//BUILD SUCCESSFUL (total time: 0 seconds)---ccsfcs111b

0 0

Discussions

Post the discussion to improve the above solution.