Question

The following code computes the radius of a circle. Using static methods from the Math class...

The following code computes the radius of a circle. Using static methods from the Math class complete the method that computes the radius of a circle using the formula  r2=(x-h)2 +(y-k)2 , given the (x,y) coordinates of one point on its circumference and the (h,k) coordinates of its center.

public class Circle {
   public static void main(String[] C)
   {
       double x1 =14.25;
       double y1 =13.68;
       double xCenter = 25.678;
       double yCenter = 10.32547;
       System.out.println("The first given points of your circle is ("+x1+", "+y1+")");
       System.out.println("The center points of your circle is ("+xCenter+", "+yCenter+")");
       double radius = radius(x1,y1,xCenter,yCenter);
       System.out.println("The radio of your circunferent is "+ radius);
      
       //Use a method to find the integer part of the radio.
       double radiusfloor = Math.floor(radius);
      
       System.out.println("The floor of the radius of your circunference is "+ radiusfloor);
   }
  
   public static double radius(double x,double y,double cx, double cy)
   {
       double radius = 0.0;  
       // write the expression to compute radius using the static methods pow() and sqrt() provided by Math class.
       return radius;
   }

}

Here is the JUnit Test:

import static org.junit.Assert.*;
import org.junit.Test;

public class CircleTest {

        @Test
        public void testRadius() {
                double epsilon = 1e-10;
                assertEquals("Circle.radius returned incorrect result", 0.0,Circle.radius(5,5,5,5), epsilon);
                assertEquals("Circle.radius returned incorrect result", 5, Circle.radius(3,4,0,0), epsilon);
                assertEquals("Circle.radius returned incorrect result", 13.19669493661197, Circle.radius(5.0285,1.055,5.24,14.25), epsilon);
                assertEquals("Circle.radius returned incorrect result", 63.97655820689325, Circle.radius(0,47,58,20), epsilon);
        }
        
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

package com.example.demo;
public class Circle {
   public static void main(String[] C) {
       double x1 = 14.25;
       double y1 = 13.68;
       double xCenter = 25.678;
       double yCenter = 10.32547;
       System.out.println("The first given points of your circle is (" + x1 + ", " + y1 + ")");
       System.out.println("The center points of your circle is (" + xCenter + ", " + yCenter + ")");
       double radius = radius(x1, y1, xCenter, yCenter);
       System.out.println("The radio of your circunferent is " + radius);

       // Use a method to find the integer part of the radio.
       double radiusfloor = Math.floor(radius);

       System.out.println("The floor of the radius of your circunference is " + radiusfloor);
   }

   public static double radius(double x, double y, double cx, double cy) {
       double radius = 0.0;
       // write the expression to compute radius using the static methods pow() and
       // sqrt() provided by Math class.
       double a = java.lang.Math.pow(x - cx, 2); // inbuilt pow method to caluclate (x-h)^2
       double b = java.lang.Math.pow(y - cy, 2); // inbuilt pow method to caluclate (y-k)^2
       radius = java.lang.Math.sqrt(a + b); // sqrt to find the value of radius r=sqrt(a+b);
       return radius; // returning the radius
   }

}

Please let me know in case you have any queries. Thanks !!!

Add a comment
Know the answer?
Add Answer to:
The following code computes the radius of a circle. Using static methods from the Math class...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a class named Circle with fields named radius, diameter, and area. Include a constructor that...

    Create a class named Circle with fields named radius, diameter, and area. Include a constructor that sets the radius to 1 and calculates the other two values. Also include methods named setRadius() and getRadius(). The setRadius() method not only sets the radius, but it also calculates the other two values. (The diameter of a circle is twice the radius, and the area of a circle is pi multiplied by the square of the radius. Use the Math class PI constant...

  • Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume...

    Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume of rectangular objects. First, write an abstract super class RectangularShape, then write a subclass Rectangle that inherits from the super class to compute areas of rectangles, including squares if there is only one data. Finally, write another subclass Cuboid that inherits from its super class Rectangle above to compute surface areas and volumes of cuboids, including 3 equal sided cube. Must apply code-reusability in...

  • PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is to...

    PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is too difficult you do not have to do part 3 but it would be greatly appreciated if you do ! Obtain example code files Circle.java, Shape.java, CircleShape2.java, Sphere.java, and CircleShapeApp.java from the downloaded files in Ch8. Compile and execute the example and understand the polymorphism it performs. (I have copied and pasted all of these files below) Modify...

  • what are the debuggers in Java? import java.text.NumberFormat; /** * * This class represents a circle...

    what are the debuggers in Java? import java.text.NumberFormat; /** * * This class represents a circle that would be used as part * of a larger geometry application */ public class Circle {    private double radius;    private NumberFormat numberFormat; /** *    Constructor for the Circle. * @param radius for the circle */    public Circle(double r) {        radius = r;    } /** *    This method uses the radius of the circle to compute...

  • please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){...

    please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); double radius; double length; System.out.println("Enter radius:"); double r = scan.nextDouble(); System.out.println("Enter length:"); double l = scan.nextDouble(); System.out.println("Enter sides:"); int s = scan.nextInt(); Circle c = Circle(radius); p = RegularPolygon(l , s); System.out.println(c); System.out.println(p); } } Instructions Debug the code provided in the starter file so it does the following: • creates two Double objects named radius and length creates...

  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • 2.1.1: Area of a Circle w/ Ra... SUBMIT CONTINUE RUN CODE TEST CASES ASSIGNMENT DOCS GRADE...

    2.1.1: Area of a Circle w/ Ra... SUBMIT CONTINUE RUN CODE TEST CASES ASSIGNMENT DOCS GRADE MORE Description Write a method that returns the area of a circle given the radius 1 import java.util.Scanner; 2. public class Main { 3. public static void main(String[] args) { Scanner io- new Scanner(System.in); System.out.println("Inout the radius of the circle: "); 6 double radius - io.nextDouble 7 System.out.println("Perimeter is -" + (2 radius - Math.PD)): 8 System.out.println("Area is - " . (Math.PI. radius radius)):...

  • What is the output of the following code? public class Test { pub be static void...

    What is the output of the following code? public class Test { pub be static void main(String() args) { Object o 1 -new Object(); Object o2 = new Object(); System out.print((o1 = o2) + "" + (o1 equals(o2))); } } A. false false B. true true C. false true D. true false Analyze the following code. //Program 1: public class Test { public static void main(String[] args) { Object circle 1 = new Circle(); Object circle2 = new Circle(); System...

  • How can I write a unit test for the following java method? (I am using intelliJ)...

    How can I write a unit test for the following java method? (I am using intelliJ) public class ReviewEmployee{ public void EmployeeReview(Employee currentEmployee){ System.out.println("Your recorded hours for the week are: ") + (currentEmployee.getHours()); System.out.println("Your satisfactory rating is: ") + (currentlyEmployee.getRating()); System.out.println("Your bonus is: "); if(currentEmployee.getRating() < 3){ System.out.println(currentlyEmployee.getBonus()); } else{ System.out.println(currentlyEmployee.getBonus() + 500); } } } The Employee class is as follows: Public class Employee{                Private double hours;                Private double rating;                Private double bonus;                Public Employee(double...

  • Complete the implementation of the Sphere class based on the Diagram below. Use the DecimalFormat class...

    Complete the implementation of the Sphere class based on the Diagram below. Use the DecimalFormat class in Sphere.java to format your output in the toString method. Then download the tester application SphereTester.java to test your Sphere class. Do not change SphereTester.java. Sphere private double radius      //radius of the sphere object private static int numSpheres   //static or class variable. All Sphere objects share it public Sphere()    //constructor . Initialize Sphere objects’ instance variable radius to 0.0. It also increments numSpheres by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT