Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java program... One of the problems that have been discussed in the class is to

ID: 3687278 • Letter: J

Question

Java program...

One of the problems that have been discussed in the class is to write a simple Java program to determine the area and circumference of a circle of a given radius. In this lab you will be rewriting the program again but this time you will be writing some functions to take care of the user input and math. Specifically, your main function should look like the following:

public class circle{

Your job will be to write these three functions:

1)getRadius

This function should ask the user to enter the value for the radius and return that value.

2)findArea

This function should take the radius as a parameter and calculate and return the area.

(Hint: area = 3.14159*radius*radius)

3)findCircumference

This function should take the radius as a parameter and calculate and return the circumference.

(Hint: circumference = 2*3.14159*radius)

A sample run of your program should look like this (bold bracketed text stands for user input):

Enter the radius of the circle: [3.5]

A circle of radius 3.5 has an area of: 38.4845

and a circumference of: 21.9911

Press any key to continue . . .

Explanation / Answer

Java application that inputs from the user the radius of a circle and prints the circle diameter, circumference and area.

The formulas for calculating the diameter, area and circumference of circle are given below:

Diameter = 2 * Radius

Area = * Radius * Radius

Circumference = 2 * * Radius

We can use the floating-point value 3.1416 for but in this application we use the predefined constant Math.PI.

Circumference = 2 * Math.PI * Radius;

This is the code for this program:

//diameter, area and circumference of circle

import java.util.Scanner;

public class JavaApplication358 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        double radius, diameter, circumference, area;

        System.out.print("Radius: ");

        radius = sc.nextDouble();

        diameter = 2 * radius;

        System.out.println("Diameter: " + diameter);

        circumference = 2 * Math.PI * radius;

        System.out.println("Circumference: " + circumference);

        area = Math.PI * radius * radius;

        System.out.println("Area: " + area);

    }

}

If the radius value is 2,5 then the program output resulting from executing this program is:

Radius: 2,5

Diameter: 5.0

Circumference: 15.707963267948966

Area: 19.634954084936208

We can use the printf method to display the result with only two decimal places instead of the default decimal places.

This is the statement to display the diameter with two decimal places:

System.out.printf("Diameter: %.2f " , diameter);

% indicates that a value will be displayed here, in this case, diameter.

.2 indicates the decimal places to display

f indicates a floating-point value

This is the code using System.out.printf:

//diameter, area and circumference of circle using System.out.printf

import java.util.Scanner;

public class JavaApplication358 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        double radius, diameter, circumference, area;

        System.out.print("Radius: ");

        radius = sc.nextDouble();

        diameter = 2 * radius;

        System.out.printf("Diameter: %.2f " , diameter);

        circumference = 2 * Math.PI * radius;

        System.out.printf("Circumference: %.2f " , circumference);

        area = Math.PI * radius * radius;

        System.out.printf("Area: %.2f " , area);

    }

}

If the radius value is 2,5 now the program output resulting from executing this program is:

Radius: 2,5

Diameter: 5,00

Circumference: 15,71

Area: 19,63

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote