Results 1 to 13 of 13
Thread: Creating a main method?
- 11-16-2010, 08:15 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
Creating a main method?
I'm creating a simple .java file that has a method called scaleUp, which just increases the size of a picture. The problem I'm having is in creating the main method. The following is what I've come up with:
public class ScaleUp
{
public static Picture main(String[] args)
{
public Picture scaleUp(int numTimes)
{
Picture targetPicture =
new Picture(this.getWidth() * numTimes,
this.getHeight() * numTimes);
Pixel sourcePixel = null;
Pixel targetPixel = null;
int targetX = 0;
int targetY = 0;
for (int sourceX = 0;
sourceX < this.getWidth();
sourceX++)
{
for (int sourceY=0;
sourceY < this.getHeight();
sourceY++)
{
sourcePixel = this.getPixel(sourceX,sourceY);
for (int indexY = 0; indexY < numTimes; indexY++)
{
for (int indexX = 0; indexX < numTimes; indexX++)
{
targetX = sourceX * numTimes + indexX;
targetY = sourceY * numTimes + indexY;
targetPixel = targetPicture.getPixel(targetX,
targetY);
targetPixel.setColor(sourcePixel.getColor());
}
}
}
}
return targetPicture;
}
}
}
When I try to compile the above, I got the following message
3 errors found:
File: C:\Users\Umar\Desktop\ScaleUp.java [line: 7]
Error: C:\Users\Umar\Desktop\ScaleUp.java:7: illegal start of expression
File: C:\Users\Umar\Desktop\ScaleUp.java [line: 7]
Error: C:\Users\Umar\Desktop\ScaleUp.java:7: ';' expected
File: C:\Users\Umar\Desktop\ScaleUp.java [line: 7]
Error: C:\Users\Umar\Desktop\ScaleUp.java:7: ';' expected
What am I doing wrong?
Thanks in advance.
- 11-16-2010, 08:27 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
You are declaring a method inside another method. You can't do that.
- 11-16-2010, 08:30 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,377
- Blog Entries
- 7
- Rep Power
- 17
You are trying to define a method in the body of another method; Java can't do that. Your class structure has to be like this:
Note the return type of the main( ... ) method; it has to be void by definition.Java Code:public class ScaleUp { public static void main(String[] args) { ... } public Picture scaleUp(int numTimes) { ... } ...
kind regards,
Jos
edit: darn, so early in the morning and already too slow ... ;-)Last edited by JosAH; 11-16-2010 at 08:32 AM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-16-2010, 08:57 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
Thanks for the quick response.
I have another question though, what do I declare after:
I tried this:Java Code:public static void main(String[] args) {
but I was returned many errors like the following:Java Code:public static void main(String[] args) { String fileName = FileChooser.pickAFile(); Picture pictObj = new Picture(fileName); } public Picture scaleUp(int numTimes) {...}
File: C:\Users\Umar\Desktop\ScaleUp.java [line: 10]
Error: C:\Users\Umar\Desktop\ScaleUp.java:10: cannot find symbol
symbol : method getWidth()
location: class ScaleUp
I think I have to refer to my method or an object that can call on the method getWidth(), but I'm not sure how to do this.
Thanks again for the help.
- 11-16-2010, 09:02 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,377
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-16-2010, 09:19 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
Sorry about that:
The errors are:Java Code:public class ScaleUp { public static void main(String[] args) { String fileName = FileChooser.pickAFile(); Picture pictObj = new Picture(fileName); } public Picture scaleUp(int numTimes) { Picture targetPicture = new Picture(this.getWidth() * numTimes, this.getHeight() * numTimes); Pixel sourcePixel = null; Pixel targetPixel = null; int targetX = 0; int targetY = 0; for (int sourceX = 0; sourceX < this.getWidth(); sourceX++) { for (int sourceY=0; sourceY < this.getHeight(); sourceY++) { sourcePixel = this.getPixel(sourceX,sourceY); for (int indexY = 0; indexY < numTimes; indexY++) { for (int indexX = 0; indexX < numTimes; indexX++) { targetX = sourceX * numTimes + indexX; targetY = sourceY * numTimes + indexY; targetPixel = targetPicture.getPixel(targetX, targetY); targetPixel.setColor(sourcePixel.getColor()); } } } } return targetPicture; } }
File: C:\Users\Umar\Desktop\ScaleUp.java [line: 10]
Error: C:\Users\Umar\Desktop\ScaleUp.java:10: cannot find symbol
symbol : method getWidth()
location: class ScaleUp
File: C:\Users\Umar\Desktop\ScaleUp.java [line: 11]
Error: C:\Users\Umar\Desktop\ScaleUp.java:11: cannot find symbol
symbol : method getHeight()
location: class ScaleUp
File: C:\Users\Umar\Desktop\ScaleUp.java [line: 18]
Error: C:\Users\Umar\Desktop\ScaleUp.java:18: cannot find symbol
symbol : method getWidth()
location: class ScaleUp
File: C:\Users\Umar\Desktop\ScaleUp.java [line: 22]
Error: C:\Users\Umar\Desktop\ScaleUp.java:22: cannot find symbol
symbol : method getHeight()
location: class ScaleUp
File: C:\Users\Umar\Desktop\ScaleUp.java [line: 25]
Error: C:\Users\Umar\Desktop\ScaleUp.java:25: cannot find symbol
symbol : method getPixel(int,int)
location: class ScaleUp
- 11-16-2010, 09:22 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Because you haven't defined those methods. You are calling "this.getWidth....", which means, of course, that those methods must either be defined in that class, or one its super classes (i.e. some class "up the extension chain"). Since you are not extending anything, and haven't defined the methods here, those methods simply don't exist.
- 11-16-2010, 09:51 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
Thanks! I see what I did there. You guys have been very helpful but I have another question.
My code now is:
It compiles fine but when I type the following to test my program in the interactions pane:Java Code:public class ScaleUp { public static void main(String[] args) { String fileName = FileChooser.pickAFile(); Picture pictObj = new Picture(fileName); } public Picture scaleUp(int numTimes) { String fileName = FileChooser.pickAFile(); Picture origPic = new Picture(fileName); Picture targetPicture = new Picture(origPic.getWidth() * numTimes, origPic.getHeight() * numTimes); Pixel sourcePixel = null; Pixel targetPixel = null; int targetX = 0; int targetY = 0; for (int sourceX = 0; sourceX < origPic.getWidth(); sourceX++) { for (int sourceY=0; sourceY < origPic.getHeight(); sourceY++) { sourcePixel = origPic.getPixel(sourceX,sourceY); for (int indexY = 0; indexY < numTimes; indexY++) { for (int indexX = 0; indexX < numTimes; indexX++) { targetX = sourceX * numTimes + indexX; targetY = sourceY * numTimes + indexY; targetPixel = targetPicture.getPixel(targetX, targetY); targetPixel.setColor(sourcePixel.getColor()); } } } } return targetPicture; } }
I get this message:Java Code:> String fileName = FileChooser.getMediaPath("rose.jpg"); > Picture p = new Picture(fileName); > p = p.scaleUp(2);
Static Error: No method in Picture has name 'scaleUp'
I'm a little confused because I thought I did create the scaleUp method within the Picture class.
- 11-16-2010, 10:04 AM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Because it is the class "ScaleUp" that has that method, not the class "Picture".
- 11-16-2010, 10:29 AM #10
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
Alright. Then can you please explain how I can get the program to actually spit out targetPicture?
- 11-16-2010, 10:35 AM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Uhm, add the scaleUp method to the Picture class?
Make the scaleUp method static and modify it to also take a "Picture" as a parameter?
- 11-16-2010, 10:54 AM #12
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
How would I go about modifying it to take a "Picture" as a parameter?
- 11-16-2010, 10:56 AM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
Main method
By vibaviattigala in forum New To JavaReplies: 2Last Post: 11-07-2010, 02:29 PM -
Calling The main method from another method
By SwissR in forum New To JavaReplies: 3Last Post: 07-27-2010, 11:03 AM -
Help with main method
By eliCanzee in forum JDBCReplies: 4Last Post: 01-06-2010, 09:12 AM -
calling method from main method
By bob_bee in forum New To JavaReplies: 4Last Post: 10-02-2009, 05:30 PM -
Main method here to :S:S hlp pls
By eliCanzee in forum AWT / SwingReplies: 6Last Post: 05-27-2009, 04:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks