Question

In: Computer Science

Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the...

Circle
Radius: double
Circle()
Circle(newRadius: double)
getArea(): double
setRadius(newRadius: double): void
getRadius(): double

After creating the Circle class, you should test this class from the main() method by passing objects of this
class to a method “ public static void printAreas(Circle c, int times)”
You should display the area of the circle object 5 times with different radii.

2

java.util.Random
+Random()
+Random(seed: long)
+nextInt(): int
+nextInt(n: int): int
+nextLong(): long
+nextDouble(): double
+nextFloat(): float
+nextBoolean(): boolean

Constructs a Random object with the current time as its seed.
Constructs a Random object with a specified seed.
Returns a random int value.
Returns a random int value between 0 and n (exclusive).
Returns a random long value.
Returns a random double value between 0.0 and 1.0 (exclusive).
Returns a random float value between 0.0F and 1.0F (exclusive).
Returns a random boolean value.

Using this class, do the following:
1. Generate and display 20 random numbers of type integer in the range 0 to 50.
2. Generate and display 20 random numbers of type integer with seed 3 in the range 0 to 1000.
3. Generate and display 20 random numbers of type double in the range 10 to 50.

3

java.util.Date
+Date()
+Date(elapseTime: long)
+toString(): String
+getTime(): long
+setTime(elapseTime: long): void

Constructs a Date object for the current time.
Constructs a Date object for a given time in
milliseconds elapsed since January 1, 1970, GMT.
Returns a string representing the date and time.
Returns the number of milliseconds since January 1,
1970, GMT.
Sets a new elapse time in the object.

The + sign indicates
public modifer

Using this class, display the current time.

Solutions

Expert Solution

Find the java code below. Comments are provided throughout the code.

Circle class

public class Circle {
        
        private double radius;
        
        public Circle() {
        }

        public Circle(double newRadius) {
                this.radius = newRadius;
        }
        
        public double getArea() {
                return Math.PI * radius * radius;
        }

        public double getRadius() {
                return radius;
        }

        public void setRadius(double newRadius) {
                this.radius = newRadius;
        }
}

Main Method - all 3 questions here

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class Main {
        
        public static void main(String[] args) {
                
                /*
                 * Question 1
                 */
                
                // print are 5 times
                Circle c = new Circle();
                printAreas(c, 5);
                
                /*
                 * Question 2
                 */
                
                // 1. Generate and display 20 random numbers of type integer in the range 0 to 50.
                
                Random r1 = new Random();
                
                for(int i = 0; i < 20; i++) {
                        System.out.print(r1.nextInt(50) + " ");
                }
                System.out.println();
                
                // 2. Generate and display 20 random numbers of type integer with seed 3 in the range 0 to 1000.
                
                Random r2 = new Random(3);
                
                for(int i = 0; i < 20; i++) {
                        System.out.print(r2.nextInt(1000) + " ");
                }
                System.out.println();
                
                // 3. Generate and display 20 random numbers of type double in the range 10 to 50.
        
                for(int i = 0; i < 20; i++) {
                        System.out.print(10 + r1.nextDouble() * 40 + " ");
                }
                System.out.println();
        
                /*
                 * Question 3
                 */
                
                // 1. Display the current time.
                
                Date date = new Date();
                
                // this method display the date also
                System.out.println(date.toString());
                
                // to display only time, use simpleDateFormat
                SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss a");
                
                System.out.println(dateFormat.format(date));
                
                
        }
        
        public static void printAreas(Circle c, int times) {
                
                Random r = new Random();
                
                for (int i = 0; i < times; i++) {
                        
                        // set a random radius
                        c.setRadius(r.nextDouble() * 10);
                        
                        // print area
                        System.out.println(c.getArea());
                }
        }

}

Kindly upvote.


Related Solutions

Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class 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 for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class 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 for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Write a Circle Class that has the following fields: • radius: a double • PI: a...
Write a Circle Class that has the following fields: • radius: a double • PI: a final double initialized with the value 3.14159 • Constructor. Accepts the radius of the circle as an argument • Constructor. A no-arg constructor that sets the radius field to 0.0. • setRadius. A mutator method for the radius field. • getRadius. An accessor method for the radius field. • getArea. Returns the area of the circle, which is calculated as area = PI *...
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this...
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this radius, the circumference of the circle, the area of the circle, the surface area of a sphere with that radius and the area of a sphere with that radius. Each of its computation functions returns its answers in call by reference parameters. Here are the formulas for computation: Circumference = 2 • π • r Circle Area = π • r2 Sphere Surface Area...
Write a Circle class that has the following member variables: radius as a double PI as...
Write a Circle class that has the following member variables: radius as a double PI as a double initialized with 3.14159 The class should have the following member functions: Default constructor Sets the radius as 0.0 and pi as 3.14159 Constructor Accepts the radius of the circles as an argument setRadius A mutator getRadius An accessor getArea Returns the area of the circle getDiameter Returns the diameter of the circle getCircumference Returns the circumference of the circle Write a program...
Write a Circle class that has the following member variables: • radius: a double • pi:...
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 for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the circle, which...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
If a circle C with radius 1 rolls along the outside of the circle x2 +...
If a circle C with radius 1 rolls along the outside of the circle x2 + y2 = 36, a fixed point P on C traces out a curve called an epicycloid, with parametric equations x = 7 cos(t) − cos(7t), y = 7 sin(t) − sin(7t). Graph the epicycloid. Find the area it encloses.
A circle of radius r has area A = πr2. If a random circle has a...
A circle of radius r has area A = πr2. If a random circle has a radius that is uniformly distributed on the interval (0, 1), what are the mean and variance of the area of the circle? Change the distribution of the radius to an exponential distribution with paramter β = 2. Also find the probability that the area of the circle exceeds 3, if it is known that the area exceeds 2.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT