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

PLEASE HELP! Part 3: calculate diameter ,perimeter and area of the circle based

ID: 671356 • Letter: P

Question

PLEASE HELP!

Part 3: calculate diameter ,perimeter and area of the circle based on the radius input given by the user. However, the functions to calculate diameter, area and perimeter arementioned in Circle class instead of main method

This is the outline for the program

TIME SPENT: how long it took you to complete the assignment I Step 2: Declaring Class Examining the problem, we need to create a Circle class, declare circle class as follows class Circle [ Inside the class declare a double variable radius. In order to calculate perimeter and area we need value Declare a variable as follows which has value private double radius; public final double PI=3.14 Define a constructor for the class which takes the default value as 1 public Circle ) radius-1; Step 3: Defining methods to calculate the diameter, perimeter and area of a circle In order to initialize value to the radius variable we need to declare a method called setValue because radius is declared as private and it can be used only by its class members public void setValue (double value) radius=value ; To calculate the diameter of the circle define a method with following syntax and calculate the diameter inside the method public double diameter () /* write the code which calculates diameter of the circle and returns the value to the main method*/ To calculate the perimeter of the circle declare a method as follows public double perimeter () /* write the code which calculates perimeter of the circle and returns the value to the main method*/ To calculate the area of the circle declare a method as follows public double area () /*write the code which calculates area of the circle and returns to the main method*/ Step4: Initializing runner method In order to use class variables and methods we need to have a runner function For any class in java "main" will be running method. In your program you already have Labá5 class inside the file, in that start main method

Explanation / Answer

// The Circle class
public class Circle
{
// variable to contain the radius of the circle
private double radius;
public final double PI=3.14;
  
// Constructor code
public Circle()
{
radius = 1;
}
  
// code to set the value of radius
public void setValue(double value)
{
radius = value;
}
  
/* Code which calculates diameter (diameter = 2 x radius) and returns the value */
public double diameter()
{
return 2 * radius;
}
  
/* Code which calculates perimeter (perimeter = 2 x PI x radius) and returns the value */
public double perimeter()
{
return 2 * PI * radius;
}
  
/* Code which calculates area (Area = PI x radius x radius) and returns the value */
public double area()
{
return PI * radius * radius;
}
}

import java.text.DecimalFormat;
import java.util.Scanner;

public class Lab5 {

public static void main(String[] args) {
/* declare Scanner class, Circle class and DecimalFormat class objects */
Scanner scanner = new Scanner(System.in);

Circle circle = new Circle();

DecimalFormat decimal = new DecimalFormat("0.000");


/* Prompt the user to enter the radius of the circle */
System.out.println("Enter the radius of the circle");

/* declaring a variable to store the input entered by the user */
/* Take the input from the user and call setValue method of circle class */
double radius = scanner.nextDouble();
circle.setValue(radius);
/* variable declared to store and display the result of various calculations*/
double result;
/* call diameter method from circle class*/
result = circle.diameter();
/* print the diameter which has only 3 digits after decimal point */
System.out.println("Diameter of the Circle: " + decimal.format(result));

/* call perimeter method from circle class*/
result = circle.perimeter();
/* print the perimeter which has only 3 digits after decimal point */
System.out.println("Perimeter of the Circle: " + decimal.format(result));

/* call area method from circle class*/
result = circle.area();
/* print the area which has only 3 digits after decimal point */
System.out.println("Area of the Circle: " + decimal.format(result));

}
}

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