In: Computer Science
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.
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.