Join 149,950 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,318 people online right now. Registration is fast and FREE... Join Now!
/* Problem4 * Brandon Watkins * 10/23/08 * * This program prompts the user to input the center of a circle and a point ona circle. It then calculates the radius, diameter, circumference, and area. */
// Import Classes Uncomment as needed import java.util.*; import java.lang.*;
public class Problem4 {
static Scanner console = new Scanner(System.in); // create scanner to obtain input from the command line
// Prompts for first double and stores value in x1 System.out.print ("Enter the value for the first point on the circle: "); x1 = console.nextDouble(); System.out.println();
// Prompts for first double and stores value in x2 System.out.print ("Enter the second point on the circle: "); x2 = console.nextDouble(); System.out.println();
// Prompts for first double and stores value in y1 System.out.print ("Enter the third point on the circle: "); y1 = console.nextDouble(); System.out.println();
// Prompts for first double and stores value in y2 System.out.print ("Enter the fourth point on the circle: "); y2 = console.nextDouble(); System.out.println();
This is what I have so far. I am stuck hardcore on the radius and distance methods.
I have to make a program that out puts a circle's radious, area, and circumference when it is given the center and a point on the circle.
I have make and use these 5 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 ricle, returns the circles radius
c. This method takes as its parameter a number that represents the radius of the circle and returns the circles circumference.
d. area: distance: This method takes as its parameters a number that represents the radius of a circle and returns the circles area.
e. Use the named constant PI from the math class to specifi the value of pi
I am getting these errors. I am a complete noob and am lost. I have been working on this for hours.
// Don't forget to set the variable, don't just call the function // Also notice that we do not specify the types of variables in the parameters when CALLING the function distancevar = distance (x1, x2, y1, y2); radius = distancevar; return radius; }
You need to create the variable to hold the return of the function call. Here we created "distancevar" and also notice that we DO NOT use the data types when CALLING the function. Data types are only used in parameters when you are defining functions etc.
This should get you compiling and moving on the right path. Enjoy!
"At DIC we be radius calculating code ninjas... I calculate the distance between your left ear and right ear to be -1.42... wait, that can't be right."
The reason you're getting all the errors on line 70 (I think thats the line that calls the distance method within the radius method) is because I think when you call a method from within another, you don't need to declare the input parameter variable types, as they have already been declared.
When you execute the radius method, it will request those input parameters (x1, x2, y1, y2), and they will be stored as instance variables which will then be used as the inputs for the distance method.
If radius is always going to = distance, why not declare a field variable, and set radius = distance?
Thank you so much Martyr2, I was trying to fix that for more than an hour and it was such a simple thing lol, thank you so much. I don't even wanna know how much longer I coulda sat their attempting to figure that out. it now compiles perfectly.
This post has been edited by bwat47: 23 Sep, 2008 - 06:22 PM
alright, I am so stuck again haha. I finished all the methods, but I can't get it to print the results. It prompts me to enter the points on the circles and then nothing happens. here is my code:
CODE
/* Problem4 * Brandon Watkins * 10/23/08 * * This program prompts the user to input the center of a circle and a point ona circle. It then calculates the radius, diameter, circumference, and area. */
// Import Classes Uncomment as needed import java.util.*; import java.lang.*;
public class Problem4 {
static Scanner console = new Scanner(System.in); public static final double PI = 3.14159; // create scanner to obtain input from the command line
// Prompts for first double and stores value in x1 System.out.print ("Enter the value for the first point on the circle: "); x1 = console.nextDouble(); System.out.println();
// Prompts for first double and stores value in x2 System.out.print ("Enter the second point on the circle: "); x2 = console.nextDouble(); System.out.println();
// Prompts for first double and stores value in y1 System.out.print ("Enter the third point on the circle: "); y1 = console.nextDouble(); System.out.println();
// Prompts for first double and stores value in y2 System.out.print ("Enter the fourth point on the circle: "); y2 = console.nextDouble(); System.out.println();
public static double area (double radius) { double area; area = PI * (radius * radius); return area;
} public static void printarea (double radius, double area, double circumference) { System.out.println ("The Radius is " + radius); System.out.println ("area: " + area); System.out.println ("circumference: " + circumference);
}
}
This is what happens when I enter my numbers:
Welcome to DrJava. Working directory is C:\Users\Brandon\Documents\Dr Java\Week4 > java Problem4 Enter the value for the first point on the circle: [DrJava Input Box]
Enter the second point on the circle: [DrJava Input Box]
Enter the third point on the circle: [DrJava Input Box]
Enter the fourth point on the circle: [DrJava Input Box]
>
please help, I'm so close to being finished!
This post has been edited by bwat47: 23 Sep, 2008 - 07:31 PM
the methods are never are being called that you have created. you must call the methods that you created from the main method that you have. right now you have, public static void main(String[] args), method. and everything is getting executed from that method, so now you just need to include the calls to the other methods within this method
only the code in the main() method is going to get executed when you start the program. the other methods must be called in order to be executed
This post has been edited by Mach1Guy: 23 Sep, 2008 - 08:54 PM
double x1; double x2; double y1; double y2; // double distance; remove this variable (delete this line) double radius; //add this variable double circumference; //add this variable double area; //add this variable
//executable statements
// Prompts for first double and stores value in x1 System.out.print ("Enter the value for the first point on the circle: "); x1 = console.nextDouble(); System.out.println();
// Prompts for first double and stores value in x2 System.out.print ("Enter the second point on the circle: "); x2 = console.nextDouble(); System.out.println();
// Prompts for first double and stores value in y1 System.out.print ("Enter the third point on the circle: "); y1 = console.nextDouble(); System.out.println();
// Prompts for first double and stores value in y2 System.out.print ("Enter the fourth point on the circle: "); y2 = console.nextDouble(); System.out.println();
//Make the calls to the methods that you created radius = radius(x1,x2,y1,y2); circumference = circumference(radius); area = area(radius); printarea(radius,area,circumference);
just another quick note...i'm not exactly sure on the math on all this so i can't check you there, but i'm suspicious of the distance and radius method
from the code above, essentially your radius is getting set to whatever you are calculating for distance because you are saying: radius = whatever is returned from your distance method
Yeah distance between those two points is supposed to be the raduis (or so it says in the book)
I am still at a loss of how to get it to print any results though, I tried to make a method that would call the others and print their values but I get errors. Here's what I have right now:
CODE
/* Problem4 * Brandon Watkins * 10/23/08 * * This program prompts the user to input the center of a circle and a point ona circle. It then calculates the radius, diameter, circumference, and area. */
// Import Classes Uncomment as needed import java.util.*; import java.lang.*;
public class Problem4 {
static Scanner console = new Scanner(System.in); public static final double PI = 3.14159; // create scanner to obtain input from the command line
// Prompts for first double and stores value in x1 System.out.print ("Enter the value for the first point on the circle: "); x1 = console.nextDouble(); System.out.println();
// Prompts for first double and stores value in x2 System.out.print ("Enter the second point on the circle: "); x2 = console.nextDouble(); System.out.println();
// Prompts for first double and stores value in y1 System.out.print ("Enter the third point on the circle: "); y1 = console.nextDouble(); System.out.println();
// Prompts for first double and stores value in y2 System.out.print ("Enter the fourth point on the circle: "); y2 = console.nextDouble(); System.out.println();
look at the post directly above your last post....i included the calls to all your methods at the very bottom of it (you have to scroll down). i included everything you need in your main method in the first code box above
you do have the correct idea above though...almost there let me know when you get it or if you have more problems...good luck, you can also IM me if needed
This post has been edited by Mach1Guy: 24 Sep, 2008 - 03:05 PM