Question

java Create a program that defines a class called circle. Circle should have a member variable...

java

Create a program that defines a class called circle. Circle should have a member

variable called radius that is used to store the radius of the circle. Circle should also

have a member method called calcArea that calculates the area of the circle using

the formula area = pi*r^2. Area should NOT be stored in a member variable of

circle to avoid stale data.

Use the value 3.14 for PI.

For now, make radius public and access it directly using the dot operator. Do not

worry about input validation for now. In main instantiate two objects of class circle

and have the user enter a radius for each. Store the radii in the appropriate objects'

member variables, then print the radius and area of each circle using the member

variable and function.

public class circle

{

public double radius = 0;

void printArea()

{

System.out.println(radius * radius * 3.14159);

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Modify what is above so that the radius variable in class circle is private. Write a setter

and getter method for radius, as well as a default constructor that initializes radius

to 0 and a constructor that takes one parameter for radius. If radius is given as a

negative in a call to the setter or the constructor set it to 0 instead.

In main, instantiate (declare/create) and use circle objects as needed to prove that

all of your functions, including both constructors, work properly (construct a test

case for your class in main).

Homework Answers

Answer #1

Dear Student ,

As per requirement submitted above kindly find below solution.

Demonstration 1:

circle.java :

//Java class
public class circle {
   //member variable
   public double radius = 0;
   //method to calculate area
   public void calcArea()
   {
       //print area of circle
       System.out.println("Area of circle with radius "+this.radius+" is "+(this.radius * this.radius * 3.14159));
   }
}
****************************

CircleTest.java :

//import package
import java.util.*;
//Java class
public class CircleTest {
   //entry point of the program , main() method
   public static void main(String[] args) {
       //creating object of Scanner class
       Scanner sc=new Scanner(System.in);
       //asking user radius
       System.out.print("Enter Radius 1: ");
       double radius1=sc.nextDouble();//reading radius
       //asking user radius
       System.out.print("Enter Radius 2: ");
       double radius2=sc.nextDouble();//reading radius
       //creating object of Circle class
       circle c1=new circle();
       c1.radius=radius1;//set radius
       //call method to print area
       c1.calcArea();
       //creating object of Circle class
       circle c2=new circle();
       c2.radius=radius2;//set radius
       //call method to print area
       c2.calcArea();
      
   }

}

==================================

Output :Compile and Run CircleTest.java to get the screen as shown below

Screen 1:

******************************

Demonstration 2:

circle.java :

//Java class
public class circle {
   //member variable
   private double radius;
   //default constructor
   public circle()
   {
       this.radius=0;//set radius to 0
   }
   //constructor with one parameter
   public circle(double r)
   {
       setRadius(r);//calling setter method
   }
   //getter method
   public void setRadius(double r)
   {
       //checking value of radius
       if(r<0)
       {
           //if radius is negative then
           this.radius=0;//set radius to 0
       }
       else {
           this.radius=r;
       }
   }
   //getter method
   public double getRadius()
   {
       return this.radius;//return radius
   }
   //method to calculate area
   public void calcArea()
   {
       //print area of circle
       System.out.println("Area of circle with radius "+getRadius()+" is "+(getRadius() * getRadius() * 3.14159));
   }
}
*****************************

CircleTest.java :

//import package
import java.util.*;
//Java class
public class CircleTest {
   //entry point of the program , main() method
   public static void main(String[] args) {
       //creating object of Scanner class
       Scanner sc=new Scanner(System.in);
       //creating object of Circle class
       circle c1=new circle();
       //asking user radius
       System.out.print("Enter Radius 1: ");
       c1.setRadius(sc.nextDouble());//reading radius
       //asking user radius
       System.out.print("Enter Radius 2: ");
       double radius=sc.nextDouble();//reading radius
       //creating object of Circle class
       circle c2=new circle(radius);      
       //call method to print area
       c1.calcArea();      
       //call method to print area
       c2.calcArea();
      
   }

}
==================================

Screen 1:

Screen 2:When radius is negative

NOTE :PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Know the answer?
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
Similar Questions
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1....
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for all methods should print simple messages to the screen using System.out.println(). Add an integer speed variable that is changed using ChangeSpeed method. ChangeSpeed adds 5 to the speed each time it is called. Create a default constructor that sets the initial speed to 0. Don't create other constructors or any setter/getter methods. // code from Q1: interface Vehicle { void Start();...
Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for...
Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for all methods should print simple messages to the screen using System.out.println(). Add an integer speed variable that is changed using the ChangeSpeed method. ChangeSpeed adds 5 to the speed each time it is called. Create a default constructor that sets the initial speed to 0. Don't create other constructors or any setter/getter methods. PLEASE CODE THIS IN JAVA
C++ Derive the cylinder class from the base circle class. Assume the circle class has a...
C++ Derive the cylinder class from the base circle class. Assume the circle class has a protected member variable representing the radius called radius and declared as a double with a default value of 1.0. It also has a public function called calcVal that evaluates the area of a circle as PI * radius * radius where PI is a constant 3.14. In your derived class include an additional protected member representing the length of the cylinder. Call this variable...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods....
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields: int x (x-coordinate for its top left corner) int y (y coordinate for its top left corner) double height (height of the rectangle) double width (width of the rectangle) Your rectangle objects should have the following methods: public Rectangle(int newx, int newy, int newwidth, int newheight) Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height....
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
We encourage you to work in pairs for this challenge to create a Student class with...
We encourage you to work in pairs for this challenge to create a Student class with constructors. First, brainstorm in pairs to do the Object-Oriented Design for a Student class. What data should we store about Students? Come up with at least 4 different instance variables. What are the data types for the instance variables? Write a Student class below that has your 4 instance variables and write at least 3 different constructors: one that has no parameters and initializes...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT