Results 1 to 12 of 12
  1. #1
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Exclamation [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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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:
    Java Code:
    public static int area (int lengthInteger,int widthInteger)
    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.

  3. #3
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Default still confused

    Quote Originally Posted by Fubarable View Post
    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:
    Java Code:
    public static int area (int lengthInteger,int widthInteger)
    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.
    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;
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    //so should i get rid of the areaInteger completely?
    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.

  5. #5
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Default

    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;
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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]

  7. #7
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Default

    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;
        }

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Don't use "==" for strings

    When comparing strings, don't use "==". Use the string methods .equals(), .comparesTo(), etc.
    String (Java Platform SE 6)
    Java Code:
    if (answer.equals("area"))
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Default

    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

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Post your final working program

    Remember to post your final working program so others can learn from your experience.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    ... and also mark the question as "Solved". congrats on finding a solution.

  12. #12
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Smile

    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

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 5
    Last Post: 07-18-2008, 04:30 PM
  3. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  4. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  5. Replies: 0
    Last Post: 11-30-2007, 11:55 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
  •