Question

In: Computer Science

Circle Class Write a Circle class that has the following member variables: • radius: a double...

Circle Class

Write a Circle class that has the following member variables:
• radius: a double
• pi: a double initialized with the value 3.14159

The class should have the following member functions:

• Default Constructor. A default constructor that sets radius to 0.0.
• Constructor. Accepts the radius of the circle as an argument.
• setRadius. A mutator function for the radius variable.
• getRadius. An accessor function for the radius variable.
• getArea. Returns the area of the circle, which is calculated as area = pi * radius * radius
• getDiameter. Returns the diameter of the circle, which is calculated as diameter = radius * 2
• getCircumference. Returns the circumference of the circle, which is calculated as circumference = 2 * pi * radius

Write a program that demonstrates the Circle class by asking the user for the circle’s radius, creating a Circle object, and then reporting the circle’s area, diameter, and circumference.

SAMPLE RUN #0: ./circle_Non-Interactive

Interactive Session Standard Error (empty) Standard Output Hide Invisibles

Highlight: NoneStandard Input OnlyPrompts OnlyStandard Output w/o PromptsFull Standard OutputAllShow Highlighted Only

Calling·default·constructor:·The·circle's·radius·in·the·default·Circle·Object·is:·0↵
↵
Calling·setRadius(20)·to·change·radius·of·default:↵
The·circle's·radius·in·the·default·Circle·Object·is·Now:·20↵
↵
Creating·Circle·circle2(10):·↵
The·circle2's·radius·in·the·circle2·Object·is:·10↵
↵
The·circle2's·area·is·therefore:·314.159↵
The·circle2's·diameter·is·therefore:·20↵
The·circle2's·circumference·is·therefore:·62.8318↵

out put must be like sample run. HELP PLEASE

Solutions

Expert Solution

Java Program

import java.util.Scanner;   //importing Scanner utility class

public class Main   //Main class with main method for testing Circle class
{
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);    
            
            System.out.print("Enter radius: "); //getting raidus input
            double radius = sc.nextDouble();    
            
            System.out.println("Calling·default·constructor:·The·circle's·radius·in·the·default·Circle·Object·is:·0\n");
            Circle circle = new Circle();   //creating a circle object by default constructor
            System.out.println("Calling·setRadius("+radius+")·to·change·radius·of·default:");
            circle.setRadius(radius);   //setting circle raidus to user inputted radius
            System.out.println("The·circle's·radius·in·the·default·Circle·Object·is·Now:·"+circle.radius+"\n");
            
            System.out.println("Creating·Circle·circle2(10):·");
            Circle circle2 = new Circle(10);    //creating circle2 by parametrized constructor 
            System.out.println("The·circle2's·radius·in·the·circle2·Object·is:·"+circle2.radius+"\n");
            
            System.out.println("The·circle2's·area·is·therefore:·"+circle2.getArea());
            System.out.println("The·circle2's·diameter·is·therefore:·"+circle2.getDiameter());
            System.out.println("The·circle2's·circumference·is·therefore:·"+circle2.getCircumference());
        }
}

class Circle    //Circle class definition
{
    public static double pi;    //class variable
    public double radius;       //object variable
    
    static      //static block executed only once when the class is loaded for the first time
    {
        pi = 3.14159;   //initializing static variables
    }
    Circle()    //default constructor called everytime an object is created
    {
        radius = 0.0;
    }
    Circle(double r)    //parametrized constructor
    {
        radius = r;
    }
    public void setRadius(double r) //setter method
    {
        radius = r;
    }
    public double getRadius()   //getter method
    {
        return radius;
    }
    public double getArea()     //computes and returns area of circle 
    {
        return pi*radius*radius;
    }
    public double getDiameter() //computes and returns circle diameter
    {
        return 2*radius;
    }
    public double getCircumference()    //computes and returns circle circumference
    {
        return 2*pi*radius;
    }
}

Program Screenshot

Output

Each and everything is explained clearly in the comment section of the code.

Thank you! Hit like, if you like my work.


Related Solutions

Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Write a Circle class that has the following member variables: radius as a double PI as...
Write a Circle class that has the following member variables: radius as a double PI as a double initialized with 3.14159 The class should have the following member functions: Default constructor Sets the radius as 0.0 and pi as 3.14159 Constructor Accepts the radius of the circles as an argument setRadius A mutator getRadius An accessor getArea Returns the area of the circle getDiameter Returns the diameter of the circle getCircumference Returns the circumference of the circle Write a program...
Write a Circle Class that has the following fields: • radius: a double • PI: a...
Write a Circle Class that has the following fields: • radius: a double • PI: a final double initialized with the value 3.14159 • Constructor. Accepts the radius of the circle as an argument • Constructor. A no-arg constructor that sets the radius field to 0.0. • setRadius. A mutator method for the radius field. • getRadius. An accessor method for the radius field. • getArea. Returns the area of the circle, which is calculated as area = PI *...
Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the...
Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the Circle class, you should test this class from the main() method by passing objects of this class to a method “ public static void printAreas(Circle c, int times)” You should display the area of the circle object 5 times with different radii. 2 java.util.Random +Random() +Random(seed: long) +nextInt(): int +nextInt(n: int): int +nextLong(): long +nextDouble(): double +nextFloat(): float +nextBoolean(): boolean Constructs a Random object...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and complexNum of type double. Your class shall have following member functions Complex(); Complex(double, double); Complex add(Complex num1, Complex num2); Complex subtract(Complex num1, Complex num2); string printComplexNumber(); Write a driver program to test your code. Please make sure your code is separated into Complex.h Containing definition of the class Complex.cpp Containing the implementation ComplexTestProgram.cpp Containing main program Use the following directive in class definition to...
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this...
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this radius, the circumference of the circle, the area of the circle, the surface area of a sphere with that radius and the area of a sphere with that radius. Each of its computation functions returns its answers in call by reference parameters. Here are the formulas for computation: Circumference = 2 • π • r Circle Area = π • r2 Sphere Surface Area...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the class are: NUM_EMPLOYEES: a constant integer variable to hold the number of employees. employeeId. An array of integers to hold employee identification numbers. hours. An array of integers to hold the number of hours worked by each employee payRate. An array of doubles to hold each employee’s hourly pay rate wages. An array of seven doubles to hold each employee’s gross wages The class...
write the code in python Design a class named PersonData with the following member variables: lastName...
write the code in python Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool ....
Create an object called Circle. Declare the following integer variables for the object Circle, radius, diameter,...
Create an object called Circle. Declare the following integer variables for the object Circle, radius, diameter, and pi is declared as double. Create the following for the Circle object: ● Implicit constructor (default constructor) ● Void method Calculate (double pi, int radius) to calculate the area of the Circle object. The method must include a system.out statement that displays the area value. Use the following formula to calculate area of the circle: Area = pi * (r * r) Your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT