Question

Create a Circle class and initialize it with a radius. (any number you like) Make one...

Create a Circle class and initialize it with a radius. (any number you like)

Make one method that prints out a squared value of the radius.

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

Java Program:

import java.util.*;

class Circle
{
double radius;

Circle(double radius)
{
this.radius=radius;
}

void squaredValue()
{
System.out.printf("\nSquared value of the radius is %.2f\n\n",(radius*radius));
}

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter a radius: ");
double radius=sc.nextDouble();

Circle circle=new Circle(radius);
circle.squaredValue();
}
}

Output:

Windows PowerShell - OX PS C:\Users\user\Desktop> javac circle.java PS C:\Users\user\Desktop> java circle squared value of th

C++ PROGRAM :


#include <iostream>

using namespace std;


class Circle
{
public:

double radius;

Circle(double rad)
{
radius=rad;
}

void squaredValue()
{
cout<<"\nSquared value of the radius is "<<(radius*radius)<<endl;
}
};

int main()
{
double radius;
cout<<"Enter a radius: ";
cin>>radius;
  
Circle circle(radius);
circle.squaredValue();
  
return 0;
}

Output:

Enter a radius: 2.50 ... Program finished with exit code 0 Press ENTER to exit console. I

Add a comment
Know the answer?
Add Answer to:
Create a Circle class and initialize it with a radius. (any number you like) Make one...
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
  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

  • PYTHON Create a Student class and initialize it with name and roll number. Make methods to...

    PYTHON Create a Student class and initialize it with name and roll number. Make methods to : 1. Display - It should display all informations of the student. 2. setAge - It should assign age to student 3. setMarks - It should assign marks to the student.

  • 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.     ...

  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

  • class Circle { private double radius; public Circle(double radius) { radius = radius; } } public...

    class Circle { private double radius; public Circle(double radius) { radius = radius; } } public Circle(double radius) { radius = radius; } } The program does not compile because Circle does not have a default constructor. The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0. The program has a compilation error because it does not have a main method. The program has a compile error...

  • Programming Language is Java 1. Create a class to describe a car. Include fields like: make,...

    Programming Language is Java 1. Create a class to describe a car. Include fields like: make, model, year and color. Add all the accessors and mutators for these fields. Add a constructor that can initialize the fields. 2. Create a class that will enable you to handle standard mail communication with a customer. Add all accessors and mutators, as well as a constructor. Add a method named fullAddress that will return the address formatted for a standard mail. Add a...

  • Python please. thanks Write a class named Circle that holds the radius of a circle in...

    Python please. thanks Write a class named Circle that holds the radius of a circle in a private variable. As part of it's public interface, include a an initializer method that initializes the radius to 0. Include an accessor and mutator for setting and getting the radius. Finally, include a method for returning the area of the circle. The area of a circle is Pl times radius squared. Use 3.14159 for Pl.

  • Write a Python class, Circle, constructed by a radius and two methods which will compute the...

    Write a Python class, Circle, constructed by a radius and two methods which will compute the area and the perimeter of a circle. Include the constructor and other required methods to set and get class attributes. Create instances of Circle and test all the associated methods. Write a Python class, Rectangle, constructed by length and width and a method which will compute the area of a rectangle. Include the constructor and other required methods to set and get class attributes....

  • 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 (...

  • Create a Ruby class Cylinder. Each cylinder is characterized by the instance variables radius and height. For this class...

    Create a Ruby class Cylinder. Each cylinder is characterized by the instance variables radius and height. For this class the initializer/constructor and the method volume that returns the volume of the cylinder. For this class the initializer/constructor and the method area that returns the area of cylinder. Create a class Can that inherits properties from the class Cylinder and adds new instance variablescontents(e.g. beer, Pepsi, Coke) and price(real number). Initialize an array of cans and write Ruby code necessary to...

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