Okay so I'm supposed to be doing an program where the user types in the circles points and the center of the circle...then make a new method for each of the things calculated like distance and area and radius...how do I bring what I did for a method back to method main? This is what I have thus far...

import java.io.*;
import java.util.*;
import static java.lang.Math.*;

public class Circle
{
	static Scanner console = new Scanner(System.in);
	
	static final double PI = 3.1416;

	public static void main(String[] args)
	{
	
		double x1, x2, y1, y2;
	
		System.out.print("Enter the X of the center of a circle.: ");
		x1 = console.nextDouble();

		System.out.print("Enter the Y of the center of a circle.: ");
		y1 = console.nextDouble();

		System.out.print("Enter the X of a point on the circle.: ");
		x2 = console.nextDouble();

		System.out.print("Enter the Y of a point on the circle.: ");
		y2 = console.nextDouble();
		
		System.out.print("The distance of the points is: " + distance(z));
	
	}
	public static double distance (double x1, double y1, double x2, double y2, double z, double y3, double x3)
	{		

		y3 = (y2 - y1);
		y3 = Math.pow(y3, 2);
		x3 = (x2 - x1);
		x3 = Math.pow(x3, 2);
		z = Math.sqrt(y3 + x3);
		return z;

		
	}
}

Call the function like this:

resultValue = myFunction(arg1, arg2. arg3);

Put a return statement in the function as the last statement:

function myFunction(valu1, valu2, valu3){
  var theAnswer;

  ...

  theAnswer =  [I]your last calculation[/I];
  return theAnswer;
};

I just realized that you are not writing in JavaScript. My answer is for JavaScript. Take your question to the forum for the language you are really using.

Hey there, I'll just give you a few pointers here:

public static double distance (double x1, double y1, double x2, double y2, double z, double y3, double x3)
{
y3 = (y2 - y1);
y3 = Math.pow(y3, 2);
x3 = (x2 - x1);
x3 = Math.pow(x3, 2);
z = Math.sqrt(y3 + x3);
return z;
}
}

This metod distance takes a ( double, double, double, double, double, double, double ), that's 7 doubles! A couple of points on this here:

  1. You only read in 4 in your program: x1, y1, x2, y2... where do z1 and z2 come from???
  2. You call System.out.println( distance( z ) ); in your main method, you have no z variable, and you're only supplying one argument!

1. I'm taking it that y1 and y2 are the points on the circumference of the circle?? and x1 and x2 is the center of the circle. As you only read in 4 points in the circle, and these are the one's you are working on, you should have a method:

distance( double _x1, double _x2, double _y1, double _y2 )
{
   //If you have a center and a point on the circumference of the triangle we use pythagoras theorem to calculate the radius, a = root( b^2 + c^2 )
  //We'll define a new double rad for this
   double rad = 0;
   //Each side is equal to the x point - the y point
   //It doesn't matter if the above returns a minus result as it is being squared.
   rad = Math.sqrt( Math.pow( _x1 - _y1, 2 ) + Math.pow( _x2 - _y2, 2 ) );
   return rad; //Return the radius
}

Notice that it's more streamline now! It can also simply be written as:

distance( double _x1, double _x2, double _y1, double _y2 )
{
   return Math.sqrt( Math.pow( _x1 - _y1, 2 ) + Math.pow( _x2 - _y2, 2 ) );
}

Do what ever works for you, and ALWAYS ALWAY ALWAYS remember to indent and comment your code.

2. As distance now only takes 4 parameters it should be called as follows:

...
    public static void main ( String [] args )
   {
       //Read yer points...
       System.out.println( distance( x1, x2, y1, y2 ) );
   }
...

just remember that when you call a method with in a class you need to give the same number of variables that you declare on the method's name

{
int y = distance(x1, x2, ...., x(n-1), xn);
}


public int distance(int x1, int x2, ...., int x(n-1), int xn)
{
int z;
return z;
}

jut remember the order that you declare the variables and that whether they are int, boolean, char...they have to be in the same order as well as the variable should be the same type as the method and the value or variable that the method returns

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.