Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Ask a java question (https://www.askmehelpdesk.com/showthread.php?t=651384)

  • Apr 16, 2012, 05:00 AM
    katt3000
    ask a java question
    How to write a java function that allows the user to make two teams and simulate a game. The program should employ polymorphism and allow subclasses. As scores against teams should be simulated by randomization- use random method to decide who scores against the other and by how many goals.

    this is what i have done so far:

    Quote:

    public class Main {

    private static void playGame(Team teamA,Team teamB)
    {

    Random rand = new Random();
    for(int i= 0 ;i<= rand.nextInt(9);i++){/*Simulating attempts to score by both sides*/
    rand = new Random();
    int score = 1+ rand.nextInt(3);//Used to decide who scores and who oncedes
    switch(score)
    {
    case 1:
    teamA.setGoalsScored();//Team A scores
    teamB.setGoalsConceded();//Team B concedes
    break;
    case 2:
    teamB.setGoalsScored();//Team B scores
    teamA.setGoalsConceded();//Team A concedes
    break;

    }

    }
    }


    /**
    * @param args the command line arguments
    */

    public static void main(String[] args) {
    // main menu
    Scanner scan ;
    Team teamA = null ;
    Team teamB = null;
    String TeamName;
    char getInput ='Y';
    int option;
    do
    {
    System.out.println();
    System.out.println(" GAME OF VOLLEYBALL\n");
    System.out.println(" ...........MAIN MENU...................... ");
    System.out.println("To make a team .................. 1 ");
    System.out.println("To view a team A ................ 2 ");
    System.out.println("To view a team B ................ 3 ");
    System.out.println("To play a game .................. 4 ");
    System.out.println("View game results................ 5 ");

    scan = new Scanner(System.in);
    option = scan.nextInt();
    switch(option)
    {
    case 1:
    if((teamA == null)||(teamB == null) )//
    {
    System.out.println("Enter the name of the team : ") ;
    scan = new Scanner(System.in);
    TeamName = scan.nextLine();
    if(teamA == null){
    teamA = new Team(TeamName);//instantiating the team
    teamA.setTeamMembers();//Assigning members to a team
    }else{
    teamB = new Team(TeamName);//instantiating the team
    teamB.setTeamMembers();//Assigning members to a team
    }
    }
    else{
    System.out.println("You have already created two teams ") ;

    }
    break;
    case 2:
    if(teamA != null){

    System.out.println(teamA.getTeamName());
    teamA.desplayTeamMembers();
    }else{
    System.out.println("You haven't created the team ") ;
    }


    break;
    case 3:
    if(teamB != null){
    System.out.println(teamB.getTeamName());
    teamB.desplayTeamMembers();
    }else{
    System.out.println("You haven't created the team ") ;
    }
    break ;

    case 4:
    Main.playGame(teamA,teamB);
    break ;
    case 5:
    System.out.println("The following are the results ") ;
    System.out.println("Team A scored " + teamA.getGoalsScored()) ;
    System.out.println("Team B scored " + teamB.getGoalsScored()) ;

    break ;
    }
    System.out.println();
    System.out.println(" ...........MAIN MENU...................... ");
    System.out.println("Enter any character to continue or N to exit : ");
    scan = new Scanner(System.in);
    getInput = scan.next().charAt(0);

    }while(getInput !='N');


    }
    public class Team {

    String name;//Name of a team
    ArrayList teamMembersList;//A collection is requireed to store teamMember objects
    int goalsScored;
    int goalsConceded;

    //Overloading construnctors
    Team()
    {

    }
    Team(String name)
    {
    this.name = name;
    this.teamMembersList = new ArrayList();

    }

    Team(String name,ArrayList teamMembersList)
    {
    this.name = name;
    this.teamMembersList = teamMembersList;
    }

    //Getters
    public String getTeamName()
    {
    return this.name;
    }
    public ArrayList getTeamMembers()
    {
    return this.teamMembersList;
    }
    public int getGoalsScored()
    {
    return this.goalsScored;
    }
    public int getGoalsConceded()
    {
    return this.goalsConceded;
    }
    public void desplayTeamMembers( )
    {
    TeamMember member;
    for(int i =0;i<this.teamMembersList.size();i++)
    {
    member = (TeamMember)this.teamMembersList.get(i);
    System.out.println();
    System.out.print((i+1) + " ) ");
    member.desplayMember();
    System.out.println();
    }
    }
    //Setters
    public void setTeamName(String name)
    {
    this.name = name;
    }
    public void setTeamMembers( )
    {


    TeamMember member;
    Scanner scan ;
    char getInput ='Y';
    System.out.println(" ...........Team Members Sub menu...................... ");
    do
    {
    System.out.println("A Team should have upto 5 team members\n\n");
    System.out.println("To ceate a player..........................1 : ");
    System.out.println("To ceate a non player......................2 : ");

    scan = new Scanner(System.in);
    if(scan.nextInt() == 1)
    {
    member = new Player();//Creating player object
    member.setTeamMember();
    this.teamMembersList.add(member) ;//Adding team members to the collection
    }else{
    if(scan.nextInt() == 2)
    {
    member = new NonePlayer();//Creating a non player object
    member.setTeamMember();
    this.teamMembersList.add(member) ;//Adding team members to the colllection
    }
    }

    System.out.println("Enter any character to continue or N to exit : ");
    scan = new Scanner(System.in);
    getInput = scan.next().charAt(0);

    }while(getInput !='N');




    }
    public void setGoalsScored( )
    {
    this.goalsScored++;
    }
    public void setGoalsConceded()
    {
    this.goalsConceded++;
    }
    abstract class TeamMember { //Data members common in all Team members String name; String DOB; String description;

    //Overloading construnctors
    TeamMember()
    {
    this.name ="--";
    this.DOB ="0/0/0";
    this.description = "--";
    }
    TeamMember(String name)
    {
    this.name = name;
    this.DOB ="0/0/0";
    this.description = "--";
    }

    //Getters
    protected String getName()
    {
    return this.name;
    }
    protected String getDOB()
    {
    return this.DOB;
    }
    protected String getDescription()
    {
    return this.description;
    }
    protected void desplayMember()
    {
    System.out.print(" Name ...... "+ this.name);
    System.out.print(", Role...."+ this.description);
    System.out.print(", Date of Birth...."+ this.DOB);
    }
    //Setters
    protected void setName(String name)
    {
    this.name = name;
    }
    protected void setDOB(String DOB)
    {
    this.DOB = DOB;
    }
    protected void setDescription( String description)
    {
    this.description = description;
    }
    protected void setTeamMember()
    {


    Scanner scan ;
    System.out.print("Team member's name............................... : ");
    scan = new Scanner(System.in);
    this.name = scan.nextLine();


    System.out.print("Team member's role............................... : ");
    scan = new Scanner(System.in);
    this.description = scan.nextLine();

    System.out.print("Team member's date of birth.............................. : ");
    scan = new Scanner(System.in);
    this.DOB = scan.nextLine();
    }
    } class Player extends TeamMember { //These data members can only be found in a player int jersey_number; boolean fit;

    //Overloading construnctors
    Player()
    {
    super();
    }
    Player(String name)
    {
    super(name);
    }
    Player(String name,int jersey&#95;number )
    {
    super(name);
    this.jersey&#95;number = jersey&#95;number;
    }

    //Getters
    public int getJersey&#95;number()
    {
    return this.jersey&#95;number;
    }
    public boolean getFitness()
    {
    return this.fit;
    }
    protected void desplayMember()
    {
    super.desplayMember();
    System.out.print(", Jersry Number ...... "+ this.jersey&#95;number);
    System.out.print(", Is fit ? ...... "+ this.fit);

    }

    //Setters
    public void setJersey&#95;number( int jersey&#95;number)
    {
    this.jersey&#95;number = jersey&#95;number;
    }
    public void setFitness( boolean fit)
    {
    this.fit = fit;
    }
    public void setTeamMember()
    {
    super.setTeamMember();

    Scanner scan ;
    System.out.print(" Player's Jersey number................... : ");
    scan = new Scanner(System.in);
    this.jersey&#95;number = scan.nextInt();
    System.out.print(" Is player fit?)(yes or no)................... : ");
    scan = new Scanner(System.in);
    String isFit = scan.nextLine();

    if(isFit.equalsIgnoreCase("yes"))
    {
    this.fit = true;
    }

    }
    }

    class NonePlayer extends TeamMember { //Data Members Specific to NonePlayers String Nationality;

    //Overloaded constructors NonePlayer() { super(); } NonePlayer(String Nationality, String name) { super(name); this.Nationality = Nationality ; } //getters public String getNatinality() { return this.Nationality; } protected void desplayMember() { super.desplayMember(); System.out.print(" Nationality ...... "+ this.Nationality);

    } //Setters public void setNatinality(String Nationality) { this.Nationality = Nationality; } public void setTeamMember() { super.setTeamMember();

    Scanner scan ;
    System.out.print(", Nationality................... : ");
    scan = new Scanner(System.in);
    this.Nationality = scan.nextLine();
    }
    }
  • Apr 17, 2012, 02:23 PM
    katt3000
    Somebody please help??

  • All times are GMT -7. The time now is 03:23 PM.