Results 1 to 4 of 4
  1. #1
    KKorn429 is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Passing variables to methods

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    KKorn429 is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default 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

  1. Passing variables from one class to another
    By nhmllr in forum New To Java
    Replies: 11
    Last Post: 08-09-2011, 12:43 AM
  2. Passing variables through classes?
    By EternalFacepalm in forum New To Java
    Replies: 4
    Last Post: 04-27-2011, 12:46 AM
  3. please help with passing variables between class's
    By jasonwucinski in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 01:27 AM
  4. Passing dynamic variables between classes
    By Brekk in forum New To Java
    Replies: 1
    Last Post: 03-22-2010, 06:07 AM
  5. passing variables to another frame
    By Grom in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 05:55 PM

Posting Permissions

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