Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program contains a main for the program. This can be left unedited. You can

ID: 3822116 • Letter: T

Question

The program contains a main for the program. This can be left unedited. You can add more code if you like, but do NOT delete or change any code already written.

SectionProblemLog.java has a number of places that indicate YOUR CODE GOES HERE. It will be your job to insert code at those points to make the methods works as indicated. See the sample jar file for details on formatting toString (it is 'X' for true and '_' for false).

public static void main(String[] args) {
final boolean X = true;
final boolean O = false;
boolean[][] grid = {{O, X, X, O, X, O, X, X, X, O},
{O, X, X, O, O, X, O, O, X, O},
{X, O, X, O, O},
{O, X, O, O, O, O, X, O, X}};
SectionProblemLog log = new SectionProblemLog(grid);
System.out.println(log);
System.out.println();
System.out.println("The number correct in each section is:");
try{
  
log.setCompleted(true, 3, 5);
for(int row = 1; row < SectionProblemLog.NUMBER_OF_SECTIONS; row++)
{
System.out.println(row + ": " + log.numberCorrectInSection(row));
}
}
catch(IndexOutOfBoundsException ex)
{
System.out.println(ex.getMessage());
}

public class SectionProblemLog {
public static int NUMBER_OF_SECTIONS = 5;
  
/**
* default constructor
* the log has 5 rows, but each is null
*/
SectionProblemLog() {
}
  
/**
* Sets the length of each row of log to the corresponding number from a.
* does nothing if a is null. If the length of a is less than 5, it
* sets only as many rows for which it has data. If the length of a is
* greater than 5, it only creates the five rows
* @param a an array holding the number of problems in each section
*/
SectionProblemLog(int[] a) {
if(a != null)
{
for(int i = 0; i < a.length && i < NUMBER_OF_SECTIONS; i++)
{
log[i] = new boolean[a[i]];
}
}
}
  
/**
* Sets each row of log to be a copy of the corresponding row from a.
* does nothing if a is null. If the length of a is less than 5, it
* sets only as many rows for which it has data. If the length of a is
* greater than 5, it only creates and sets the five rows
* @param a an array holding the log values
*/
SectionProblemLog(boolean[][] a) {
// YOUR CODE GOES HERE
  
if(a!=null){
  
}
  
}
  
/**
* mutator for log
* sets the rows of log to be copies of all rows passed in.
* missing rows in a should cause the corresponding row of log
* to be set to null
* @param a an array holding the log values
*/
final void setLog(boolean[][] a) {
if(a != null)
{
for(int i = 0; i < a.length && i < NUMBER_OF_SECTIONS; i++)
{
// YOUR CODE GOES HERE
}
}
}
/**
*
* @param complete the boolean to set at the specified section and number
* @param row the section in which the problem is located(starting with 1)
* @param col the number of the problem (starting with 1)
* @throws IndexOutOfBoundsException if problem not in the log array
*/
public void setCompleted(boolean complete, int row, int col)
throws IndexOutOfBoundsException
{
// YOUR CODE GOES HERE
}
  
/**
*
* @param row the section in which the problem is located(starting with 1)
* @param col the number of the problem (starting with 1)
* @return was the col problem in section row completed?
* @throws IndexOutOfBoundsException if problem not in the log array
*/
public boolean getCompleted(int row, int col)
throws IndexOutOfBoundsException
{
// YOUR CODE GOES HERE
}

/**
* accessor for log
* @return a copy of the log
*/
public boolean[][] getLog()
{
// YOUR CODE GOES HERE

}
  
/**
*
* @param section the number of the section (from 1 to 5)
* @return number correct in the specified section
* @throws IndexOutOfBoundsException if section not in the log array
*/
public int numberCorrectInSection(int section)
throws IndexOutOfBoundsException
{
int total = 0;
if(log[section - 1] != null)
{
for(boolean item: log[section - 1])
if(item)
total++;
}
return total;
}
  
/**
* String version of the log
* @return the array as a string
*/
@Override
public String toString()
{
String mark = "_";
String result = "";
for(int i = 0; i < NUMBER_OF_SECTIONS && log[i] != null; i++)
{
// YOUR CODE GOES HERE
  
  
}
return result;
}
  
private final boolean[][] log = new boolean[NUMBER_OF_SECTIONS][];
}

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TicToe extends JFrame implements ItemListener, ActionListener{
int i,j,a,b,a1,b1,fc;   
int a[][]={{10,1,2,3,11},{10,1,4,7,11},{10,1,5,9,11},{10,2,5,8,11},
{10,3,5,7,11},{10,3,6,9,11},{10,4,5,6,11},
{10,7,8,9,11} };
int a1[][]={{10,1,2,3,11},{10,1,4,7,11},{10,1,5,9,11},{10,2,5,8,11},
{10,3,5,7,11},{10,3,6,9,11},{10,4,5,6,11},{10,7,8,9,11} };
  
boolean cond,fact,group;
  
ico XCX,OCO,ico,ico1,ico2;
Checkbox c1,c2;
JLabel l1,l2;
JButton b[]=new JButton[9];
JButton reset;
  
public void displaybtn(){
  
a1=10; b1=10;j=0;
for(i=0;i<=8;i++,a1+=100,j++){
b[i]=new JButton();
if(j==3)
{j=0; b1+=100; a1=10;}
b[i].setBounds(a1,b1,100,100);
add(b[i]);
b[i].addActionListener(this);
}
  
reset=new JButton("RESET");
reset.setBounds(100,350,100,50);
add(reset);
reset.addActionListener(this);
  
}
public void check(int num1){
for(a=0;a<=7;a++){
for(b=1;b<=3;b++){
if(a[a][b]==num1){ a[a][4]=11; }
  
}
  
}
}
  
public void complogic(int num){
  
for(i=0;i<=7;i++){
for(j=1;j<=3;j++){
if(a[i][j]==num){ a[i][0]=11; a[i][4]=10; }
}
}
for(i=0;i<=7;i++){   
group=true;   
if(a[i][4]==10){   
int count=0;
for(j=1;j<=3;j++){
if(b[(a[i][j]-1)].getIcon()!=null){
count++;
}   
else{ fc=a[i][j]; }
}   
if(count==2){
b[fc-1].setIcon(OCO);   
this.check(fc); group=false;break;
}
}   
else
if(a[i][0]==10){
for(j=1;j<=3;j++){   
if(b[(a[i][j]-1)].getIcon()==null){ //if 1
b[(a[i][j]-1)].setIcon(OCO);
this.check(a[i][j]);
group=false;
break;
}
}
if(group==false)
break;   
}
  
if(group==false)
break;
}
  
  
}
  
  

  
TicToe(){
super("tic tac toe by akshay");
  
CheckboxGroup check_grup=new CheckboxGroup();
c1=new Checkbox("vs computer",check_grup,false);
c2=new Checkbox("vs friend",check_grup,false);
c1.setBounds(120,80,100,40);
c2.setBounds(120,150,100,40);
add(c1); add(c2);
c1.addItemListener(this);
c2.addItemListener(this);
  
  
cond=true;fact=true;group=true;
XCX=new ImageIcon("XCX.jpg");
OCO=new ImageIcon("OCO.jpg");
ico1=new ImageIcon("ico1.jpg");
ico2=new ImageIcon("ico2.jpg");
  
setLayout(null);
setSize(330,450);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
  

public void itemStateChanged(ItemEvent e){
if(c1.getState())
{   
fact=false;
}
  
else if(c2.getState())
{ fact=true;
}
remove(c1);remove(c2);
repaint(0,0,330,450);
displaybtn();
}
  
public void actionPerformed(ActionEvent e){
  
if(fact==true)
{
if(e.getSource()==reset){
for(i=0;i<=8;i++){
b[i].setIcon(null);
}
}
else{   
for(i=0;i<=8;i++){
if(e.getSource()==b[i]){

if(b[i].getIcon()==null){
if(cond==true){ ico=OCO;   
cond=false;} else{ ico=XCX; cond=true; }
b[i].setIcon(ico);
}
}   
}
}
}
else if(fact==false){
if(e.getSource()==reset){
for(i=0;i<=8;i++){
b[i].setIcon(null);
}//eof for   
for(i=0;i<=7;i++)
for(j=0;j<=4;j++)
a[i][j]=a1[i][j];   
}
else{ //complogic
for(i=0;i<=8;i++){
if(e.getSource()==b[i]){
if(b[i].getIcon()==null){   
b[i].setIcon(XCX);
if(b[4].getIcon()==null){
b[4].setIcon(OCO);
this.check(5);
} else{
this.complogic(i);
}
}
}
}
}
}
  
for(i=0;i<=7;i++){
  
ico icon1=b[(a[i][1]-1)].getIcon();
ico icon2=b[(a[i][2]-1)].getIcon();
ico icon3=b[(a[i][3]-1)].getIcon();
if((icon1==icon2)&&(icon2==icon3)&&(icon1!=null)){
if(icon1==XCX){   
b[(a[i][1]-1)].setIcon(ico1);
b[(a[i][2]-1)].setIcon(ico1);   
b[(a[i][3]-1)].setIcon(ico1);
JOptionPane.showMessageDialog(TicToe.this,"!!!YOU won!!! click reset");   
break;
}
else if(icon1==OCO){   
b[(a[i][1]-1)].setIcon(ico2);
b[(a[i][2]-1)].setIcon(ico2);
b[(a[i][3]-1)].setIcon(ico2);   
JOptionPane.showMessageDialog(TicToe.this,"won! click reset");
break;   
}
}
}
  
  
}

  
public static void main(String []args){
new TicToe();
}
}