The program below is the answer to Deitel’s Java How to Program (9th Edition) Chapter 2 Exercise 2.28.
Question: Write an application that inputs from the user the radius of a circle as an integer and prints the circle’s diameter, circumference and area using the floating-point value 3.14159 for π. Use the techniques shown in Fig. 2.7. [Note: You may also use the predefined constant Math.PI for the value of π. Use the following formulas (r is the radius):
diameter = 2r
circumference = 2πr
area = πr2
Do not store the results of each calculation in a variable. Rather, specify each calculation as the value that will be output in a System.out.printf statement. The values produced by the circumference and area calculations are floating-point numbers. Such values can be output with the format specifier %f in a System.out.printf statement.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.28 - Diameter, Circumference and Area of a Circle<br /> * This Program Calculates The Diameter, Circumference and Area of a Circle<br /> *<br /> */ <br /><br />import java.util.Scanner;<br /><br />public class Ex02_28 {<br /> public static void main (String [] args) {<br /> <br /> Scanner value = new Scanner (System.in);<br /> <br /> int radius;<br /> <br /> System.out.println ("This Application n");<br /> <br /> System.out.print ("Enter The Value For The Radius of The Circle: ");<br /> radius = value.nextInt();<br /> <br /> System.out.printf("nThe diameter of the circle is %dnThe circumference "<br /> + "of the circle is %fnThe Area of the circle is %fn",<br /> (2*radius), (2*Math.PI*radius), (Math.PI*radius*radius));<br /><br /> }<br />}<br /> |