Results 1 to 12 of 12
- 12-07-2008, 10:25 PM #1
Member
- Join Date
- Dec 2008
- Location
- North America
- Posts
- 29
- Rep Power
- 0
[SOLVED] Beginner, need help with Returning Values from Methods
I have to create a program in my grade 11 first year of programming with java class. It must use returning values from methods. The program must ask the user for a length and width and then ask the user if they want the perimeter calculated or the area calculate. SO far i only started the area method, and im not sure how to transfer both int length and width to my method called 'area'. Any help would be much appreciated.:)
// The "Method" class.
import java.awt.*;
import hsa.Console;
public class Method
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
// Place your program here. 'c' is the output console
int length, width, perimeter, areaAnswer;
String answer;
c.print ("Please enter the length: ");
length = c.readInt ();
c.print ("Please enter the width: ");
width = c.readInt ();
c.print ("Would you like to determine the perimeter or area? P/A: ");
answer = c.readLine ();
if (answer == "A")
{
areaAnswer = area (length,width);
c.println ("The area is: " + areaAnswer);
}
perimeter = (length * 2) + (width * 2);
} // main method
public static int area (int lengthInteger,int widthInteger, int areaInteger)
{
areaInteger = lengthInteger*widthInteger;
return areaInteger;
}
} // Method class
-
Your area method doesn't need an "areaInteger" parameter as this is not needed by the method but instead is what the method will return. If you look at your method signature there should be three ints:
the first int, right after the static is the type of the variable that will be returned -- the area. The second two ints that are in the parenthesis are the types of the values that are passed into the method. Make sense? If not, please say so, and good luck.Java Code:public static int area (int lengthInteger,int widthInteger)
- 12-07-2008, 10:53 PM #3
Member
- Join Date
- Dec 2008
- Location
- North America
- Posts
- 29
- Rep Power
- 0
still confused
No, sorry i still dont understand it:
if (answer == "A")
{
areaAnswer = area (length,width); //here im trying to send the length and width variable to the area method, is this correct?
c.println ("The area is: " + areaAnswer); //if person wants area, it will be printed
}
perimeter = (length * 2) + (width * 2);
} // main method
public static int area (int lengthInteger,int widthInteger, int areaInteger) //so should i get rid of the areaInteger completely?
{
areaInteger = lengthInteger*widthInteger;
return areaInteger;
}
-
Yep. At least get rid of it in the parameter. In the body of the method, first line, stick an "int" keyword in front of areaInteger so that you declare it there as an int. Try it and see if it works. Again, g/l.//so should i get rid of the areaInteger completely?
- 12-07-2008, 11:02 PM #5
Member
- Join Date
- Dec 2008
- Location
- North America
- Posts
- 29
- Rep Power
- 0
My program runs but after asking for area or perimeter and i type area, i program ends without showing anything at all.
c.print ("Would you like to determine the perimeter or area? : ");
answer = c.readLine ();
if (answer == "area") //im not sure if this line is working
{
areaAnswer = area (length,width);
c.println ("The area is: " + areaAnswer);
But i understand how the method works i think:
public static int area (int lengthInteger,int widthInteger)
{
int areaInteger = lengthInteger*widthInteger;
return areaInteger;
}
-
Hm, you've got a bug and you may do well to repost your entire program.
Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:
Java Code:[code] // your code block goes here. // note the differences between the tag at the top vs the bottom. [/code]
- 12-07-2008, 11:09 PM #7
Member
- Join Date
- Dec 2008
- Location
- North America
- Posts
- 29
- Rep Power
- 0
Java Code:public static void main (String[] args) { c = new Console (); // Place your program here. 'c' is the output console int length, width, perimeter, areaAnswer; String answer; c.print ("Please enter the length: "); length = c.readInt (); c.print ("Please enter the width: "); width = c.readInt (); c.print ("Would you like to determine the perimeter or area? : "); answer = c.readLine (); if (answer == "area") { areaAnswer = area (length,width); c.println ("The area is: " + areaAnswer); } perimeter = (length * 2) + (width * 2); } // main method public static int area (int lengthInteger,int widthInteger) { int areaInteger = lengthInteger*widthInteger; return areaInteger; }
- 12-07-2008, 11:11 PM #8
Don't use "==" for strings
When comparing strings, don't use "==". Use the string methods .equals(), .comparesTo(), etc.
String (Java Platform SE 6)
Luck,Java Code:if (answer.equals("area"))
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-07-2008, 11:16 PM #9
Member
- Join Date
- Dec 2008
- Location
- North America
- Posts
- 29
- Rep Power
- 0
Thank you all very much for the help, i understand the concept and now i can finish the perimeter method of the program..im new to this forum, and i already think its great to get such good help and so quickly:) thanks again
- 12-08-2008, 12:08 AM #10
Post your final working program
Remember to post your final working program so others can learn from your experience.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
-
... and also mark the question as "Solved". congrats on finding a solution.
- 12-08-2008, 12:29 AM #12
Member
- Join Date
- Dec 2008
- Location
- North America
- Posts
- 29
- Rep Power
- 0
Ok thank you...heres my final program:
Java Code:// The "Act3Num2" class. import java.awt.*; import hsa.Console; public class Act3Num2 { static Console c; // The output console public static void main (String[] args) { c = new Console (); // Place your program here. 'c' is the output console int length, width, areaAnswer, perimeterAnswer; String answer; c.println ("Finding Perimeter or Area: "); c.print ("Please enter the length: "); length = c.readInt (); c.print ("Please enter the width: "); width = c.readInt (); c.print ("Would you like to determine the perimeter or area? : "); answer = c.readLine (); if (answer.equals ("area")) { areaAnswer = area (length, width); c.println ("The area is: " + areaAnswer); } else if (answer.equals ("perimeter")) { perimeterAnswer = perimeter (length, width); c.println ("The perimeter is: " + perimeterAnswer); } } // main method public static int area (int lengthInteger, int widthInteger) { int someInteger = lengthInteger * widthInteger; return someInteger; } public static int perimeter (int lengthInteger, int widthInteger) { int someInteger = (lengthInteger * 2) + (widthInteger * 2); return someInteger; } } // Act3Num2 class
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
text box listeners and returning multiple strings from methods
By int80 in forum New To JavaReplies: 5Last Post: 07-18-2008, 04:30 PM -
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 10:53 AM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM -
How to add interceptor to persistent ejb entity to add check before returning values
By umen in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 11-30-2007, 11:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks