Results 1 to 4 of 4
Thread: Passing variables to methods
 05182012, 09:27 AM #1Member
 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(x1x2,2) + Math.pow(y1y2,2)); //Calculates distances
dist2 = Math.sqrt(Math.pow(x1x3,2) + Math.pow(y1y3,2)); //Calculates distances
dist3 = Math.sqrt(Math.pow(x2x3,2) + Math.pow(y2y3,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
 05182012, 09:33 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,773
 Blog Entries
 7
 Rep Power
 21
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,
Joscenosillicaphobia: the fear for an empty beer glass
 05182012, 12:31 PM #3
Re: Passing variables to methods
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
 05212012, 01:40 AM #4Member
 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
Similar Threads

Passing variables from one class to another
By nhmllr in forum New To JavaReplies: 11Last Post: 08092011, 01:43 AM 
Passing variables through classes?
By EternalFacepalm in forum New To JavaReplies: 4Last Post: 04272011, 01:46 AM 
please help with passing variables between class's
By jasonwucinski in forum New To JavaReplies: 4Last Post: 02112011, 02:27 AM 
Passing dynamic variables between classes
By Brekk in forum New To JavaReplies: 1Last Post: 03222010, 07:07 AM 
passing variables to another frame
By Grom in forum New To JavaReplies: 10Last Post: 11092008, 06:55 PM
Bookmarks