Rivers31334
Rivers31334

Reputation: 654

Getting the correct print output from return methods

I am trying to write a small bit of code that outputs min of three numbers, max of three numbers, and surface areas of a cylinder when given r and h.

I have written the code, compiled it, and it works exactly the way I want it to. However, with the actual print output, I want it to be a bit better. I am having trouble with this.

In looking at the code below, the current output reads as: 3 56.8 The surface area of a cylinder with a radius 3.0 and a height 4.5 is 141.3716694115407

I want to model the actual print output after the cylinder method (ie "The minimum of 5, 7, and 3 is 3" and "The maximum of ......"). It's easy for the cylinder portion as my print statment is in the method...the method itself is not a return type.

But the the findMin and findMax methods that are return types, can anyone offer me advice or hints on how to get the code to actually output more than just the minimum value or the maximum value? I tried actually playing within the actual println statements under the main method, but keep getting errors.

Much thanks...from a beginner

public class Test {

public static void main(String[] args) {
    System.out.println(findMin(5, 7, 3));
    System.out.println(findMax(-5.1, 32.5, 56.8));
    cylinderSurfaceArea(3.0, 4.5);  
}   

public static int findMin(int a, int b, int c) {
    int minimum = Math.min(a, b);
    int minimum2 = Math.min(minimum, c);
    return minimum2;
}   

public static double findMax(double a, double b, double c) {
    double maximum = Math.max(a, b);
    double maximum2 = Math.max(maximum, c); 
    return maximum2;
}

public static void cylinderSurfaceArea(double a, double b) {
    double answer = 2 * Math.PI * (a * a) + 2 * Math.PI * a * b;
    System.out.println("The surface area of a cylinder with radius " + a + " and height " + b + " is " + answer); 
}

}

Upvotes: 0

Views: 95

Answers (1)

Christian Tapia
Christian Tapia

Reputation: 34166

One way is to put the print statement inside the method (which I won't recommend):

public static int findMin(int a, int b, int c)
{
    int minimum = Math.min(a, b);
    int minimum2 = Math.min(minimum, c);
    System.out.println("The minimum of " + a + ", " + b + " and " + c + " is " + minimum2);
    return minimum2;
}

and another is to store the variables you are going to find the minimum of:

int a = 5, b = 7, c = 3;

and have a print statement in the main() method:

System.out.println("The minimum of " + a + ", " + b + " and " + c + " is " + 

Note:

You may find interesting printf, which can be used in a more flexible way:

System.out.printf("The minimum of %d, %d and %d is %d.", a, b, c, minimum2);
System.out.printf("The minimum of %d, %d and %d is %d.", a, b, c, findMin(5, 7, 3));

Upvotes: 1

Related Questions