CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Mar 2005
    Posts
    9

    Assignment Methods

    hi guys
    im a uni student and i got an assignment where i have to right code for a magic square where each row and column of a 3x3 square has to equal the same number in order to be a "magic" square.
    According to the assignment requirements i need to have one class constructor and instance variable along with 11 methods. (if your interested i can send you the document)
    I have had no previous experience with java, we have been going to about 5 weeks and i know the basics.
    What i dont understand is why so many methods are created when you could do all this in the main method. Also because the markers want the methods seperated, i dont know how to start and what to put in these methods.
    I know if i used logic i could get this program working, but not with the required loop statements and boolean types that are needed.
    thanx

    This is what the main aprt of the assignment asks for


    The java class MagicSquare you create must have:
    One instance variable…
    • A two dimensional array of integers that will hold the numbers for this magic
    square attempt.
    One class constructor…
    • The constructor accepts a single parameter which is an integer that represents
    the size dimension of the square.
    • The constructor must use this parameter to initialize the array of integers.
    Eleven class methods…
    1. getCell
    This method accepts two parameters and has a return value.
    The first parameter (an integer) specifies which row the target cell is on.
    The second parameter (integer) specifies which column the target cell is in.
    The return value is the integer stored in the specified cell.
    2. getMaxCellValue
    This method accepts no parameters and returns a single integer value that is
    the highest valid number that a cell can have. This value is equal to the
    number of cells in the square.
    3. getSize
    This method accepts no parameters and has a return value that is the
    integer size (or “dimension”) value of the current square. (This should be
    equal to the length of each row in the array.)
    4. isMagic
    This method accepts no parameters and returns a boolean value.
    The boolean value returned will be true if this is a “semi magic” square. That
    is, all the columns and rows must add up to the same value which must be
    greater than 0. If any row or column adds up to a different value from any of
    the others, this method should return false.
    5. isNew
    This method accepts a single parameter and returns a boolean.
    The parameter for this method is an integer.
    This method returns true if and only if the parameter has not already been
    used in this square. Each number in the magic square can only be used once.
    If the parameter is a number that is already being used, this method must
    return false.
    HIT1051/HIT1151 Assignment 1
    6. printSquare
    This method accepts no parameters and has no return value.
    This method prints the values stored in this square on the screen. Each row
    of the square should be printed on it’s own line of output, and each number
    should be separated by a space.
    (See the sample output below for an example.)
    7. setCell
    This method accepts three parameters and has no return value.
    The first parameter is an integer specifying which row the cell is in.
    The second parameter is an integer specifying which column the cell is in.
    The third parameter is an integer which is the number that should get stored
    in the specified cell. This number does not need to get validated in this
    method. It is assumed that row, column and value parameters are all
    correct.
    8. setup
    This method accepts no parameters and has no return value.
    This method loops through the square and asks the use for a value for each
    cell. These values must be validated before being saved in the array. (See
    the validateNumber method for more details on what is considered valid.)
    9. sumCol
    This method accepts one parameter and returns an integer.
    The parameter for this method is an integer, which is the number of the
    column of the square to be summed. The return value is the sum of all the
    values on the specified column.
    10. sumRow
    The same as sumCol(), except it returns the sum of the values in the
    specified row.
    11. validateNumber
    This method accepts a single parameter and returns a boolean.
    The parameter for this method is an integer.
    If the parameter is a valid number for this square, then this method returns
    true. Otherwise it returns false.
    A valid number is one that is greater than 0, and less than or equal to the
    number of cells in this square and has not already been used in this square.
    (See details for the isNew() method for more details). E.g., a valid number
    for a square of size 3 would be 1 to 9, inclusive.

  2. #2
    Join Date
    Feb 2003
    Location
    Sunny Glasgow!
    Posts
    258

    Re: Assignment Methods

    Quote Originally Posted by barney007
    hi guys
    im a uni student and i got an assignment where i have to right code for a magic square where each row and column of a 3x3 square has to equal the same number in order to be a "magic" square.
    According to the assignment requirements i need to have one class constructor and instance variable along with 11 methods. (if your interested i can send you the document)
    I have had no previous experience with java, we have been going to about 5 weeks and i know the basics.
    What i dont understand is why so many methods are created when you could do all this in the main method. Also because the markers want the methods seperated, i dont know how to start and what to put in these methods.
    I know if i used logic i could get this program working, but not with the required loop statements and boolean types that are needed.
    thanx
    That is what OO programming is all about. Progedural programming shuch as C used long main methods, but OO is all about "getting the ball rolling" and then delegating to other classes. In order to do this, you need to have methods separated out, otherwise when you start doing code on a larger scale, you will hit loads of problems trying to find values of objects.

    This is what the main aprt of the assignment asks for


    The java class MagicSquare you create must have:
    One instance variable…
    • A two dimensional array of integers that will hold the numbers for this magic
    square attempt.
    One class constructor…
    • The constructor accepts a single parameter which is an integer that represents
    the size dimension of the square.
    • The constructor must use this parameter to initialize the array of integers.
    Eleven class methods…
    1. getCell
    This method accepts two parameters and has a return value.
    The first parameter (an integer) specifies which row the target cell is on.
    The second parameter (integer) specifies which column the target cell is in.
    The return value is the integer stored in the specified cell.
    2. getMaxCellValue
    This method accepts no parameters and returns a single integer value that is
    the highest valid number that a cell can have. This value is equal to the
    number of cells in the square.
    3. getSize
    This method accepts no parameters and has a return value that is the
    integer size (or “dimension”) value of the current square. (This should be
    equal to the length of each row in the array.)
    4. isMagic
    This method accepts no parameters and returns a boolean value.
    The boolean value returned will be true if this is a “semi magic” square. That
    is, all the columns and rows must add up to the same value which must be
    greater than 0. If any row or column adds up to a different value from any of
    the others, this method should return false.
    5. isNew
    This method accepts a single parameter and returns a boolean.
    The parameter for this method is an integer.
    This method returns true if and only if the parameter has not already been
    used in this square. Each number in the magic square can only be used once.
    If the parameter is a number that is already being used, this method must
    return false.
    HIT1051/HIT1151 Assignment 1
    6. printSquare
    This method accepts no parameters and has no return value.
    This method prints the values stored in this square on the screen. Each row
    of the square should be printed on it’s own line of output, and each number
    should be separated by a space.
    (See the sample output below for an example.)
    7. setCell
    This method accepts three parameters and has no return value.
    The first parameter is an integer specifying which row the cell is in.
    The second parameter is an integer specifying which column the cell is in.
    The third parameter is an integer which is the number that should get stored
    in the specified cell. This number does not need to get validated in this
    method. It is assumed that row, column and value parameters are all
    correct.
    8. setup
    This method accepts no parameters and has no return value.
    This method loops through the square and asks the use for a value for each
    cell. These values must be validated before being saved in the array. (See
    the validateNumber method for more details on what is considered valid.)
    9. sumCol
    This method accepts one parameter and returns an integer.
    The parameter for this method is an integer, which is the number of the
    column of the square to be summed. The return value is the sum of all the
    values on the specified column.
    10. sumRow
    The same as sumCol(), except it returns the sum of the values in the
    specified row.
    11. validateNumber
    This method accepts a single parameter and returns a boolean.
    The parameter for this method is an integer.
    If the parameter is a valid number for this square, then this method returns
    true. Otherwise it returns false.
    A valid number is one that is greater than 0, and less than or equal to the
    number of cells in this square and has not already been used in this square.
    (See details for the isNew() method for more details). E.g., a valid number
    for a square of size 3 would be 1 to 9, inclusive.
    The methods are the methods that are needed to get/set the values of the data - you need these in OO programming (see first comment).
    One thing I did notice is that you seem to be getting mixed up with class and method. A class is the whole piece of code and a method is a 'function' in the code. A class can contain many methods.

    Every java class requires a constructor, so if your class is called Test.java, the constructor will be:
    Code:
    public Test()
    {
    
    }
    This is an empty constructor - ie. takes no arguments (the bit between the '()' brackets)
    You will need to have an integer argument which will set the size of the square.
    Code:
    public Test(int size)
    {
      //use this value to set the size of the array - ie. myArray[size]
    }
    I need to go now, but hopefully one part of what I have written helps.

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Assignment Methods

    Following on from what themoffster said, once you have written the constructor that sets up the array, write a method for every step in the instructions. You are given the method name, the parameters it takes and what it returns, so most shouldn't be any problem, e.g. the first is 'getCell' that takes 2 int parameters, row and column, and returns an int with the contents of the cell at that position in the array:
    Code:
    int getCell(int row, int column) {
        // return the array element at the specified row and column
    }
    If you write just the method signatures first (i.e. the methods without any code inside), then go back and start filling in the code for each method, you'll get more of an idea of how the whole thing will look and work when finished.

    If you get stuck on any method, just ask. If it may help, post up the code you have so far so we can see what's going on.

    Sketch the outline first, then fill in the details...
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: Assignment Methods

    The program is basically laid out by your instructor, all you need to do is interpret each statement and implement. Here's a skeleton with the constructor and getCell():
    Code:
    public class MagicNumberSquare
    {
        // 2-d array that represents the magic squares
        int[][] magicSquare;
        
        public MagicNumberSquare(int size)
        {   magicSquare = new int[size][size]; // short and simple
        }
        
        public int getCell(int row, int column)
        {   // if you havn't dealt with if statements (decision statements) or exceptions yet,
            // then ignore the next 2 lines
            if (row < 0 || row > magicSquare.length || column < 0 || column > magicSquare.length)
                throw new IndexOutOfBoundsException("Either the row or column specified is attempting to access a square that doesn't exist");
            
            return magicSquare[row][column];
        }
    }
    Note: If you don't understand the code I just put up, then you have a long ways to go.
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  5. #5
    Join Date
    Mar 2005
    Posts
    9

    Re: Assignment Methods

    ok, im slowly getting there
    i do understand to an extent what cma has written in his code, its just that i dont understand what some are the methods are for. for instance getCell, i dont know what it does, getSize as well.
    in terms of cma's code:

    if (row < 0 || row > magicSquare.length || column < 0 || column > magicSquare.length)

    to me this means if row is less then 0 or row is greater then magicsquare.length that message. I dont quite understand the magicsquare.length, im not sure what is.
    As well as "throw new IndexOutOfBoundsException."

    The rest seems fine. but my main problem is what are the purposes of these methods?

  6. #6
    Join Date
    Feb 2003
    Location
    Sunny Glasgow!
    Posts
    258

    Re: Assignment Methods

    Basically, the magic square is of length 0->magicSquare.length.
    In java, it is good programming principle to use variables like magicSquare.length to represent the size rather than jusy (say) 4.
    It also becomes useful when you dnyamically change the size of the magicSquare (this may not happen in this assignment, but when you start coding more advance things in java, their sizes will chage - lists, queues etc.)

    Basically all that if statement is saying is if the number of the grid element you are trying to access is <0 or >magicSquare.length throw an exception - in this case an ArrayOutOfBoundsException - which is a special exception in Java.
    This is done because the array is of size 0>magicsquare.length, so tryig to access a grid outside this range would result in an error - so the exception is thrown.

    You follow?

    edit - forgot to add things

    The magicSquare is an array in 2D, so it loks something like this:
    Code:
    [00] [01] [02] [03]
    [04] [05] [06] [07]
    [08] [09] [10] [11]
    [12] [13] [14] [15]
    Where here, the number of the grid is shown.

    The getCell method retruns the number of the grid, so if we called:
    Code:
    magicSquare.getGrid(0,2)
    we would get 08 back, as 0 along 2 down is grid number 08. (normally it would just be 8, but I had to pad zeros to the front of the singly digit numbers to make the structure of the array clearer).

    The getSize method just returns an integer of the size of the grid. In this case if you called
    Code:
    magicSquare.getSize()
    16 would be returned as there are 16 elements making up the magicSquare.
    Last edited by themoffster; March 30th, 2005 at 08:29 AM.

  7. #7
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: Assignment Methods

    Don't worry about the "throw ..." part of the code if you don't understand it. You'll eventually learn about exception handling later on in the course (it's a guarantee).

    About the magicSquare.length, you have to know/remember that arrays are objects in Java. length is an instance variable that all array objects have that tell you the size of the array. A common beginner mistake is to keep an extra variable telling you the capacity of the array, which is unecessary.
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Assignment Methods

    Quote Originally Posted by barney007
    ... my main problem is what are the purposes of these methods?
    In OO programming, an instance of a class (an object) consists of some hidden (private) data, and a set of operations that can be performed on it / with it. The data is hidden because someone using the object doesn't need to know the details of what's inside the object, only what they can do with it. The idea is to make the object behave just like an ideal real-world object.

    If you want to play around with a MagicSquare object, you don't need to know how the data is organised inside it, just that you can (for example) find out what the contents of a particular cell in the MS are by asking it what is in the cell at the particular row and column you are interested in, using getCell(row, column). This makes it a lot easier to use the object in a program, because no matter how the data is stored inside, you have a simple, relevant set of methods to use on the object. It also means that you can change your mind about how the internal data is represented without having to rewrite all the code that uses that object. For example, your magic square data could be sitting on a remote server or database somewhere instead of in the object itself, but users of MagicSquare could still find out what's in a particular cell because they just use the getCell(...) method which gives them the answer - they don't know or care whether it is looked up over the internet or stored locally or read from file.

    The principle of hiding data and implementation details is called encapsulation. When you write a class to model an object like a MagicSquare, think first how users of the class would want to use the object, and assemble a set of black-box method declarations that will do the job before you start thinking about exactly how they will do it and how the data will be stored or laid out inside.

    An advantage of writing a bunch of methods to handle basic operations on your object is that you can use them yourself when writing the more complicated methods that the object may have.

    If you don't think carefully, you might believe that programming is just typing statements in a programming language...
    W. Cunningham
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    Join Date
    Mar 2005
    Posts
    9

    Re: Assignment Methods

    ok guys, i understand now
    and also know what the point of getSize and getCell are....
    well ill go off and start trying to code this thing, ill kepp you posted with details
    thanx

  10. #10
    Join Date
    Mar 2005
    Posts
    9

    Re: Assignment Methods

    well guys, i found out this week, that we havent even learned about the basics about arrays and for loops, now that we have done a little bit on that, we still have to go over two dimensional arrays and nested loops before we can start the assignment.

  11. #11
    Join Date
    Mar 2005
    Posts
    9

    Re: Assignment Methods

    hi guys, this is my code atm.

    import java.util.Scanner;

    public class MagicSquare
    {
    public static void main (String [] args)
    {

    }

    }

    class Magic
    {
    int[][] number;

    public Magic(int size);
    {
    number = new int [size][size];
    }

    public int getCell(int row, int column);
    {

    }

    }

    i know its not much, but i know there are already errors in this.
    According to me, it sounds like its better off to start at the printSquare method. Can anyone help me as to how i would go about this, or even if im thinking about starting at the wrong spot.
    thanx guys

  12. #12
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Assignment Methods

    From the code you posted, you don't seem to have done anything at all yourself, just copied bits of what cma provided. I suggested coding each of the method declarations in the class and cma even gave you the code for the getCell method, so why not get that stuff done? The method declarations are all described in the instructions, so they shouldn't give you any difficulty. Then you can start filling out the method bodies and we can help there.

    We have already given plenty of clues and assistance - why should we continue when you haven't used what we already provided?

    This is supposed to be your assignment, not ours. If you make an effort we will help.

    Teachers open the door, but you must enter by yourself...
    Chinese proverb.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  13. #13
    Join Date
    Mar 2005
    Posts
    9

    Re: Assignment Methods

    well its kind of hard, when you have had absolutely no experience in programming previous to this point
    Sorry if you feel your doing all the work.

  14. #14
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Assignment Methods

    None of this is work for me, I do it to relax. But I do know what it takes to learn it - I knew nothing about it when I first started. You need to learn the basics before you can program, which means a Java course or a good tutorial book (the Java Tutorial is pretty good). You can't learn to program off a Java forum or newsgroup, but you can get help when you have difficulty.

    But in the end, you have to do something yourself. If you don't want to follow the suggestions we make, then don't, but you have to do something. Read the Java Tutorial, try and follow the examples it gives, read your course notes if you have some, find a good Java book. Figure out what you have to do before you start writing code. It's not difficult, programmers aren't cleverer than everyone else, but it does require a bit of study and thought.

    If you get stuck, ask a specific question.

    The truth is, when all is said and done, one does not teach a subject, one teaches a student how to learn it. Teaching may look like administering a dose, but even a dose must be worked on by the body if it is to cure. Each individual must cure his or her own ignorance...
    J. Barzun
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  15. #15
    Join Date
    Apr 2005
    Posts
    2

    Re: Assignment Methods

    ahh...SD1 rite guys - barney 007....
    yea...im having ALOT of trouble with it as well...


    /**
    * MagicSquare class defines the way the Magic Square is to be set up.
    *@
    * PUBLIC FEATURES:
    * //constructors
    * public class MagicSquare (int size)

    * //methods
    * public int getCell() //prints out the value in the targeted cell
    * @ param Row //looks at the row the cell is in
    * @ param Col //looks at the column the cell is in
    * public intgetMaxCellValue() //
    *
    * COLLABORATORS:
    * Scanner

    ^^^ is this correct for the JavaDoc Class Header??? look at the @pram plz...

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured