For JAVASCRIPT, NOT C++ The following formula gives the distance between two poi
ID: 3582129 • Letter: F
Question
For JAVASCRIPT, NOT C++
The following formula gives the distance between two points (x1, y1) and (x2, y2) in the Cartesian plane:
Given the center and a point on a circle, you can use this formula to find the radius of the circle. Write a program that prompts the user to enter the center and a point on the circle. The program should then output the circle’s radius, diameter, circumference, and area. Your program must have at least the following methods:
a. distance: This method takes as its parameters four numbers that represent two points in the plane and returns the distance between them.
b. radius: This method takes as its parameters four numbers that represent the center and a point on the circle, calls the method distance to find the radius of the circle, and returns the circle’s radius.
f. Export the output into a text file
This is what I have so far, but I can't get it to work. Keep saying error at outFile.printf
What can I do to fix this?
import java.util.*; //importing teh scanner class
import java.io.*;
public class circle
{
public static void main(String[] args)throws
FileNotFoundException
{
System.out.println("Enter the center and point on the circle in the sequence of x1, y1, and x2, y2");
double w,x,y,z;
double Radius, Diameter, Circumference, Area;
Scanner sc = new Scanner(System.in);
PrintWriter outFile = new
PrintWriter("CircleSolutions.txt");
w = sc.nextDouble();
x = sc.nextDouble();
y = sc.nextDouble();
z = sc.nextDouble();
Radius = radius(w,x,y,z);
Circumference = circumference(Radius);
Area = area(w,x,y,z);
System.out.println("The radius of the circle is " + Radius);
System.out.println("The circumference of the circle is " + Circumference);
System.out.println("The area of the circle is " + Area);
}
public static double distance (double x1, double y1, double x2, double y2)
{
double d1, d2, Distance;
d1 = Math.pow((x2-x1),2);
d2 = Math.pow((y2-y1),2);
Distance = Math.sqrt(d1+d2);
return Distance;
}
public static double radius(double x, double y, double x1, double y1)
{
double Radius;
Radius = distance (x, y, x1, y1);
return Radius;
outFile.printf("The radius of the circle is: "
+ String.format("%.2f",Radius) + " ");
}
public static double circumference(double n)
{
double Circumference1=(2*Math.PI*n);
return Circumference1;
outFile.printf("The circumference of the circle is: "
+ String.format("%.2f",Circumference) + " ");
}
public static double area(double a, double b, double c, double d)
{
double r1= radius(a,b,c,d);
double area = (Math.PI*r1*r1);
return area;
outFile.printf("The area of the circle is: "
+ String.format("%.2f",area) + " ");
}
}
Explanation / Answer
I have made few changes to your code which is working good now
****It is JAVA language not JAVASCRIPT****
--> Here is the total code below , I have added comments where ever I changed code.
--> Just go through the comments and run it then you will get your answers written in file.
--> Make sure that you remove those comments that I have written before showing it to someone.
--> Because they are for your understanding of writing the code.
import java.util.*; //importing the scanner class
import java.io.*;
public class Circle {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Enter the center and point on the circle in the sequence of x1, y1, and x2, y2");
double w, x, y, z;
double Radius, Diameter, Circumference, Area;
//You did not used that Diameter variable above, its better to remove the declaration
Scanner sc;
sc = new Scanner(System.in);
PrintWriter outFile = new PrintWriter("CircleSolutions.txt");
w = sc.nextDouble();
x = sc.nextDouble();
y = sc.nextDouble();
z = sc.nextDouble();
Radius = radius(w, x, y, z);
Circumference = circumference(Radius);
Area = area(w, x, y, z);
System.out.println("The radius of the circle is: " + Radius);
System.out.println("The circumference of the circle is " + Circumference);
System.out.println("The area of the circle is " + Area);
//Instead of writing the data into the file in the functions, it is easy to write the data
//at once in the main function, like I did here
outFile.println("The radius of the circle is " + String.format("%.2f", Radius) + " "
+"The circumference of the circle is: " + String.format("%.2f", Circumference) + " "
+"The area of the circle is: " + String.format("%.2f", Area) + " ");
//Calling checkError method which will check for errors in the operation and
//flushes the data , data won't be written to the file until unless you flush it
System.out.println(outFile.checkError());
//Close the file after working with it
outFile.close();
//Its good to close the scanner always after completing our work
sc.close();
}
public static double distance(double x1, double y1, double x2, double y2) {
double d1, d2, Distance;
d1 = Math.pow((x2 - x1), 2);
d2 = Math.pow((y2 - y1), 2);
Distance = Math.sqrt(d1 + d2);
return Distance;
}
public static double radius(double x, double y, double x1, double y1) {
double Radius;
Radius = distance(x, y, x1, y1);
//Your code gave error in this line before , because you are trying to access
//outFile variable which is main method and we can't access that variable untill unless it is global or
//we sent it through parameters
//outFile.printf("The radius of the circle is: " + String.format("%.2f", Radius) + " ");
return Radius;
}
public static double circumference(double n) {
double Circumference1 = (2 * Math.PI * n);
//As I have explained in the radius method , same thing applies here
//outFile.printf("The circumference of the circle is: " + String.format("%.2f", Circumference1) + " ");
return Circumference1;
}
public static double area(double a, double b, double c, double d) {
double r1 = radius(a, b, c, d);
double area = (Math.PI * r1 * r1);
//As I have explained in the radius method , same thing applies here
//outFile.printf("The area of the circle is: " + String.format("%.2f", area) + " ");
return area;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.