Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { } //get method (Accessor Methods )

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

}

}

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: ---

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions