Results 1 to 5 of 5
  1. #1
    jermaindefoe is offline Member
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Question Cant get the logic right

    Hello everyone, i was hoping for some help with java project,
    here is the conditions of the programme i am making

    • the Yellow and Green Houses compete against each other in a series of
    matches (18 peeps in yellow 18 peeps in green each house has 6 classes)
    • each match is ‘best of three’ single games of one type of game
    • pupils cannot play other pupils from the same class
    • pupils can only play another pupil once
    • each pupil will take part in one match for each type of game.

    below is the code that i have written

    Class Student

    Java Code:
    public class student
    {
        private String name;
        private int c_lass;
        private int []game;
        private int []oponent;
    
    public student(String n, int c)
        
        {
            oponent=new int [18];
            game = new int [4];
            game[0] = -1;
            game[1] = -1;
            game [2] = -1;
            game[3] = -1;
            c_lass = c;
            name = n;
        }
        
    public String getName()
    
        {
         
          return name;      
            
        }
        
    public int getC_lass()
    
        {
         
          return c_lass;    
            
        }
    
    public int getGame(int y)
    
        {
         
          return game [y];      
            
        }
        
    public int getOponent(int y)
    
        {
         
          return oponent [y];      
            
        }
        
    public void changeName (String y)
    
        {
         
          name = y;      
            
        }
      
    public void changeC_lass (int y)
    
        {
         
          c_lass = y;      
            
        }
      
    public void changeGame (int gindex, int oponent)
    
        {
         
         game [gindex] = oponent;       
            
        }
        
    public void eraseGame (int y)
    
        {
         
         game [y] = -1;       
            
        }
      
    public void eraseOponent (int y)
    
        {
         
         oponent [y] = 0;       
            
        }
    
    public void changeOponent (int y)
    
        {
         
         oponent [y] = 1;       
            
        }
    }
    Class StudentDetails

    Java Code:
    public class studentdetails
    
    {
        public student [] gh = new student [18];
        public student [] yh = new student [18];
        
        public studentdetails()
        
        {
        }
    
    public void createstudentdetails ()
    
        {
            
        gh[0]=new student("Arnold",1);
        gh[1]=new student("Bertha",1);
        gh[2]=new student("Bella",1);
          
        gh[3]=new student("Charles",2);
        gh[4]=new student("Denise",2);
        
        gh[5]=new student("Edward",3);
        gh[6]=new student("Earl",3);
        gh[7]=new student("Freda",3);
        
        gh[8]=new student("George",4);
        gh[9]=new student("Gerry",4);
        gh[10]=new student("Harrier",4);
        gh[11]=new student("Helen",4);
        
        gh[12]=new student("Ian",5);
        gh[13]=new student("Issac",5);
        gh[14]=new student("Jenny",5);
        
        gh[15]=new student("Keith",6);
        gh[16]=new student("Kevin",6);
        gh[17]=new student("Leila",6);
     
        yh[0]=new student("Albert",1);
        yh[1]=new student("Aswan",1);
        yh[2]=new student("Betty",1);
        
        yh[3]=new student("Colin",2);
        yh[4]=new student("Debra",2);
        
        yh[5]=new student("Elias",3);
        yh[6]=new student("Felicity",3);
        yh[7]=new student("Fiona",3);
        
        yh[8]=new student("Gilbert",4);
        yh[9]=new student("Gwyn",4);
        yh[10]=new student("Hebe",4);
        yh[11]=new student("Hilary",4);
        
        yh[12]=new student("Idris",5);
        yh[13]=new student("Jane",5);
        yh[14]=new student("Jasmine",5);
        
        yh[15]=new student("Kenny",6);
        yh[16]=new student("Laura",6);
        yh[17]=new student("Linda",6);
        
        
    }    
    
    public void printstudentdetails()
    {
    
    for(int i=0;i<18;i++)
    {
    System.out.println(gh[i].getName()+" Class "+gh[i].getC_lass()+" Green House");
    System.out.println(yh[i].getName()+" Class "+yh[i].getC_lass()+" Yellow House");
    }
     
    
    }    
        public student getgh(int y)
    {
        return gh[y];
    }
        public student getyh(int y)
    {
        return yh[y];
    }
        
    }
    Class Matchlist (the problematic one!)

    Java Code:
    /** http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html */
    public class Matchlist
    {
    
        private studentdetails sd = new studentdetails();
        /** matchList StringBuilder stores the match list in progress */
        private StringBuilder matchList = new StringBuilder();
    
        public Matchlist()
        {
    
        }
    
        /** Method to create the actual match list, returns the list as a string */
        public String CreateMatch()
        {
            sd.createstudentdetails();
    
            for (int g = 0; g < 4; g++)/**g = game increments to 4 games*/
    
            {
                for (int y = 0; y < 18; y++)/**y = green house - adds the user to the game from the green house using an array until the limit has been met*/
                {
                    if (sd.getgh(y).getGame(g) == -1)
                    {
                        for (int x = 0; x < 18; x++) /**x = yellow house - adds the user to the game from the yellow house using an array until the limit has been met*/
                        {
                            if (sd.getyh(x).getGame(g) == -1)
                            {
                                if (sd.getgh(y).getC_lass() != sd.getyh(x).getC_lass()) /** ! means a boolean component operator */ 
                                {
                                    sd.getyh(x).changeGame(g, x);
                                    sd.getgh(y).changeGame(g, y);
                                    sd.getyh(x).changeOponent(y);
                                    sd.getgh(y).changeOponent(x);
                                                                   
                                    /** Build the match list step by step using append with \n at the end to create a new line */
                                    
                                    matchList.append(sd.getyh(x).getName() + " vs " + sd.getgh(y).getName() + "\n");
                                    
                                    /** The printline isnt needed any more
                                     *System.out.println(sd.getyh(x).getName()+" vs "+sd.getgh(y).getName()); */
                                     
                                }
    
                            }
                        }
                    }
                }
            }
            /** Convert the stringbuilder into an actual string, then return it using the code below */
            String completeMatchList = matchList.toString();
            return completeMatchList;
        }
        }
    Class Interface

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class MyPanel extends JPanel implements ActionListener
    {   /**add the "implements ActionListener" line so the class knows its going to take an input */
    
        private Matchlist ml = new Matchlist();
        private JButton btngenerate;
        private JTextArea txtbdisplaymatch;
        private JLabel lblvs;
        private JLabel lblgreenhouse;
        private JLabel lblyellowhouse;
        private JLabel lblname;
        private JTextArea txtbgreenentry;
        private JTextField txtbyellowentry;
        private JTextArea txtbenterscoreg;
        private JTextField txtbenterscorey;
        private JButton btnfinalresults;
        private JScrollPane scrollpane;
    
        public MyPanel()
        {
            //construct components
    
            btngenerate = new JButton("Generate Match List");
            txtbdisplaymatch = new JTextArea(5, 5);
            scrollpane = new javax.swing.JScrollPane(txtbdisplaymatch);
            scrollpane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            lblvs = new JLabel(" vs");
            lblgreenhouse = new JLabel(" Green House");
            lblyellowhouse = new JLabel(" Yellow House");
            lblname = new JLabel(" Name");
            txtbgreenentry = new JTextArea(5, 5);
            txtbyellowentry = new JTextField(5);
            txtbenterscoreg = new JTextArea(5, 5);
            txtbenterscorey = new JTextField(5);
            btnfinalresults = new JButton("View Final Results");
    
            //adjust size and set layout
            setPreferredSize(new Dimension(542, 542));
            setLayout(null);
    
            //add components
            add(btngenerate);
            add(scrollpane);
            add(lblvs);
            add(lblgreenhouse);
            add(lblyellowhouse);
            add(lblname);
            add(txtbgreenentry);
            add(txtbyellowentry);
            add(txtbenterscoreg);
            add(txtbenterscorey);
            add(btnfinalresults);
    
            //set component bounds (only needed by Absolute Positioning)
            btngenerate.setBounds(185, 25, 165, 20);
            scrollpane.setBounds(60, 70, 415, 320); /**Set the bounds on the scroll not the text area */
            lblvs.setBounds(285, 415, 25, 25);
            lblgreenhouse.setBounds(120, 415, 100, 25);
            lblyellowhouse.setBounds(385, 415, 100, 25);
            lblname.setBounds(30, 445, 45, 25);
            txtbgreenentry.setBounds(75, 445, 155, 20);
            txtbyellowentry.setBounds(355, 445, 155, 20);
            txtbenterscoreg.setBounds(235, 445, 20, 20);
            txtbenterscorey.setBounds(330, 445, 20, 20);
            btnfinalresults.setBounds(185, 495, 205, 25);
    
    
    
            //add action listener
            btngenerate.addActionListener(this);    /**use the components name and use the ".addActionListener()" method to enable this component to take inputs*/
        }
    
        public void actionPerformed(ActionEvent e)
        {/**create a method called actionPerformed, this method will contain the code which is run when the component is pressed.*/
                    if (e.getSource() == btngenerate)
            { /**use the getSource() method and use it to see if it is equal to the compoenent.*/
                txtbdisplaymatch.setText(ml.CreateMatch());  //use the setText() method to add text from variable a to the txtbdisplaymatch which will display in the text area when the generate button is pressed.
            }
        }
    
        public static void main(String[] args)
        {
            JFrame frame = new JFrame("MyPanel");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new MyPanel());
            frame.pack();
            frame.setVisible(true);
        }
    }
    ok, so weh ni compile and run the programme which i am making using the environemnt called blue j i get the following match list

    Java Code:
    Colin vs Arnold
    Debra vs Arnold
    Elias vs Arnold
    Felicity vs Arnold
    Fiona vs Arnold
    Gilbert vs Arnold
    Gwyn vs Arnold
    Hebe vs Arnold
    Hilary vs Arnold
    Idris vs Arnold
    Jane vs Arnold
    Jasmine vs Arnold
    Kenny vs Arnold
    Laura vs Arnold
    Linda vs Arnold
    Albert vs Charles
    Aswan vs Charles
    Betty vs Charles
    Colin vs Arnold
    Debra vs Arnold
    Elias vs Arnold
    Felicity vs Arnold
    Fiona vs Arnold
    Gilbert vs Arnold
    Gwyn vs Arnold
    Hebe vs Arnold
    Hilary vs Arnold
    Idris vs Arnold
    Jane vs Arnold
    Jasmine vs Arnold
    Kenny vs Arnold
    Laura vs Arnold
    Linda vs Arnold
    Albert vs Charles
    Aswan vs Charles
    Betty vs Charles
    Colin vs Arnold
    Debra vs Arnold
    Elias vs Arnold
    Felicity vs Arnold
    Fiona vs Arnold
    Gilbert vs Arnold
    Gwyn vs Arnold
    Hebe vs Arnold
    Hilary vs Arnold
    Idris vs Arnold
    Jane vs Arnold
    Jasmine vs Arnold
    Kenny vs Arnold
    Laura vs Arnold
    Linda vs Arnold
    Albert vs Charles
    Aswan vs Charles
    Betty vs Charles
    Colin vs Arnold
    Debra vs Arnold
    Elias vs Arnold
    Felicity vs Arnold
    Fiona vs Arnold
    Gilbert vs Arnold
    Gwyn vs Arnold
    Hebe vs Arnold
    Hilary vs Arnold
    Idris vs Arnold
    Jane vs Arnold
    Jasmine vs Arnold
    Kenny vs Arnold
    Laura vs Arnold
    Linda vs Arnold
    Albert vs Charles
    Aswan vs Charles
    Betty vs Charles
    this isnt right and i really cannot figure out why this is the case, please can somone help me

    thanks for your time

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Welcome to the Java Forums!

    I know you gave a bulleted list of requirements for your program, but I'm afraid I don't quite understand them - can you be more descriptive of why your output is "not right" ? For example, why isn't is right? What should it be outputting?
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    jermaindefoe is offline Member
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Default Problems

    Ok well since the last time i have posted i have made a few changes to the code and i am now closer to completing it

    the specifications for the game stand as befroe, ill list them again and add the rest of the explanation

    • the Yellow and Green Houses compete against each other in a series of
    matches (18 peeps in yellow 18 peeps in green each house has 6 classes)
    • each match is ‘best of three’ single games of one type of game
    • pupils cannot play other pupils from the same class
    • pupils can only play another pupil once
    • each pupil will take part in one match for each type of game (there are four game types, snaap, scrabble, spillinkins and cribbage).

    before the output was just displaying arnold against eveyone else but this isnt correct, becuase it doesnt fit the above rules so ive changed it to the following;

    student
    Java Code:
    public class student
    {
        private String name;
        private int c_lass;
        private String[] game;
    
        public student(String n, int c)
        {
            game = new String[4];
            game[0] ="";
            game[1] ="";
            game[2] ="";
            game[3] ="";
            c_lass = c;
            name = n;
        }
    
        public void getEmptyMatches()
        {
            for (String i : game)
            {
                if (i.equalsIgnoreCase(""))
                {
                    System.out.println(this.name + "Is missing a match");
                }
            }
        }
        public String getName()
        {
    
            return name;
    
        }
    
        public int getC_lass()
        {
    
            return c_lass;
    
        }
    
        /** Change getGame to return a string */
        public String getGame(int y)
        {
    
            return game[y];
    
        }
    
        public void changeName(String y)
        {
    
            name = y;
    
        }
    
        public void changeC_lass(int y)
        {
    
            c_lass = y;
    
        }
        
        
        public boolean checkPlayed(String player)
        {
            for (String i : game)
            {
                if (i.compareTo(player) == 0)
                {
                    return true;
                }
                 
            }
            return false;
        }
    
        public void changeGame(int gindex, String opponent)
        {
    
            game[gindex] = opponent;
    
        }
    }
    studentdetails
    Java Code:
    public class studentdetails
    {
    
        public student[] gh = new student[18];
        public student[] yh = new student[18];
    
        public studentdetails()
        {
        }
    
        public void createstudentdetails()
        {
    
            gh[0] = new student("Arnold", 1);
            gh[1] = new student("Bertha", 1);
            gh[2] = new student("Bella", 1);
    
            gh[3] = new student("Charles", 2);
            gh[4] = new student("Denise", 2);
    
            gh[5] = new student("Edward", 3);
            gh[6] = new student("Earl", 3);
            gh[7] = new student("Freda", 3);
    
            gh[8] = new student("George", 4);
            gh[9] = new student("Gerry", 4);
            gh[10] = new student("Harrier", 4);
            gh[11] = new student("Helen", 4);
    
            gh[12] = new student("Ian", 5);
            gh[13] = new student("Issac", 5);
            gh[14] = new student("Jenny", 5);
    
            gh[15] = new student("Keith", 6);
            gh[16] = new student("Kevin", 6);
            gh[17] = new student("Leila", 6);
    
            yh[0] = new student("Albert", 1);
            yh[1] = new student("Aswan", 1);
            yh[2] = new student("Betty", 1);
    
            yh[3] = new student("Colin", 2);
            yh[4] = new student("Debra", 2);
    
            yh[5] = new student("Elias", 3);
            yh[6] = new student("Felicity", 3);
            yh[7] = new student("Fiona", 3);
    
            yh[8] = new student("Gilbert", 4);
            yh[9] = new student("Gwyn", 4);
            yh[10] = new student("Hebe", 4);
            yh[11] = new student("Hilary", 4);
    
            yh[12] = new student("Idris", 5);
            yh[13] = new student("Jane", 5);
            yh[14] = new student("Jasmine", 5);
    
            yh[15] = new student("Kenny", 6);
            yh[16] = new student("Laura", 6);
            yh[17] = new student("Linda", 6);
    
    
        }
    
        public void printstudentdetails()
        {
    
            for (int i = 0; i < 18; i++)
            {
                System.out.println(gh[i].getName() + " Class " + gh[i].getC_lass() + " Green House");
                System.out.println(yh[i].getName() + " Class " + yh[i].getC_lass() + " Yellow House");
            }
    
    
        }
    
        public student getgh(int y)
        {
            return gh[y];
        }
    
        public student getyh(int y)
        {
            return yh[y];
        }
    }
    matchlist
    Java Code:
    public class Matchlist
    {
    
        private studentdetails sd = new studentdetails();
        /** matchList StringBuilder stores the match list in progress */
        private StringBuilder matchList = new StringBuilder();
        
        private int loop = 0;
        private int matches = 0;
        public Matchlist()
        {
            sd.createstudentdetails();
        }
    
        /** Method to create the actual match list, returns the list as a string */
        public String CreateMatch()
        {
            int g;
            int y;
                for (g = 0; g < 4; g++)//g = game
                {
                    for (y = 17; y > -1; y--)//y = green house
                    {
                        /** Check to see if the game is empty */
                        if (sd.getgh(y).getGame(g).equalsIgnoreCase(""))
                        {
                            for (int x = 0; x < 18; x++) //x = yellow house
                            {
                                if (sd.getyh(x).getGame(g).equalsIgnoreCase(""))
                                {
                                    if (sd.getgh(y).getC_lass() != sd.getyh(x).getC_lass())
                                    {
                                        /** Check to see if the person has played the other person */
                                        if (sd.getgh(y).checkPlayed(sd.getyh(x).getName()) == false)
                                        {
                                            /** Set the game to the name of the opponent played */
                                            sd.getyh(x).changeGame(g, sd.getgh(y).getName());
                                            sd.getgh(y).changeGame(g, sd.getyh(x).getName());
                                            /** Build the match list step by step using append with \n at the end to create a new line */
                                            matchList.append(sd.getyh(x).getName() + " vs " + sd.getgh(y).getName() + "\n");
                                            matches++;
                                            break;
                                        }
                                    /** Don't need the println anymore, delete it if you want
                                     *System.out.println(sd.getyh(x).getName()+" vs "+sd.getgh(y).getName());
                                     */
                                    }
    
                                }
                            }
                        }
                    }
    
                }
                /** Convert the stringbuilder into an actual string, then return it */
                String completeMatchList = matchList.toString();
                System.out.println(matches);
                for (int i = 0; i <18; i++)
                {
                    sd.getyh(i).getEmptyMatches();
                    sd.getgh(i).getEmptyMatches();
                }
                return completeMatchList;
            }
        }
    interface
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class MyPanel extends JPanel implements ActionListener
    {   //add the "implements ActionListener" line so the class knows its going to take an input
    
        private Matchlist ml = new Matchlist();
        private JButton btngenerate;
        private JTextArea txtbdisplaymatch;
        private JLabel lblvs;
        private JLabel lblgreenhouse;
        private JLabel lblyellowhouse;
        private JLabel lblname;
        private JTextArea txtbgreenentry;
        private JTextField txtbyellowentry;
        private JTextArea txtbenterscoreg;
        private JTextField txtbenterscorey;
        private JButton btnfinalresults;
        private JScrollPane scrollpane;
    
        public MyPanel()
        {
            //construct components
    
            btngenerate = new JButton("Generate Match List");
            txtbdisplaymatch = new JTextArea(5, 5);
            /** Might want to change the name of scrollpane here and in the setbounds to something else, I couldn't be bothered to think of anything */
            scrollpane = new javax.swing.JScrollPane(txtbdisplaymatch);
            scrollpane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            lblvs = new JLabel(" vs");
            lblgreenhouse = new JLabel(" Green House");
            lblyellowhouse = new JLabel(" Yellow House");
            lblname = new JLabel(" Name");
            txtbgreenentry = new JTextArea(5, 5);
            txtbyellowentry = new JTextField(5);
            txtbenterscoreg = new JTextArea(5, 5);
            txtbenterscorey = new JTextField(5);
            btnfinalresults = new JButton("View Final Results");
    
            //adjust size and set layout
            setPreferredSize(new Dimension(542, 542));
            setLayout(null);
    
            //add components
            add(btngenerate);
            add(scrollpane);
            add(lblvs);
            add(lblgreenhouse);
            add(lblyellowhouse);
            add(lblname);
            add(txtbgreenentry);
            add(txtbyellowentry);
            add(txtbenterscoreg);
            add(txtbenterscorey);
            add(btnfinalresults);
    
            //set component bounds (only needed by Absolute Positioning)
            btngenerate.setBounds(185, 25, 165, 20);
            scrollpane.setBounds(60, 70, 415, 320); /**Changed it from setting the bounds on the textarea to setting them on the scrollpane */
            lblvs.setBounds(285, 415, 25, 25);
            lblgreenhouse.setBounds(120, 415, 100, 25);
            lblyellowhouse.setBounds(385, 415, 100, 25);
            lblname.setBounds(30, 445, 45, 25);
            txtbgreenentry.setBounds(75, 445, 155, 20);
            txtbyellowentry.setBounds(355, 445, 155, 20);
            txtbenterscoreg.setBounds(235, 445, 20, 20);
            txtbenterscorey.setBounds(330, 445, 20, 20);
            btnfinalresults.setBounds(185, 495, 205, 25);
    
    
    
            //add action listener
            btngenerate.addActionListener(this);    //use the components name and use the ".addActionListener()" method to enable this component to take inputs
        }
    
        public void actionPerformed(ActionEvent e)
        {//create a method called actionPerformed, this method will contain the code which is run when the component is pressed.
            /** Don't need these anymore as CreateMatch is called when setting the textarea
            //ml.CreateMatch();
            //ml.showList();
            //String a = "hello";	   //create a string variable called "a" and store some text in it.
             */
            if (e.getSource() == btngenerate)
            { //use the getSource() method and use it to see if it is equal to the compoenent.
                txtbdisplaymatch.setText(ml.CreateMatch());  //use the setText() method to add text from variable a to the txtbdisplaymatch which will display in the text area when the generate button is pressed.
            }
        }
    
        public static void main(String[] args)
        {
            JFrame frame = new JFrame("MyPanel");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new MyPanel());
            frame.pack();
            frame.setVisible(true);
        }
    }
    so now i do get a correct matchlist almost, there are a maximum of 72 games that need to be generated, 18 people, 4 games, = 72, however im not gettign all of them, im managing upto 68 i think

    hope this is a little clearer, its really hard to explain,

    thanks
    Last edited by jermaindefoe; 03-08-2008 at 06:27 PM.

  4. #4
    jermaindefoe is offline Member
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Default

    any chance you could take a peek now? im 4 ganes, 8 people away from finishing the project :mad:

    regards

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I trid to break up the code block in your CreateMatch method a bit but wasn't able to do any better than you did (without changing the data in studentdetails). I think you've got it.
    Java Code:
    public class Matchlist
    {
        private studentdetails sd = new studentdetails();
        /** matchList StringBuilder stores the match list in progress */
        private StringBuilder matchList = new StringBuilder();
        
        private int loop = 0;
        private int matches = 0;
        int numOfStudents = 18;
        int numOfGames = 4;
    
        public Matchlist()
        {
            sd.createstudentdetails();
        }
    
        /** Method to create the actual match list, returns the list as a string */
        public String CreateMatch()
        {
            student[] yellowStudents = getYellowStudents();
            for(int j = numOfStudents-1; j >= 0 ; j--)
            {
                student green = sd.getgh(j);
                assignGames(green, yellowStudents);
            }
    
            student[] greenStudents = getGreenStudents();
            for(int j = 0; j < numOfStudents; j++)
            {
                student yellow = sd.getyh(j);
                assignGames(yellow, greenStudents);
            }
    /*
            // Using this loop in the first block above gives
            // 64 matches with 4 missing for each name below.
            //for(int j = 0; j < numOfStudents; j++)
            // Game opponents for Kevin, Laura, Linda, Leila:
            showGamesFor(sd.getgh(16));
            showGamesFor(sd.getyh(16));
            showGamesFor(sd.getyh(17));
            showGamesFor(sd.getgh(17));
    */
    /*
            for (int g = 0; g < 4; g++)  //g = game
            {
                for (int y = 17; y > -1; y--)  //y = green house
                {
                    // Check to see if the game is empty
                    if (sd.getgh(y).getGame(g).equalsIgnoreCase(""))
                    {
                        for (int x = 0; x < 18; x++)  //x = yellow house
                        {
                            if (sd.getyh(x).getGame(g).equalsIgnoreCase(""))
                            {
                                if (sd.getgh(y).getC_lass() !=
                                           sd.getyh(x).getC_lass())
                                {
                                    // Check to see if the person has
                                    //    played the other person
                                    if (sd.getgh(y).checkPlayed(
                                             sd.getyh(x).getName()) == false)
                                    {
                                        // Set the game to the name of the
                                        //    opponent played
                                        sd.getyh(x).changeGame(g,
                                               sd.getgh(y).getName());
                                        sd.getgh(y).changeGame(g,
                                               sd.getyh(x).getName());
                                        // Build the match list step by step
                                        //    using append with \n at the end
                                        //    to create a new line
                                        matchList.append(sd.getyh(x).getName() +
                                            " vs " + sd.getgh(y).getName() + "\n");
                                        matches++;
                                        break;
                                    }
                                    // Don't need the println anymore,
                                    //    delete it if you want
                                    // System.out.println(sd.getyh(x).getName()+
                                    //      " vs "+sd.getgh(y).getName());
                                }
                            }
                        }
                    }
                }
            }
    */
            // Game opponents for Arnold, Bertha, Laura, Linda:
            showGamesFor(sd.getgh(0));
            showGamesFor(sd.getgh(1));
            showGamesFor(sd.getyh(16));
            showGamesFor(sd.getyh(17));
    
            // Check green house students.
            checkForDuplicateGames(getGreenStudents(), "green");
    
            // Check yellow house students.
            checkForDuplicateGames(getYellowStudents(), "yellow");
    
            /** Convert the stringbuilder into an actual string, then return it */
            String completeMatchList = matchList.toString();
            System.out.println("matches = " + matches);
            for (int i = 0; i < 18; i++)
            {
                sd.getyh(i).getEmptyMatches();
                sd.getgh(i).getEmptyMatches();
            }
            return completeMatchList;
        }
    
        // These next two methods belong in student details.
        private student[] getGreenStudents()
        {
            student[] studs = new student[numOfStudents];
            for(int j = 0; j < studs.length; j++)
            {
                studs[j] = sd.getgh(j);
            }
            return studs;
        }
    
        private student[] getYellowStudents()
        {
            student[] studs = new student[numOfStudents];
            for(int j = 0; j < studs.length; j++)
            {
                studs[j] = sd.getyh(j);
            }
            return studs;
        }
    
        // Find 4 games/opponents for each student.
        private void assignGames(student s, student[] opponents)
        {
            for(int j = 0; j < numOfGames; j++)
            {
                // Find the first qualified opponent.
                for(int k = 0; k < opponents.length; k++)
                {
                    student candidate = opponents[k];
                    boolean played = havePlayed(s, candidate);
                    boolean classOkay = isDifferentClass(s, candidate);
                    boolean canPlay = canPlayGame(j, s, candidate);
                    if(!played && classOkay && canPlay)
                    {
                        s.changeGame(j, candidate.getName());
                        candidate.changeGame(j, s.getName());
                        matchList.append(s.getName() + " vs " +
                                         candidate.getName() + "\n");
                        matches++;
                        break;
                    }
                }
            }
        }
    
        private boolean havePlayed(student s1, student s2)
        {
            return s1.checkPlayed(s2.getName());
        }
    
        private boolean isDifferentClass(student s1, student s2)
        {
            return s1.getC_lass() != s2.getC_lass();
        }
    
        private boolean canPlayGame(int index, student s1, student s2)
        {
            return s1.getGame(index).equals("") &&
                   s2.getGame(index).equals("");
        }
    
        private boolean contains(String target, String[] strs)
        {
            if(target.equals(""))
                return false;
            for(int j = 0; j < strs.length; j++)
            {
                if(strs[j] != null && strs[j].equals(target))
                    return true;
            }
            return false;
        }
    
        private void showGamesFor(student s)
        {
            System.out.println("Game opponents for " + s.getName());
            for(int j = 0; j < numOfGames; j++)
            {
                String game = s.getGame(j);
                if(game.equals(""))
                    game = "empty";
                System.out.println("\t" + game);
            }
        }
    
        private void checkForDuplicateGames(student[] studs, String id)
        {
            int duplicateCount = 0;
            for(int j = 0; j < studs.length; j++)
            {
                student stud = studs[j];
                String[] names = new String[numOfGames];
                for(int k = 0, count = 0; k < numOfGames; k++)
                {
                    String name = stud.getGame(k);
                    if(contains(name, names))
                    {
                        System.out.printf("duplicate " + id +
                                          " names for %s = %n\t%s%n",
                                           stud.getName(),
                                           java.util.Arrays.toString(names));
                        duplicateCount++;
                    }
                    else
                        names[count++] = name;
                }
            }
            if(duplicateCount > 0)
                System.out.println("duplicateCount for " + id + " house " +
                                   "game opponents = " + duplicateCount);
        }
    }

Similar Threads

  1. iterate HashMap with logic
    By Heather in forum Web Frameworks
    Replies: 2
    Last Post: 07-03-2007, 09:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •