Question

Design and implement class Circle to represent a circle object. The class defines the following attributes...

Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods:

1.      A private variable of type double named radius to represent the radius. Set to 1.

2.      Constructor Methods:

•        Python : An overloaded constructor method to create a default circle.

•        C# & Java: Default Constructor with no arguments.

•        C# & Java: Constructor method that creates a circle with user-specified radius.

3.      Method getRadius() that returns the radius.

4.      Method setRadius() that sets the radius.

5.      Method getArea() that returns the area.

6.      Method getCircumference() that returns the circumference.

7.      Method To String,

•        Java: toString()

•        C#: ToString()

•        Python: __str__(self)

that prints out a meaningful description of the circle as follows:

The circle has radius X.     Where X is the value of variable radius.

Now design and implement a test program to create a default circle object and test all class methods on the object. Print the object after each method call and use meaningful label for each method call as shown in the following sample run.

Sample run:

Print radius:

The radius is 1.

Print area:

The area is 3.14

Print circumference:

The perimeter is 6.28

Set radius to 10 and print the object: The circle has radius 10.

Print area:

The area is 314.23

Print circumference:

The perimeter is 62.81

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the java code with attached output as per required in given question. Please refer the comments given above variabe and method to better understand.

=================== TestCircle.java ========================

public class TestCircle {

public static void main(String[] args) {

Circle circle = new Circle();

System.out.println("Print radius: ");

System.out.println("The radius is " + circle.getRadius());

System.out.println("Print Area: ");

System.out.println("The area is " + circle.getArea());

System.out.println("Print Circumference: ");

System.out.println("The circumference is " + circle.getCircumference());

System.out.println("Calling toString(): ");

// Calling toString() method

System.out.println(circle);

System.out.println();

System.out.println("************** Setting Radius to 10 ************");

// Setting radius to 10

circle.setRadius(10);

System.out.println("Print radius: ");

System.out.println("The radius is " + circle.getRadius());

System.out.println("Print Area: ");

System.out.println("The area is " + circle.getArea());

System.out.println("Print Circumference: ");

System.out.println("The circumference is " + circle.getCircumference());

System.out.println("Calling toString(): ");

// Calling toString() method

System.out.println(circle);

}

}

========================= Circle.java =========================

public class Circle {

private double radius;

// Default Constructor

public Circle() {

this.radius = 1;

}

// User-specified radius Constructor

public Circle(double radius) {

this.radius = radius;

}

// Method to get Radius

public double getRadius() {

return radius;

}

// Method to set Radius

public void setRadius(double radius) {

this.radius = radius;

}

// Method to calculate Area

public double getArea() {

return 3.14 * radius * radius;

}

// Method to calculate Circumference

public double getCircumference() {

return 2 * 3.14 * radius;

}

// toString method returning Radius, Area and Circumference collectively

@Override

public String toString() {

return "Radius " + " " + radius + ", Area " + getArea() + ", Circumference "+ getCircumference();

}

}

=================== Output =======================

Console X <terminated> TestCircle [Java Application] C:\Program FilesJavajre1.8.0_65\bin javaw.exe (31-Oct-2018 10:37:52 am) Print radius: The radius is 1.0 Print Area: The area is 3.14 Print Circumference: The circumference is 6.28 Calling toString() Radius 1.0, Area 3.14, Circumference 6.28 Print radius: The radius is 10.0 Print Area: The area is 314.0 Print Circumference: The circumference is 62.800000000000004 Calling toString() Radius 10.0, Area 314.0, Circumference 62.800000000000004

Add a comment
Know the answer?
Add Answer to:
Design and implement class Circle to represent a circle object. The class defines the following attributes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The...

    SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: A private variable of type double named radius to represent the radius. Set to 1. Constructor Methods: Python : An overloaded constructor method to create a default circle. C# & Java: Default Constructor with no arguments. C# & Java: Constructor method that creates a circle with user-specified radius. Method getRadius() that returns the radius. Method setRadius()...

  • Write a java class definition for a circle object. The object should be capable of setting...

    Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { }    //get method (Accessor Methods ) public double getRadius (...

  • Write a Java console application that prompts the user to enter the radius of a circle,...

    Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. Write a JavaFX GUI application to do the same calculation, and draw the circle. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Write and document your program per class coding conventions. Add an instance variable double radius. Generate...

  • ** IN JAVA **: Design and implement class Radio to represent a radio object. The class defines th...

    ** IN JAVA **: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods:Assume that the station and volume settings range from 1 to 10.A private variable of type int named station to represent a station number. Set toA private variable of type int named volume to represent the volume setting. Set to 1.A private variable of type boolean named on to represent the radio on or off. Set to false.A...

  • PYTHON Exercise #2: Design and implement class Radio to represent a radio object. The class defines...

    PYTHON Exercise #2: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods: Assume that the station and volume settings range from 1 to 10. 1. A private variable of type int named station to represent a station number. Set to 1. 2. A private variable of type int named volume to represent the volume setting. Set to 1. 3. A private variable of type boolean named on to represent the...

  • C++ Chapter 10 defined the class circleType to implement the basic properties of a circle. (Add t...

    C++ Chapter 10 defined the class circleType to implement the basic properties of a circle. (Add the function print to this class to output the radius, area, and circumference of a circle.) Now every cylinder has a base and height, where the base is a circle. Design a class cylinderType that can capture the properties of a cylinder and perform the usual operations on the cylinder. Derive this class from the class circleType designed in chapter 10.   Some of the...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • SOLVE IN PYTHON: Exercise #2: Design and implement class Radio to represent a radio object. The c...

    SOLVE IN PYTHON: Exercise #2: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods: Assume that the station and volume settings range from 1 to 10. A private variable of type int named station to represent a station number. Set to 1. A private variable of type int named volume to represent the volume setting. Set to 1. A private variable of type boolean named on to represent the radio...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Note- can you rewrite the code in C++ if there is any existing code. Can you...

    Note- can you rewrite the code in C++ if there is any existing code. Can you not use arrow -> operator. 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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT