Given the above UML diagram write a Circle class by implementing in code format.
ID: 3838746 • Letter: G
Question
Given the above UML diagram write a Circle class by implementing in code format. Display the results for three different radius (3.75, 8.99, 2) 1. Create a firstArray with 10 elements 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 2.Create a secondArray that will also have 10 elements and add the values in that array as the multiples of 2 by taking the values from the firstArray 3. Create a method that will take the secondArray as an argument and add all its elements by returning the Sum. 4. Call this method from the Main() method. Use appropriate call and arguments to get the right result.Explanation / Answer
Question 1 :
class Circle
{
private double radius;
public Circle()
{
radius = 0;
}
public Circle(double radius)
{
this.radius = radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return 3.14*radius*radius;
}
public double getDiameter()
{
return 2*radius;
}
public double getCircumference()
{
return 2*3.14*radius;
}
}
class TestCircle
{
public static void main (String[] args)
{
Circle c1 = new Circle(3.75);
Circle c2 = new Circle();
c2.setRadius(8.99);
Circle c3 = new Circle(2);
System.out.println("Area of Circle c1 = "+ c1.getArea());
System.out.println("Diameter of Circle c1 = "+ c1.getDiameter());
System.out.println("Circumference of Circle c1 = "+ c1.getCircumference());
System.out.println(" Area of Circle c2 = "+ c2.getArea());
System.out.println("Diameter of Circle c2 = "+ c2.getDiameter());
System.out.println("Circumference of Circle c2 = "+ c2.getCircumference());
System.out.println(" Area of Circle c3 = "+ c3.getArea());
System.out.println("Diameter of Circle c3 = "+ c3.getDiameter());
System.out.println("Circumference of Circle c3 = "+ c3.getCircumference());
}
}
output:
Area of Circle c1 = 44.15625
Diameter of Circle c1 = 7.5
Circumference of Circle c1 = 23.55
Area of Circle c2 = 253.775114
Diameter of Circle c2 = 17.98
Circumference of Circle c2 = 56.4572
Area of Circle c3 = 12.56
Diameter of Circle c3 = 4.0
Circumference of Circle c3 = 12.56
Question 2 :
class TestArray
{
public static void main (String[] args)
{
int[] firstArray = {1,2,3,4,5,6,7,8,9,10};
int[] secondArray = new int[10];
for(int i=0;i<10;i++)
{
secondArray[i] = 2*firstArray[i];
}
System.out.println("Sum of elements of secondArray = "+ SumArray(secondArray));
}
public static int SumArray(int[] a)
{
int sum = 0;
for(int i=0;i<10;i++)
{
sum = sum + a[i];
}
return sum;
}
}
output:
Sum of elements of secondArray = 110
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.