In: Computer Science
Complete the following C++ tasks:
a. Design a class named BaseballGame that has fields for two team
names and a final score for each team. Include methods to set and
get the values for each data field. Create the class diagram and
write the pseudocode that defines the class.
b. Design an application that declares three BaseballGame objects
and sets and displays their values.
c. Design an application that declares an array of 12 BaseballGame
objects. Prompt the user for data for each object, and display all
the values. Then pass each object to a method that displays the
name of the winning team or "Tie" if the score is a tie.
DO GIVE THUMPS UP....
ANS (a):HERE IS YOUR ANSWER FOR QUESTION 1
CLASS DIAGRAM FOR THE ABOVE PROBLEM IS GIVEN BELOW.
ANS (b) & ANS (C): I HAVE WRITTEN BOTH THE APPLICATION IN ONE CODE .
I TOOK OBJECT OF 4 TEAM INSTEAD OF 12, IF WANT TO CHANGE THE SIZE OF ARRAY, JUST CHANGE IT TO 12 IN FOR LOOP AND OBJECT OF ARRAYS.
CODE :
#include <iostream>
#include<string.h>
using namespace std;
//CLASS NAME
class BaseBallGame{
//INITIALIZATION OF MEMBERS AND METHODS
public:
string teamName;
int finalScore;
public:
//SET AND GETTER METHOD OF TAKING
AND PRINTING THE OUTPUT
void setter(string team, int
score){
teamName =
team;
finalScore =
score;
}
//GET METHOD
void getter(){
cout<<"Team Name: "<<teamName<<endl;
cout<<"Fina score: "<<finalScore<<endl;
}
};
int main(){
//declared three BaseballGame objects and sets and
displays their values
BaseBallGame game1;
BaseBallGame game2;
BaseBallGame game3;
cout<<"OUTPUT FOR APPLICATION NO
2"<<endl;
game1.setter("Los ANgels", 12);
game1.getter();
game2.setter("Golden State Warriors", 14);
game2.getter();
game3.setter("Bostan Celtics", 22);
game3.getter();
/*declares an array of 12 BaseballGame objects.
Prompt the user for data for each object, and display
all the values.
Then pass each object to a method that displays the
name of the winning team
or "Tie" if the score is a tie.*/
cout<<"================================="<<endl;
cout<<"OUTPUT FOR APPLICATION NO
3"<<endl;
cout<<"================================="<<endl;
string team_name;
int score;
/*I TOOK OBJECT OF 4 TEAM INSTEAD OF 12, IF WANT TO
CHANGE THE SIZE OF ARRAY, JUST CHANGE IT TO 12 IN FOR LOOP AND
OBJECT OF ARRAYS.*/
BaseBallGame gme[4];
for(int i=0;i<4;i++){
cout<<"Enter the name of your
team and their score:"<<endl;
cin>>team_name>>score;
gme[i].setter(team_name,
score);
}
cout<<"================================="<<endl;
cout<<"Teams and their Scores
"<<endl;
for(int i=0;i<4;i++){
cout<<"Team number
"<<i+1<<endl;;
gme[i].getter();
}
cout<<"================================="<<endl;
for(int i=0;i<3;i++){
for(int j=i+1;j<4;j++){
if(gme[i].finalScore>gme[j].finalScore){
cout<<"WINNER TEAM :
"<<gme[i].teamName<<" against
"<<gme[j].teamName<<endl;
}
else
if(gme[i].finalScore==gme[j].finalScore){
cout<<"Match TIE TEAM:
"<<gme[i].teamName<<" against
"<<gme[j].teamName<<endl;
}
}
}
return 0;
}
OUTPUT :
OUTPUT FOR APPLICATION NO 2
================================
Team Name: Los ANgels
Fina score: 12
Team Name: Golden State Warriors
Fina score: 14
Team Name: Bostan Celtics
Fina score: 22
=================================
OUTPUT FOR APPLICATION NO 3
=================================
Enter the name of your team and their score:
team1
12
Enter the name of your team and their score:
team2
13
Enter the name of your team and their score:
team3
13
Enter the name of your team and their score:
team4
11
=================================
Teams and their Scores
Team number 1
Team Name: team1
Fina score: 12
Team number 2
Team Name: team2
Fina score: 13
Team number 3
Team Name: team3
Fina score: 13
Team number 4
Team Name: team4
Fina score: 11
=================================
WINNER TEAM : team1 against team4
Match TIE TEAM: team2 against team3
WINNER TEAM : team2 against team4
WINNER TEAM : team3 against team4
--------------------------------
SCREENSHOT OF CODE AND OUTPUT:
IF YOU HAVE ANY PROBLEM REGARDING CODE AND CLASS DIAGRAM, DO COMMENT IN THE COMMENT SECTION, HIT A LIKE.