Write a Java application to calculate the circumference and the area of a circle
ID: 3640995 • Letter: W
Question
Write a Java application to calculate the circumference and the area of a circle. Your application must have two classes. The Circle class will have two methods: the circumference method and the area method. Each method will take a double input parameter as the value of the radius, perform its calculation, and return a double result.The CircleTest class will have a main method that uses a JOptionPane to get the value of the radius from the user. The main method will create an object of the Circle class and test its methods. The main method will also use a JOptionPane for output. Use Math.PI and the Math.pow method in your calculations.
Limit your output to three digits after the decimal point by using the String.format method. Add exception handling to your program so that, if the user inputs something other than a string of digits, your program catches the exception and gives the user another chance to input the value of the radius.
Explanation / Answer
import java.text.DecimalFormat; import java.util.Scanner; import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { boolean success = false; while(!success){ try{ String arg = JOptionPane.showInputDialog("Enter radius"); Double radius = Double.parseDouble(arg); Circle c = new Circle(radius); DecimalFormat format = new DecimalFormat("0.000"); JOptionPane.showMessageDialog(null, "Circumference - " + format.format(c.getCircumference())); JOptionPane.showMessageDialog(null, "Area - " + format.format(c.getArea())); success = true; } catch(Exception e){ JOptionPane.showMessageDialog(null, "You entered wrong radius...", "Error", JOptionPane.ERROR_MESSAGE); } } } } class Circle{ private double radius; public Circle(double radius) { this.radius = radius; } public double getCircumference(){ return 2 * Math.PI * radius; } public double getArea(){ return Math.PI * Math.pow(radius, 2); } } Pls rate
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.