Java Programming

Build 3 methods(no arguments) that will do the work (input, calculations and output) for each of the 3 different figures. Then in the 3 “if” statements (or Switch stmt) that you have to determine which figure was selected, you should now only have to call the methods, which will then do the input, calculation and print the results. (Remember, you will need to use “static” for each of the methods that you build, since you will be calling them from the “main” method, which is also static.)

I'm trying to Modify my code so it would work for the question above but I'm to figure out how to do that. Does anyone have suggestions?

Here my code:
import java.util.Scanner;
public class Area {

public static void main(String args[]) {
Scanner input = new Scanner(System.in); // input scanner

// calculated the area of the circle, square, and rectangle
String shape; // shape string
double radius, area, side, height, width;
System.out.print("Choose these letters C, S, or R:");
shape = input.nextLine();

// executable code
if (shape.equals("C")) {
System.out.println("Radius:");
radius = input.nextDouble();
area = 3.14 * radius * radius;
System.out.println("The area of the circle is " + area);
}
else {
if (shape.equals("S"))
{
System.out.println("Side:");
side = input.nextDouble();
area = side * side;
System.out.println("The area of the square is " + area);
}
else {
if (shape.equals("R"))
{
System.out.println("Height:");
height = input.nextDouble();
System.out.println("Width:");
width = input.nextDouble();
area = height * width;
System.out.println("The area of the rectangle is " + area);
}
else{
System.out.println("Invalid");
}
}
}
} //end main()
} // end class

To modify your code to use three separate methods for input, calculation, and output, you can follow these steps:

1. Declare three separate methods: `inputShape()`, `calculateArea()`, and `outputResult()`.

2. Modify your main method to call these three methods instead of directly calculating and printing the results.

Here's the modified code:

```java
import java.util.Scanner;

public class Area {
public static void main(String args[]) {
Scanner input = new Scanner(System.in); // input scanner

// declare variables
String shape;
double radius, side, height, width;

// get shape input
shape = inputShape(input);

// calculate area based on shape input
double area = calculateArea(shape, input);

// output result
outputResult(shape, area);
}

// method for shape input
public static String inputShape(Scanner input) {
System.out.print("Choose these letters C, S, or R:");
return input.nextLine();
}

// method for calculating the area based on shape input
public static double calculateArea(String shape, Scanner input) {
if (shape.equals("C")) {
System.out.println("Radius:");
double radius = input.nextDouble();
return 3.14 * radius * radius;
} else if (shape.equals("S")) {
System.out.println("Side:");
double side = input.nextDouble();
return side * side;
} else if (shape.equals("R")) {
System.out.println("Height:");
double height = input.nextDouble();
System.out.println("Width:");
double width = input.nextDouble();
return height * width;
} else {
return -1; // signifies an invalid shape input
}
}

// method for outputting the result
public static void outputResult(String shape, double area) {
if (area != -1) {
System.out.println("The area of the " + shape + " is " + area);
} else {
System.out.println("Invalid shape input");
}
}
}
```

Now, when you run the program, it will call the `inputShape()` method to get the shape input, the `calculateArea()` method to perform the calculation, and the `outputResult()` method to print the result. This way, each method is responsible for a specific task, making the code easier to understand and maintain.