Question

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 ( ) {

return radius;

}

//set method (Mutator Methods)

public void setRadius (double r) {

radius=r;

}

public double findArea ( ) {

return PI*radius*radius; //area= pi r squared

}

public double findCircumference ( ) {

return 2 * PI * radius;

}

}//end Circle

Now you can write a tester program called CircleDemo.java to create and use Circle objects. Compile and run it.

Warning! If you cut and paste code from outside your IDE, it may result in errors – in particular, check that double quotes and hyphens are written correctly.

public class CircleDemo{

public static void main(String[] args){

Circle cir1, cir2;

cir1 = new Circle();

cir2 = new Circle();

cir1.setRadius(3.0);

cir2.setRadius(11.01);

System.out.println("Area of the first circle: " + cir1.findArea());

System.out.println("Circumference of the first circle: " + cir1.findCircumference());

System.out.println("Area of the second circle: " +cir2.findArea());

System.out.println("Circumference of the second circle: " +cir2.findCircumference());

}

}

1 (b) Now make the following changes:

In the Circle class, use the keyword this in the setRadius method

In the Circle class, add a String attribute called colour that represents the colour of the circle

In the Circle class, add the methods getColour and setColour. Use the keyword this in the new mutator method.

In the Circle class, add another constructor that accepts both the radius and the colour and sets both attributes accordingly

In the Circle class, add a toString method to print the radius, the area of the circle, and colour like this:

Radius: 10 Area: -- Colour: Red

Modify the CircleDemo program to read user given radius and colour from the keyboard, create the Circle object and print its radius, area and colour (using your toString method) and the circumference (using the findCircumference method):

Enter the radius: 10

Radius: 10 Area: -- Colour: Red

Circumference: ---

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

Below contains the modified code for Circle as well as CircleDemo class.
The new requirements are marked in the code as "Requirement [1-6]" to separately highlight these new additions to the code.

Circle class:

public class Circle{

       //Instance Variables

       private double PI = 3.1459;

       private double radius;

       private String colour; //Requirement 2

       //Default Constructor
       public Circle ( ) { }
     
     
       //Modified Constructor
     
       Circle(double radius,String colour){ //Requirement 4
           this.radius=radius;
           this.colour=colour;
       }
     
     
       //Methods

       public String toString(){ //Requirement 5
           return String.format("Radius: %f Area: %f Colour: %s", this.getRadius(),this.findArea(),this.getColour());
       }
     
     
       public String getColour() { //Requirement 3
       return this.colour;
   }

   public void setColour(String colour) { //Requirement 3
       this.colour = colour;
   }

  

          

       //get method (Accessor Methods )

       public double getRadius ( ) {

   return radius;

       }

  

       //set method (Mutator Methods)

        public void setRadius (double r) {

   this.radius=r; //Requirement 1

        }

  

        public double findArea ( ) {

   return PI*radius*radius; //area= pi r squared

        }

  

        public double findCircumference ( ) {

   return 2 * PI * radius;

        }

   }//end Circle

CircleDemo class

import java.util.Scanner;

public class CircleDemo{

public static void main(String[] args){

Circle cir1, cir2;

cir1 = new Circle();

cir2 = new Circle();

cir1.setRadius(3.0);

cir2.setRadius(11.01);

System.out.println("Area of the first circle: " + cir1.findArea());

System.out.println("Circumference of the first circle: " + cir1.findCircumference());

System.out.println("Area of the second circle: " +cir2.findArea());

System.out.println("Circumference of the second circle: " +cir2.findCircumference());

////Requirement 6-Start

System.out.println("Reading from the user");
Scanner scan=new Scanner(System.in);
System.out.println("Enter the radius:");
double rad=scan.nextDouble();
System.out.println("Enter the Colour:");
String color=scan.next();

Circle c=new Circle(rad,color);
System.out.println(c.toString());
System.out.println("Circumference:"+c.findCircumference());


////Requirement 6-End

}

}

Below of the output generated from the code run.

Add a comment
Know the answer?
Add Answer to:
Write a java class definition for a circle object. The object should be capable of setting...
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
  • 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.     ...

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

  • Create a class named Circle with fields named radius, diameter, and area. Include a constructor that...

    Create a class named Circle with fields named radius, diameter, and area. Include a constructor that sets the radius to 1 and calculates the other two values. Also include methods named setRadius() and getRadius(). The setRadius() method not only sets the radius, but it also calculates the other two values. (The diameter of a circle is twice the radius, and the area of a circle is pi multiplied by the square of the radius. Use the Math class PI constant...

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

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

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

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • .Write a class Circle which has a radius and a color (a String). The class should...

    .Write a class Circle which has a radius and a color (a String). The class should include a toString() method, and a method to compute the circumference (2tr), and any other methods you may need java

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An...

    Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An instance variable named x of type int. An instance variable named y of type int. Declare a method named toString() Returns a string representation of the point. Constructor that accepts values of all data members as arguments. Define a Sub class named Circle. A Circle object stores a radius (double) and inherit the (x, y) coordinates of its center from its super class Point....

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