Hi I'm currently doing a connect 4 program and I need to write an apporpiate get and set methods so that the other classes can access this information. ('State of the autoplay' is simply whether red or yellow (or no-one) is set to have the computer make their move). I've supplied the code, however its really confusing as I can't seem to figure out what the appropiate method would be.

Connect4View

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

public class Connect4View extends JFrame
{
	DrawPanel canvas;
	Connect4Model model;
	ControlPanel myControlPanel, JRadioButton;

	public static void main(String[] args)
	{
		Connect4View w = new Connect4View();
		w.setVisible(true);
	}

	public Connect4View()
	{
		setTitle("Connect4 solution");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500,220);
		setLocation(300,300);

		int number_of_columns = 7;
		int number_of_rows = 9;

		model = new Connect4Model(number_of_columns,number_of_rows);
		canvas = new DrawPanel(number_of_columns,number_of_rows);
		myControlPanel = new ControlPanel();


		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		add(canvas, BorderLayout.CENTER);
		add(myControlPanel, BorderLayout.SOUTH);
 	}
	class ControlPanel extends JPanel implements ActionListener
	{
		JButton resetButton;
		JRadioButton twoPlayerButton;
		JRadioButton autoRedButton;
		JRadioButton autoYellowButton;

		public ControlPanel()
		{

			resetButton = new JButton("reset!");
			add(resetButton);
			resetButton.addActionListener(this);

			twoPlayerButton = new JRadioButton("Two Player Game");
			twoPlayerButton.addActionListener(this);
			add(twoPlayerButton);
			twoPlayerButton.setSelected(true);


			autoRedButton = new JRadioButton("Auto Red Button");
			autoRedButton.addActionListener(this);
			add(autoRedButton);
			autoRedButton.setSelected(true);

			autoYellowButton = new JRadioButton("Auto Yellow Button");
			autoYellowButton.addActionListener(this);
			add(autoYellowButton);
			autoYellowButton.setSelected(true);


			ButtonGroup group = new ButtonGroup();
			group.add(twoPlayerButton);

			ButtonGroup group1 = new ButtonGroup();
			group.add(autoRedButton);

			ButtonGroup group2 = new ButtonGroup();
			group.add(autoYellowButton);
		// and also add the other Radio Buttons to the group
		}



		public void actionPerformed(ActionEvent event)
		{
		 if(event.getSource()==twoPlayerButton)
		   {
		    System.out.println("twoPlayer");
		   }
		   else if(event.getSource()==autoRedButton)
		   {
		    System.out.println("RedButton");
		   }
		   else if(event.getSource()==autoYellowButton)
		   {
		    System.out.println("Yellow Button");
		   }
		   else if(event.getSource()==resetButton)
		   {
		    model.reset();
   }
			System.out.println("event is " + event.getActionCommand());


			canvas.repaint();
			getParent().repaint();
		}
	}
	class DrawPanel extends JPanel implements MouseListener
	{
		public int numCols;
		public int numRows;

		public DrawPanel(int nc, int nr)
		{
			numCols = nc;
			numRows = nr;
			addMouseListener(this);
		}
		int getCol(int x)
		{
			return x*numCols/getWidth();
		}
		int getRow(int y)
		{
			return y*numRows/getHeight();
		}

		public void mouseReleased(MouseEvent event)
		{
		}
		public void mousePressed(MouseEvent event)
		{
		}
		public void mouseClicked(MouseEvent event)
		{
			int thisCol = getCol(event.getX());
			model.go(thisCol);
			repaint();

		}
		public void mouseEntered(MouseEvent event)
		{
		}
		public void mouseExited(MouseEvent event)
		{
		}


		Rectangle getRect(int thisCol, int thisRow)
		{
			// if input is out of range, return "null"
			if(thisCol <0 || thisRow < 0)
				return null;
			if(thisCol>=numCols || thisRow>=numRows)
				return null;

			// otherwise, make and return the Rectangle
			int w = getWidth()/numCols;
			int h = getHeight()/numRows;

			int x = thisCol*w;
			int y = thisRow*h;

			Rectangle myRect = new Rectangle(x,y,w,h);
			return myRect;
		}

		public void paint(Graphics g)
		{
			g.setColor(Color.gray);
			g.fillRect(0,0,getWidth(), getHeight());
			g.setColor(Color.black);


			Graphics2D g2 = (Graphics2D)g;
			// we'll use Graphics2D for it's "draw" method -
			// neater than the Graphics "drawRect" suppled
			// (which you could also use)

			for (int i = 0;i<numCols;i++)
				for(int j = 0;j<numRows;j++)
					g2.draw(getRect(i,j));

			for (int thisCol = 0; thisCol < model.getNumCols(); thisCol ++)
			{
				int num_of_counters = model.getNumCounters(thisCol);
				System.out.println("col " + thisCol + " has " + num_of_counters);
				for (int counter=0; counter < num_of_counters; counter ++)
				{
					int colour = model.getCounter(thisCol, counter);
					if(colour==Connect4Column.RED_COUNTER)
						g2.setColor(Color.red);
					else if(colour==Connect4Column.YELLOW_COUNTER)
						g2.setColor(Color.yellow);

					Rectangle r = getRect(thisCol, numRows-counter-1);
					if (r != null)
						g2.fillOval(r.x, r.y, r.width, r.height);
				}
			}
		}
	}

 }

Connect4Model

public class Connect4Model
{
	Connect4Column [] columns; // an array of columns
	int NUM_COLUMNS; // how many columns we have in the game
	int NUM_ROWS;    // how many rows we have in the game
	int playerToGoNext = Connect4Column.RED_COUNTER; //can be red or yellow

	public Connect4Model(int numCols, int numRows)
	{
		NUM_COLUMNS = numCols;
		NUM_ROWS = numRows;

		columns = new Connect4Column[NUM_COLUMNS];
		for (int i=0; i < NUM_COLUMNS; i++) {
			columns[i] = new Connect4Column(NUM_ROWS);
		}
	}

	int getNumCols() // simply return the number of columns
	{
		return NUM_COLUMNS;
	}

	int getNumRows() // simply return the number of rows
	{
		return NUM_ROWS;
	}

	int getNextPlayer() // returns who gets to go next
	{
		return playerToGoNext;
	}
	void reset()
	{
		for (int thisCol = 0; thisCol < NUM_COLUMNS; thisCol ++)
		{
			for (int thisRow = 0; thisRow < NUM_ROWS; thisRow ++)
			{
				columns[thisCol].numCounters = 0;
				columns[thisCol].counters[thisRow] = 0;
			}
		}
	}

	boolean go(int thisColumn) // try to put a counter at this col
	{
		if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
			return false;

		System.out.println("thisCol is " + thisColumn);
		if(columns[thisColumn].addCounter(playerToGoNext)==true)
		{
			if(playerToGoNext==Connect4Column.YELLOW_COUNTER)
				playerToGoNext=Connect4Column.RED_COUNTER;
			else
				playerToGoNext=Connect4Column.YELLOW_COUNTER;
			return true;
		}
		return false;
	}

	int getCounter(int thisColumn, int thisRow) //
	{
		if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
			return 0;
		return columns[thisColumn].getCounter(thisRow);
	}
	int getNumCounters(int thisColumn) //
	{
		if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
			return 0;
		return columns[thisColumn].getNumCounters();
	}

}

What about Connect4Column?

What about Connect4Column?

Here's connect4Column. However what i need to do is make get and set methods so it can access information from the Connect4View class. I have an idea of how to do it, its just implementing it. I've worked out that you can store the get and set methods as an 'int'. :/

public class Connect4Column
{
	int [] counters; // array to store counters in this column
	int numCounters; // integer to say how many counters currently in this column
	int MAX_NUM_COUNTERS;  // height of the 'Connect 4' game

	static int YELLOW_COUNTER = 1;  // represents the 'yellow' counters
	static int RED_COUNTER = 2;  // represents the 'red' counters

	public Connect4Column(int maxCounters)
	{
		MAX_NUM_COUNTERS = maxCounters;
		counters = new int[MAX_NUM_COUNTERS];
		numCounters = 0;
	}

	public boolean addCounter(int thisCounter)
	{
		if (numCounters>=MAX_NUM_COUNTERS)
			return false;

		if (thisCounter == YELLOW_COUNTER
			 || thisCounter == RED_COUNTER)
		{
			counters[numCounters] = thisCounter;
			numCounters ++;
			return true;
		}
		else
			return false;
	}

	int getNumCounters()
	{
		return numCounters;
	}

	int getCounter(int thisRow)
	{
		if(thisRow<0 || thisRow >= MAX_NUM_COUNTERS)
			return 0;

		return counters[thisRow];
	}

}

what data memeber You Want to implement the getes and seters for it?

what data memeber You Want to implement the getes and seters for it?

I don't know what you mean by that? What I'm trying to do is write two methods 'get' and 'se't methods that access the Connect4View class.

The following is what is being asked of me.

It's best to store the state of the autoplay in the Connect4Model class. You decide how this is to be stored, and write appropriate get and set methods so that the other classes can access this information. ('State of the autoplay' is simply whether red or yellow (or no-one) is set to have the computer make their move). Use these methods where you have print statements

I'm not sure what you mean.
If I undertand you have to declare a variable named autoplay in the Connect4Model class with the corresponding get and set.
and use those getter and and setter in the paint method of the Connect4View class because there is used "System.out.println" or/and in the actionPerformed in the inner class ControlPanel!

If it is so the setter will be called in the actionPerformed and the getter will be called in the paint methode I think. in tha case the radio buttons will change the sate (by setting it's value) and the pain methode will reflect that chage (by calling the getter).
I'm not sure!

If it is so the setter will be called in the actionPerformed and the getter will be called in the paint methode I think. in tha case the radio buttons will change the sate (by setting it's value) and the pain methode will reflect that chage (by calling the getter).
I'm not sure!

Connect4View

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

public class Connect4View extends JFrame
{
	DrawPanel canvas;
	Connect4Model model;
	ControlPanel myControlPanel, JRadioButton;


	public static void main(String[] args)
	{
		Connect4View w = new Connect4View();
		w.setVisible(true);
	}

	public Connect4View()
	{
		setTitle("Connect4 solution");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500,220);
		setLocation(300,300);

		int number_of_columns = 7;
		int number_of_rows = 9;

		model = new Connect4Model(number_of_columns,number_of_rows);
		canvas = new DrawPanel(number_of_columns,number_of_rows);
		myControlPanel = new ControlPanel();


		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		add(canvas, BorderLayout.CENTER);
		add(myControlPanel, BorderLayout.SOUTH);
 	}
	class ControlPanel extends JPanel implements ActionListener
	{
		JButton resetButton;
		JRadioButton twoPlayerButton;
		JRadioButton autoRedButton;
		JRadioButton autoYellowButton;

		public ControlPanel()
		{
			resetButton = new JButton("Reset Board");
			add(resetButton);
			resetButton.addActionListener(this);

			twoPlayerButton = new JRadioButton("Two Player Game");
			twoPlayerButton.addActionListener(this);
			add(twoPlayerButton);
			twoPlayerButton.setSelected(true);

			autoRedButton = new JRadioButton("Red Button");
			autoRedButton.addActionListener(this);
			add(autoRedButton);

			autoYellowButton = new JRadioButton("Yellow Button");
			autoYellowButton.addActionListener(this);
			add(autoYellowButton);

			ButtonGroup group = new ButtonGroup();
			group.add(twoPlayerButton);

			ButtonGroup group2 = new ButtonGroup();
			group.add(autoRedButton);

			ButtonGroup group3 = new ButtonGroup();
			group.add(autoYellowButton);
		}

		public void actionPerformed(ActionEvent event)
		{

			if(event.getSource()==twoPlayerButton)
			{
				System.out.println("Selected Two Player");
				model.setAutoplay(1);
			}
			else if(event.getSource()==autoRedButton)
			{
				System.out.println("Selected Red Button");
				model.setAutoplay(2);
			}
			else if(event.getSource()==autoYellowButton)
			{
				System.out.println("Selected Yellow Button");
				model.setAutoplay(3);
			}
			else if(event.getSource()==resetButton)
			{
				model.reset();
			}

			//System.out.println("event is " + event.getActionCommand());
			canvas.repaint();
		}
	}
	class DrawPanel extends JPanel implements MouseListener
	{
		public int numCols;
		public int numRows;

		public DrawPanel(int nc, int nr)
		{
			numCols = nc;
			numRows = nr;
			addMouseListener(this);
		}
		int getCol(int x)
		{
			return x*numCols/getWidth();
		}
		int getRow(int y)
		{
			return y*numRows/getHeight();
		}

		public void mouseReleased(MouseEvent event)
		{
		}
		public void mousePressed(MouseEvent event)
		{
		}
		public void mouseClicked(MouseEvent event)
		{
			int thisCol = getCol(event.getX());
			model.go(thisCol);
			if(selectedOption == 1)
			{
			try { Thread.sleep(1000);} catch (Exception e)
				{

				};
			}
			if(selectedOption == 2)
			{
			}
			if(selectedOption == 3)
			{
			}

			repaint();

		}
		public void mouseEntered(MouseEvent event)
		{
		}
		public void mouseExited(MouseEvent event)
		{
		}


		Rectangle getRect(int thisCol, int thisRow)
		{
			// if input is out of range, return "null"
			if(thisCol <0 || thisRow < 0)
				return null;
			if(thisCol>=numCols || thisRow>=numRows)
				return null;

			// otherwise, make and return the Rectangle
			int w = getWidth()/numCols;
			int h = getHeight()/numRows;

			int x = thisCol*w;
			int y = thisRow*h;

			Rectangle myRect = new Rectangle(x,y,w,h);
			return myRect;
		}

		public void paint(Graphics g)
		{
			g.setColor(Color.gray);
			g.fillRect(0,0,getWidth(), getHeight());
			g.setColor(Color.black);


			Graphics2D g2 = (Graphics2D)g;
			// we'll use Graphics2D for it's "draw" method -
			// neater than the Graphics "drawRect" suppled
			// (which you could also use)

			for (int i = 0;i<numCols;i++)
				for(int j = 0;j<numRows;j++)
					g2.draw(getRect(i,j));

			for (int thisCol = 0; thisCol < model.getNumCols(); thisCol ++)
			{
				int num_of_counters = model.getNumCounters(thisCol);
				System.out.println("col " + thisCol + " has " + num_of_counters);
				for (int counter=0; counter < num_of_counters; counter ++)
				{
					int colour = model.getCounter(thisCol, counter);
					if(colour==Connect4Column.RED_COUNTER)
						g2.setColor(Color.red);
					else if(colour==Connect4Column.YELLOW_COUNTER)
						g2.setColor(Color.yellow);

					Rectangle r = getRect(thisCol, numRows-counter-1);
					if (r != null)
						g2.fillOval(r.x, r.y, r.width, r.height);
				}
			}
		}
	}

 }

Connect4Model

public class Connect4Model
{
	Connect4Column [] columns; // an array of columns
	int NUM_COLUMNS; // how many columns we have in the game
	int NUM_ROWS;    // how many rows we have in the game
	int playerToGoNext = Connect4Column.RED_COUNTER; //can be red or yellow
	int autoplay;

	public Connect4Model(int numCols, int numRows)
	{
		NUM_COLUMNS = numCols;
		NUM_ROWS = numRows;

		columns = new Connect4Column[NUM_COLUMNS];
		for (int i=0; i < NUM_COLUMNS; i++) {
			columns[i] = new Connect4Column(NUM_ROWS);
		}
	}

	int getNumCols() // simply return the number of columns
	{
		return NUM_COLUMNS;
	}

	int getNumRows() // simply return the number of rows
	{
		return NUM_ROWS;
	}

	int getNextPlayer() // returns who gets to go next
	{
		return playerToGoNext;
	}
	void reset()
	{
		for (int thisCol = 0; thisCol < NUM_COLUMNS; thisCol ++)
		{
			for (int thisRow = 0; thisRow < NUM_ROWS; thisRow ++)
			{
				columns[thisCol].numCounters = 0;
				columns[thisCol].counters[thisRow] = 0;
			}
		}
	}
	int getAutoplay()
	{
		return autoplay;
	}
	boolean setAutoplay(int selectedOption)
	{
		if(selectedOption >= 1 && selectedOption <= 3) // valid
		{
			autoplay = selectedOption;
			return true;

		}
		else
			return false;
	}

	int getSuggestedMove(int player_colour) // 2.5
	{
		java.util.Random r = new java.util.Random();
		for (int x = 0; x <= NUM_ROWS; x++)
		{
			go(r.nextInt(NUM_COLUMNS));
		}
		return player_colour;
	}

	boolean go(int thisColumn) // try to put a counter at this col
	{
		if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
			return false;

		System.out.println("thisCol is " + thisColumn);
		if(columns[thisColumn].addCounter(playerToGoNext)==true)
		{
			if(playerToGoNext==Connect4Column.YELLOW_COUNTER)
				playerToGoNext=Connect4Column.RED_COUNTER;
			else
				playerToGoNext=Connect4Column.YELLOW_COUNTER;
			return true;
		}
		return false;
	}

	int getCounter(int thisColumn, int thisRow) //
	{
		if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
			return 0;
		return columns[thisColumn].getCounter(thisRow);
	}
	int getNumCounters(int thisColumn) //
	{
		if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
			return 0;
		return columns[thisColumn].getNumCounters();
	}

}

This is what I have worked it out to be and yes you were correct I had to declare an int autoplay. Im currently doing an if statement so that it finds out whether the player to go next is set to autoplay if so, then it should paugse for one second using a try-catch block. I was wondering if what I have done is correct.

It is a common practice that the setter does not return a value; insteade trows an exception (for instance out of range exception) and should be declared void.
And I think that it is more explicite to declare tree static variable named for example NOCOLOR=0, RED=1 and YELLOW=2 or repectivly (1,2 and 3).
I did not now why you opte for this:

Thread.sleep(1000);

What is the point here? please!

It is a common practice that the setter does not return a value; insteade trows an exception (for instance out of range exception) and should be declared void.
And I think that it is more explicite to declare tree static variable named for example NOCOLOR=0, RED=1 and YELLOW=2 or repectivly (1,2 and 3).
I did not now why you opte for this:

Thread.sleep(1000);

What is the point here? please!

It was just set for me to make the Thread.sleep(1000) for what reasons I don't know fully. I've managed to sort it out however I'm working on getting the suggested next move (a column index) for this player and in order to this you have to call the 'go' method, using this column as the parameter.

If it is in the requirement it's ok.
But could not see the use of getAutoplay()!!
I suggest to put it in the pain method.

And I suggest to made this chage:

public void setAutoplay(int selectedOption) throws Exception {
        if (selectedOption >= 1 && selectedOption <= 3) // valid
        {
            autoplay = selectedOption;

        } else {
            throw new Exception("value out of range");
        }
    }

and make the getAutoplay as public.

Or you can just use :

public void setAutoplay(int selectedOption)  {
        if (selectedOption >= 1 && selectedOption <= 3) // valid
        {
            autoplay = selectedOption;

        } else {
            System.out.println("value out of range");//just for information
        }
    }
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.