# Thread: Passing variables to methods

1. Member
Join Date
May 2012
Posts
2
Rep Power
0

## Passing variables to methods

I a working on a program to calculate the length of sides of a triangle and some other trig functions (that are not important to my question). I am to display the coordinates of three points, which are given. I have gotten this portion down. I have the equations for determining the distance. I do not understand how to get the coordinates that were inputted to pass to my other method that is performing the calculation.(All calculations must have their own method.) Thank you in advance for any pointers or references that might clear up my confusion. The answer i should be getting for the side calculations with an input of (0,0) , (3,2) , (2,5) is:
3.60555
3.16228
5.38516

This is my first time posting I apologize for any incorrect form.

//Start Program
package MathInput;

import java.util.Scanner; //calls the class for data input

public class Triangle
{ //Begin Triangle Class

private
double x1; //Variable declaration for coordinate x1
double x2; //Variable declaration for coordinate x2
double x3; //Variable declaration for coordinate x3
double y1; //Variable declaration for coordinate y1
double y2; //Variable declaration for coordinate y1
double y3; //Variable declaration for coordinate y1

public Triangle()
{ //Begin Triangle constructor

} //End Triangle constructor

public void EnterCoordinates()
{ //Begin Enter Coordinates Method
Scanner in = new Scanner(System.in); // for data input

System.out.print("Enter the x coordinate for point 1: "); //Asks user for input
double x1 = in.nextDouble(); //Saves input to the variable
System.out.print("Enter the y coordinate for point 1: "); //Asks user for input
double y1 = in.nextDouble(); //Saves input to the variable
System.out.print("Enter the x coordinate for point 2: "); //Asks user for input
double x2 = in.nextDouble(); //Saves input to the variable
System.out.print("Enter the y coordinate for point 2: "); //Asks user for input
double y2 = in.nextDouble(); //Saves input to the variable
System.out.print("Enter the x coordinate for point 3: "); //Asks user for input
double x3 = in.nextDouble(); //Saves input to the variable
System.out.print("Enter the y coordinate for point 3: "); //Asks user for input
double y3 = in.nextDouble(); //Saves input to the variable

System.out.println(); //Spacing
System.out.print("(x1,y1)= "); //Prints out coordinates entered
System.out.print(" (");
System.out.print(x1);
System.out.print(",");
System.out.print(y1);
System.out.print(")");

System.out.println(); //Spacing
System.out.print("(x2,y2)= "); //Prints out coordinates entered
System.out.print(" (");
System.out.print(x2);
System.out.print(",");
System.out.print(y2);
System.out.print(")");

System.out.println(); //Spacing
System.out.print("(x2,y2)= "); //Prints out coordinates entered
System.out.print(" (");
System.out.print(x3);
System.out.print(",");
System.out.print(y3);
System.out.println(")");

} //End EnterCoordinate Method

public void Sidelength()
{ //Begin Sidelength Method

double dist1, dist2, dist3; //Variable declaration for calculating distances
dist1 = Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2)); //Calculates distances
dist2 = Math.sqrt(Math.pow(x1-x3,2) + Math.pow(y1-y3,2)); //Calculates distances
dist3 = Math.sqrt(Math.pow(x2-x3,2) + Math.pow(y2-y3,2)); //Calculates distances

System.out.println(dist1); //prints out distance
System.out.println(dist2); //Prints out distance
System.out.println(dist3); //Prints out distance
} //End sidelength Method
} //End Triangle class

//Tester Program

package MathInput;

public class Triangletester
{ //Begin Trriangletester class
public static void main(String[] args)
{ //Begin Main

Triangle coordinates = new Triangle(); //creates new instance in the Triangle class
coordinates.EnterCoordinates(); //Calls the Enter Coordinates Method to input coordinate numbers
coordinates.Sidelength(); //Calls the side length Method to calculate the sides.

} //End Main

} //End Triangletester Class

//Output

Enter the x coordinate for point 1: 0
Enter the y coordinate for point 1: 0
Enter the x coordinate for point 2: 3
Enter the y coordinate for point 2: 2
Enter the x coordinate for point 3: 2
Enter the y coordinate for point 3: 5

(x1,y1)= (0.0,0.0)
(x2,y2)= (3.0,2.0)
(x2,y2)= (2.0,5.0)
0.0
0.0
0.0

2. ## Re: Passing variables to methods

Your local variables x1, x2, etc. shadow the member variables x1, x2, etc. (the cure is simple: remove those local variables).

kind regards,

Jos

4. Member
Join Date
May 2012
Posts
2
Rep Power
0

## Re: Passing variables to methods

Jos,

Thank you for your quick response. Being new to java and programing it took me a little bit to realize I was designating local variable in my Sidelength class because I put the double in front of them. Thank you for the help to my simplistic problem. This forum is a valuable resource as is your input.

Kyle

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•