Results 1 to 13 of 13
Thread: Return statements in methods
- 03-17-2011, 12:56 AM #1
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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.
- 03-17-2011, 01:13 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
I don't know what methods pic has but I used some randomly chosen ones in the example.Java Code:public Picture makePic(int x){ Picture pic = new Picture(); pic.setSize(x); pic.setColor(Color.black); return pic; }
- 03-17-2011, 01:28 AM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
okay, so how can I create a grid that expands with the int that I put in to the method?
- 03-17-2011, 02:02 AM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
can anyone help me out?
- 03-17-2011, 02:04 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Im not sure what the picture class looks like. Is it a java API class or your own? If the latter, post the code.
- 03-17-2011, 02:06 AM #6
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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
- 03-17-2011, 02:08 AM #7
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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.
- 03-17-2011, 02:22 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Do you have access to the parent class code?
- 03-17-2011, 02:23 AM #9
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
yes, however, I can only write/change code under methods
- 03-17-2011, 02:25 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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]
- 03-17-2011, 02:50 AM #11
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
I have no code there, that is what I am trying to figure out
- 03-17-2011, 03:56 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 03-17-2011, 02:31 PM #13
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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
-
return multiple values from class methods
By exdox77 in forum New To JavaReplies: 0Last Post: 01-29-2011, 08:08 PM -
Methods, JOptionPane, Return Values
By Cubba27 in forum New To JavaReplies: 2Last Post: 12-04-2009, 02:46 AM -
design? return output within methods
By jon80 in forum New To JavaReplies: 1Last Post: 06-08-2009, 07:22 AM -
Static Method and Return Statements
By berelson in forum New To JavaReplies: 2Last Post: 11-29-2008, 11:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks