Results 1 to 13 of 13
  1. #1
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Return statements in methods

    Edit** = so I figured out the return method, but how can I create a grid that expands with the int that I place in the method?

    So, I have to create a method that will take a picture and, using nested loops, depending on the variable placed in the method will be the size of the grid.
    so...

    public Picture makeGrid(int size)
    {
    code
    }

    so... if I put 2 in there it will copy the picture over so it will be a 2x2 grid of that picture and there will be 4 copies of the image.
    I am having trouble with my return statement, I don't know what to put as a return statement after all of this.
    Last edited by adjit; 03-17-2011 at 01:28 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You want to return a Picture so say you created a pocture object right away and made changes to it throughout the method you would return that object at the end.

    Here is an extremely simple example

    Java Code:
    public Picture makePic(int x){
      Picture pic = new Picture();
      pic.setSize(x);
      pic.setColor(Color.black);
      return pic;
    }
    I don't know what methods pic has but I used some randomly chosen ones in the example.

  3. #3
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    okay, so how can I create a grid that expands with the int that I put in to the method?

  4. #4
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    can anyone help me out?

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im not sure what the picture class looks like. Is it a java API class or your own? If the latter, post the code.

  6. #6
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import java.text.*;
    import java.util.*;
    import java.util.List; // resolves problem with java.awt.List and java.util.List

    /**
    * A class that represents a picture. This class inherits from
    * SimplePicture and allows the student to add functionality to
    * the Picture class.
    *
    * Copyright Georgia Institute of Technology 2004-2005
    * @author Barbara Ericson ericson@cc.gatech.edu
    */
    public class Picture extends SimplePicture
    {
    ///////////////////// constructors //////////////////////////////////

    /**
    * Constructor that takes no arguments
    */
    public Picture ()
    {
    /* not needed but use it to show students the implicit call to super()
    * child constructors always call a parent constructor
    */
    super();
    }

    /**
    * Constructor that takes a file name and creates the picture
    * @param fileName the name of the file to create the picture from
    */
    public Picture(String fileName)
    {
    // let the parent class handle this fileName
    super(fileName);
    }

    /**
    * Constructor that takes the width and height
    * @param width the width of the desired picture
    * @param height the height of the desired picture
    */
    public Picture(int width, int height)
    {
    // let the parent class handle this width and height
    super(width,height);
    }

    /**
    * Constructor that takes a picture and creates a
    * copy of that picture
    */
    public Picture(Picture copyPicture)
    {
    // let the parent class do the copy
    super(copyPicture);
    }

    /**
    * Constructor that takes a buffered image
    * @param image the buffered image to use
    */
    public Picture(BufferedImage image)
    {
    super(image);
    }

    ////////////////////// methods ///////////////////////////////////////

    /**
    * Method to return a string with information about this picture.
    * @return a string with information about the picture such as fileName,
    * height and width.
    */
    } //this is the end of class Picture, put all new methods before this

    this is in a program called drJava, and the assignment requires us to create a public Picture method with a grid that can change to the size that I put in as the int size

  7. #7
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    so, what is going to happen is I have to put the method under methods. We should be using nested loops and things of that sort. I could probably figure out how to copy the picture, but I don't know how to create the grid, because when I go to apply the method it keeps going off the grid to an undefined area.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do you have access to the parent class code?

  9. #9
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    yes, however, I can only write/change code under methods

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ok, I, and others may be able to help you more if we can see that code.

    Also, use code tags
    [code ]
    your code here
    [/code]

  11. #11
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    I have no code there, that is what I am trying to figure out

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Did your professor provide you with any methods in the picture class? Also, us seeing the SimplePicture class that Picture descends from will help out.

  13. #13
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    nope, everything you have is what I have.

    SPECIFICATION
    Create a new method at the bottom of Picture.java. The declaration will be as follows:
    public Picture makeGrid(int size)
    }
    ...
    {
    This method will create a square grid, size images wide by size images high, of duplicate copies of the original image. To be clear, the actual pixel dimensions of the grid may not be square, they will be proportional to the dimensions of the original image. Rather the number of images in the grid will be square. See the examples below for clarification.
    At the beginning of the method you will create a new Picture object for the grid with the correct dimensions. At the
    end, you should return the new Picture object. Refer to chapter 5 for help.

    those are the instructions

Similar Threads

  1. return multiple values from class methods
    By exdox77 in forum New To Java
    Replies: 0
    Last Post: 01-29-2011, 08:08 PM
  2. Methods, JOptionPane, Return Values
    By Cubba27 in forum New To Java
    Replies: 2
    Last Post: 12-04-2009, 02:46 AM
  3. design? return output within methods
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-08-2009, 07:22 AM
  4. Static Method and Return Statements
    By berelson in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 11:17 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
  •