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.
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;
}
}
Re: Printing data from a method
The code you posted doesn't have anything to do with the problem you asked about.
Re: Printing data from a method
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
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.
Re: Printing data from a method
Quote:
how to display those results.
Use println() statements. You've done it in this thread:
http://www.javaprogrammingforums.com...html#post76079
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?
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?
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).
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)
Re: Printing data from a method