Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target

In the previous article, we have seen Java Program to Solve Pizza Cut Problem(Circle Division by Lines)

In this article we will discuss about how to find minimum revolutions to move center of a circle to a target using java programming language.

Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target

Before jumping into the program directly, let’s first know how can we find minimum revolutions to move center of a circle to a target .

Explanation:

Formula to Find Minimum Revolutions to Move Center of a Circle to a Target: ceil(d/2*r)

Example:

When r=2,P1=(0,0), and P2=(0,4), d = 4

Minimum Revolutions: ceil(d/2*r)

=> ceil(4/2*2)

=>ceil(1)

=> 1

Let’s see different ways to find minimum revolutions to move center of a circle to a target.

Method-1: Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target By Using Static Value

Approach:

  • Declare the value for the coordinates of the point, of radius and size of radius.
  • Find the distance between both the points.
  • Find the minimum revolutions using the formula ceil(distance/(2*radius))
  • Then print the result.

Program:

import java.awt.Point; 
import java.util.Scanner;
import static java.lang.Math.*;

public class Main
{
    public static void main(String[] args){
        // Static initialization of both points and the radius
        Point rad = new Point(0,0);
        Point p = new Point(0,4);
        double radius = 2;
        // Caclculates the distance between the radius and the point
        double distance = Math.sqrt((rad.x-p.x)*(rad.x-p.x)+(rad.y-p.y)*(rad.y-p.y));
        // Prints the minimum revloutions
        System.out.println("The minimum revolutions required is "+(int)Math.ceil(distance/(2*radius)));
    }
}
Output:

The minimum revolutions required is 1

Method-2: Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target By User Input Value

Approach:

  • Take user input the value for the coordinates of the point and radius and size of radius.
  • Find the distance between both the points.
  • Find the minimum revolutions using the formula ceil(distance/(2*radius))
  • Then print the result.

Program:

import java.awt.Point; 
import java.util.Scanner;
import static java.lang.Math.*;

public class Main
{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        //Asking the user to input both points and the radius
        System.out.println("Enter coordinates of the point");
        Point p = new Point(scan.nextInt(),scan.nextInt());
        System.out.println("Enter coordinates of the radius");
        Point rad = new Point(scan.nextInt(),scan.nextInt());
        System.out.println("Enter the radius");
        double radius = scan.nextDouble();
        // Caclculates the distance between the radius and the point
        double distance = Math.sqrt((rad.x-p.x)*(rad.x-p.x)+(rad.y-p.y)*(rad.y-p.y));
        // Prints the minimum revloutions
        System.out.println("The minimum revolutions required is "+(int)Math.ceil(distance/(2*radius)));
    }
}
Output:

Enter coordinates of the point
5 5
Enter coordinates of the radius
3 3 
Enter the radius
2
The minimum revolutions required is 1

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Articles: