Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Printing data from a method

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Printing data from a method

    I am trying to make a program that will get the area of a Triangle,Rectangle,Circle.

    The user picks which shape they want the area of and then inputs the Length/Width/Radius or whatever is needed. Im now stuck. I cannot get the data to print. I have made all the equations in the code. Here is the code.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Logan
     */
    import java.util.Scanner;
    public class area {
     
     
        public static void main(String[] args) 
        {
            //Declaring Variables.
            int triHeight, triBase, lengthRect, widthRect, shapeNum;
            double circRadi;
     
            Scanner reader = new Scanner (System.in);
     
            //Receiving Input
            System.out.println ("Which shape do you want area for?");
            System.out.println("1 Triangle 2 Circle 3 Rectangle 0 None");
            shapeNum = reader.nextInt ();
            if (shapeNum == 1) {
                System.out.println("Base of triangle?");
                triBase = reader.nextInt ();
                System.out.println("Height of triangle");
                triHeight = reader.nextInt ();
     
             }
            if (shapeNum == 2) {
                System.out.println("Radius of circle?");
                circRadi = reader.nextDouble ();
            }
            if (shapeNum == 3) {
                System.out.println ("Length of rectangle?");
                lengthRect = reader.nextInt ();
                System.out.println("Width of rectangle?");
                widthRect = reader.nextInt ();
     
            }
     
     
     
     
        }
        public static void areaTriangle(int triBase,int triHeight, int triOutput) {
            triOutput = (triBase*triHeight)/2;
     
        }
     
        public static void areaCircle (double circOutput,int circRadi ) {
            circOutput = (circRadi*Math.PI);
        }
     
        public static void areaRectangle(int rectOutput, int lengthRect, int widthRect) {
            rectOutput = lengthRect*widthRect;
        }
     
     
     
    }
    Last edited by LoganC; September 22nd, 2012 at 08:44 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Printing data from a method

    The code you posted doesn't have anything to do with the problem you asked about.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing data from a method

    Fixed it Norm

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Printing data from a method

    What is shown on the console when you execute the program? Please copy the full contents of the console and paste it here showing what was typed in and what was printed.

    Where does the code print out any of the results of the computations?

    Comments on the code:
    When you have a bunch of mutually exclusive choices (only one can be true)
    Use an if/else if/else chain for the tests. The ending else to catch an error if none of the above were true.

    Also look at using a switch() statement for this type of selection. Be sure to add a default: case to catch any error.



    Is this the same question: http://www.javaprogrammingforums.com...html#post76168
    Last edited by Norm; September 22nd, 2012 at 08:58 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing data from a method

    run:
    Which shape do you want area for?
    1 Triangle 2 Circle 3 Rectangle 0 None
    3
    Length of rectangle?
    4
    Width of rectangle?
    2
    BUILD SUCCESSFUL (total time: 5 seconds)

    that part works for each and every shape, so I should use chained conditions instead of 3 If statements? And I have only made it as far as making the equations. after the user inputs his info, the variables are declared and it computes in the methods areaTriangle, areaCircle and areaRectangle, but I need help on how to display those results.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Printing data from a method

    how to display those results.
    Use println() statements. You've done it in this thread:
    http://www.javaprogrammingforums.com...html#post76079
    Last edited by Norm; September 22nd, 2012 at 09:03 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing data from a method

    That was a code that my teacher made and I had to debug it. Would I print the results after the main in it's own method? Or in the areaTriangle areaCircle areaRectangle methods?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Printing data from a method

    To print the results in the main() method, the methods would have to return the results.
    What does the assignment say the program should do?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing data from a method

    The point of this exercise is to create methods that invoke other methods, create methods that
    take parameters and to practice using conditionals and keyboard input.
    Create a new project named Area.
    • Create three methods, called areaTriangle, areaCircle, and areaRectangle.
    a. Each method should take as parameters integers for the variables necessary to
    calculate the area of the respective figure.
    b. Each method should use mathematical operations to compute and print the area as
    a double.
    • Create a method called calcArea:
    a. prompt the user for the type of area to calculate (1 for Triangle, 2 for Circle, 3 for
    Rectangle, 0 for none of them). Use the Java scanner to read the input. If the user
    enters 0, return immediately. Otherwise….
    b. Use a conditional, and based on the type of area to be calculated, prompt the user
    for the dimensions of the object, then call the appropriate area method.
    • In your main method, simply call calcArea
    HINTS/SUGGESTIONS:
    • Don’t forget to import the Scanner! See Section 4.9.
    • Math.PI should be used for your area of the circle calculation.
    • For the area of the circle, the result needs to be a double (because of PI). Since the radius
    is an integer parameter, you will need to use typecasting when calculating the area (see
    Section 3.2, but remember to typecast to a double, not an int as the example there shows).

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Printing data from a method

    I didn't ask to see your assignment, I asked you if the assignment required how the code be written.
    You need to understand your assignment and if you have a question about your assignment, you should ask it.

    Here is the tutorial: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Printing data from a method

    New thread for same problem>
    http://www.javaprogrammingforums.com...itiliazed.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. BufferedReader hangs when handling POST method data
    By Sucker Punch in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2012, 04:33 PM
  2. Calling a print method from another class (printing array)
    By Kaldanis in forum Object Oriented Programming
    Replies: 7
    Last Post: November 25th, 2011, 01:32 PM
  3. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  4. [SOLVED] Printing Data in JAVA
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 15th, 2010, 01:22 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM