Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9197413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:04:10+00:00 2026-06-17T22:04:10+00:00

This is homework for my AP Computer Science class dealing with 2D Arrays. Basically

  • 0

This is homework for my AP Computer Science class dealing with 2D Arrays. Basically what the program does is input values into a Magic Square (all rows, columns, and diagonals add up to be the same number) and determines if it is a Magic Square. My algorithm continually puts out false (according to the isMagic() method) for when it checks if it’s Magic. I believe I have problems in my row() and column() methods and if anyone could provide me a solution that would be great.

public class Square
{
  private int [][] sq;
  private int position = 0;

  public Square(int size)
  {
    sq = new int[size][size];
  }

  /**
   * adds value to the matrix at the given position, row,col
   */

  public void add(int value, int row, int col)
  {
    sq[row][col] = value;
    // fill in code here.

  }

  public boolean fullSquare()
  {
    int numcheck = 1;
    boolean found = false;
    while (numcheck < sq.length*sq.length)
    {
      for(int i = 0; i < sq.length; i++)
        for(int j = 0; j < sq.length; j++)
      {
        if(sq[i][j] == numcheck)
        {
          found = true;
        }
      }
      numcheck++;

      //use nested for loops to loop through array sq.
      //if the value in sq == numcheck, set found to true

      //After the loop, check to see if numcheck was found 
      //if not found, return false, otherwise set found to false
      //and increment numcheck to be ready to check the next number
    }
    if(found == false)
      return false;
    else
      return true;
  }

  public boolean isMagic()
  {
    int targetNum = 0;
    boolean stuff = false;

    for (int c = 0; c < sq[0].length; c++)
    {
      targetNum += sq[0][c];
    }
    if(column(targetNum) == true && row(targetNum) == true && diagonalLeft(targetNum) == true && diagonalRight(targetNum) == true && fullSquare() == true)
    {
      return true;
    }
    else
      return false;
    //if the rows, columns, diagonals and fullSquare are all
    //true, return true, otherwise, return false.

  }

  public boolean diagonalLeft(int tN)
  {
    int sum = 0;
    //write loop to see if the diagonal from 
    //lower left to upper right is the same as tN
     for(int d = 0; d < sq[0].length; d++)
    {
      sum = sum + sq[d][(sq.length-1) - d];
    }
    return (sum == tN);
  }

  public boolean diagonalRight (int tN)
  {
    int sum = 0;
   //write loop to see if the diagonal from upper left
    //to lower right is the same as tN
    for (int d = 0; d < sq[0].length; d++)
    {
        sum = sum + sq[d][d];
    }

   return (sum == tN);
  }

  public boolean column (int tN)
  {
    boolean same = true;
    int sum = 0;

    //write nested loops to check each column's sum
    //if the sum is not the same as tN, set same to false.
    for(int i = 0; i < sq[0].length; i++)
    {
      for(int j = 0; j < sq[0].length; j++)
      {
        sum = sum + sq[j][i];
      }
    }
    if(sum == tN)
      same = true;
    else
      same = false;

    return same;
  }

  public boolean row(int tN)
  {
   boolean same = true;
    int sum = 0;

    //write nested loops to check each column's sum
    //if the sum is not the same as tN, set same to false.
    for(int i = 0; i < sq[0].length; i++)
    {
      for(int j = 0; j < sq[0].length; j++)
      {
        sum = sum + sq[i][j];
      }
    }
    if(sum == tN)
      same = true;
    else
      same = false;

    return same;
  }

  public String toString()
  {
    String s = "";
    for (int r = 0; r < sq.length; r++)
    {
      for (int c = 0; c < sq[r].length; c++)
      {
        s += sq[r][c] + " ";
      }
      s+= "\n";
    }
    return s;
  }
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:04:11+00:00Added an answer on June 17, 2026 at 10:04 pm

    The main problem you’re having is you are checking if the sum is the same outside the for loop. I’d re-write it like this:

    public boolean rowAndColumn(int tN)
    {
      int rowsum = 0, colsum = 0;
      for(int i = 0; i < sq[0].length; i++)
      {
        for(int j = 0; j < sq[0].length; j++)
        {
          rowsum = rowsum + sq[i][j];
          colsum = colsum + sq[j][i];
        }
        if(rowsum != tN || colsum != tN)
          return false; //no point checking the rest if the sums doesn't match
        rowsum = 0; //reset row count
        colsum = 0; //reset col count
      }
      return true; //if it doesn't return by here, all the sums match
    }
    

    Edit: fullSquare also won’t work as intended. Try something like this:

    public boolean fullSquare()
    {
      int numcheck = 1;
      boolean found = false;
      while (numcheck < sq.length*sq.length)
      {
        for(int i = 0; i < sq.length;
          for(int j = 0; j < sq.length; j++)
            if(sq[i][j] == numcheck)
              found = true;
    
        if (!found)
          return false; //if the number wasn't found, it's not a full square
        found = false;
        numcheck++;
      }
        //use nested for loops to loop through array sq.
        //if the value in sq == numcheck, set found to true
    
        //After the loop, check to see if numcheck was found 
        //if not found, return false, otherwise set found to false
        //and increment numcheck to be ready to check the next number
      return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is homework. Beginning Java class. Still wrapping my head around this stuff. The
Basically i am having problems with my code - this is homework so would
I'll be taking a Python-based computer science class next semester using my MacBook Pro.
I was trying to do my computer science homework but I am stuck as
(As the 'homework' tag indicates, this is part of a big project in Computer
A college freshman I know taking an intro to computer science class asked me
I am doing a homework assignment for my Computer Science course. The task is
A homework assignment for a computer networking software dev class, the prof has us
I just finished a homework problem for Computer Science 1 (yes, it's homework, but
I've been working through a recent Computer Science homework involving recursion and big-O notation.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.