Program 1: Write a Java application to calculate thecircumference and the area o
ID: 3613737 • Letter: P
Question
Program 1: Write a Java application to calculate thecircumference and the area of a circle. Your application must havetwo classes. The Circle class will have two methods: thecircumference method and the area method. Each method will take adouble input parameter as the value of the radius, perform itscalculation, and return a double result.
TheCircleTest class will have a main method that uses aJOptionPane to get the value of the radius fromthe user. The main method will create an object of the Circle classand test its methods. The main method will also use aJOptionPane for output. Use Math.PI and theMath.pow method in your calculations.
Limit your output to three digits after thedecimal point by using the String.formatmethod. Add exception handling to your program so that, if the userinputs something other than a string of digits, your programcatches the exception and gives the user another chance to inputthe value of the radius.
Explanation / Answer
Your Circle class should be independent ofinput. publicclass Circle { publicstatic double calcCircumference(double radius) { return 2*radius*Math.PI; } publicstatic double calcArea(double radius) { return Math.pow(2,radius)*Math.PI; } } Now in your main method ask for input. public classCircleTest { publicstatic void main(String[]args) { //get radius double radius =0.0; do { try { radius =Double.parseDouble(JOptionPane.showInputDialog("Input the radius: ")); } catch(NumberFormatException ex){} } while(true); } System.out.println("Circumference: "+String.format("%.3f",Circle.calcCircumference(radius))); System.out.println("Area:"+String.format("%.3f",Circle.calcArea(radius))); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.