Question
import java.util.Scanner; public class Area { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print(Enter code(C for circle, R for rectangle, S
import java.util.Scanner;
public class Area {
public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0);
if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') { System.out.print("Enter width: "); double width = in.nextDouble(); System.out.print("Enter height: "); double height = in.nextDouble(); System.out.println("Area of rectangle is " + (width * height)); } else { System.out.print("Enter length of side: "); double side = in.nextDouble(); System.out.println("Area of square is " + (side * side)); } } }
Modify this program by building 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.)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started