How do I show the linear composition of two linear equations in Java?(NOT javascript)

Discussion in 'Programming' started by Black Fire, Nov 4, 2009.

  1. #1
    Ok, so I want to be able to show the composition of two linear equations for a program I'm working on. Everything is DONE except for this. In the program, there are two classes. One is the LinearEquation class, and the other is the TestLinearEquation class. For the first linear equation (found by the slope and a point), a user enters in the values) for the second linear equation (found with two points) I hard coded that one so it's made inside of the program.

    Here's my code:

    //LINEAR EQUATION CLASS
    public class LinearEquation {
    //Declare variables used (slope and y-intercept)
    private double slope;
    private double yInt;

    //"specification" of variables
    public LinearEquation(double slope, double yInt)
    {
    this.slope = slope;
    this.yInt = yInt;
    }
    //define what the slope equals and what the y-intercept equals
    public LinearEquation(double xValue, double yValue, double xValue2, double yValue2)
    {
    slope = (yValue2 - yValue) / (xValue2 - xValue);

    yInt = yValue - (slope * xValue);
    }
    //given an x value, returns appropriate y value
    public double xToY(double x)
    {
    double y = (slope * x) + yInt;

    return y;
    }
    //given a y value, returns appropraite x value
    public double ytoX(double y)
    {
    double x = (y - yInt) / slope;

    return x;
    }
    //find the inverse of the equation
    public LinearEquation inverse()
    {
    double inverseSlope = 1/slope;

    double inverseYInt = -yInt/slope;

    LinearEquation inverseEquation = new LinearEquation(inverseSlope, inverseYInt);

    return inverseEquation;

    }
    //display equation as slope-intercept form
    public String toString()
    {
    return ("y = " + slope + "x + " + yInt);
    }




    }

    //////////////////////////////////////…


    //TEST LINEAR EQUATION CLASS
    import java.util.Scanner;
    public class TestLinearEquation {

    //method for the equation with slope and point given
    public static void slopePoint()
    {
    //Ask for and receive slope for the equation
    System.out.println("Please enter the slope for your equation.");
    Scanner s = new Scanner(System.in);
    double slope = s.nextDouble();

    //Ask for and receive y-intercept fore the equation
    System.out.println("Please enter the y-intercept for your equation.");
    double yInt = s.nextDouble();

    //Constructor
    LinearEquation equation1 = new LinearEquation(slope, yInt);

    //Ask for and receive the x value for the point
    System.out.println("Please enter an x value.");
    double x1 = s.nextDouble();

    //Ask for and receive the y value
    double point1y = equation1.xToY(x1);
    System.out.println("Y value: " + point1y);

    //print the equation
    System.out.println("and the equation is " + equation1);

    }
    //method for the equation with two points given
    public static void pointPoint()
    {
    //constructor
    LinearEquation equation2 = new LinearEquation(1,2,3,7);

    //equation with two points
    double point2y = equation2.xToY(5);
    System.out.println("For an equation through (1,2) and (3,7) with an x value of 5,");
    System.out.println("Y value: " + point2y);
    System.out.println("and the equation is " + equation2);

    //give x value when a y value is given
    double point3x = equation2.ytoX(15);
    System.out.println("For the same equation, with a y value of 15,");
    System.out.println("X value: " + point3x);

    //inverse of equation
    LinearEquation equationInverse = equation2.inverse();
    double inverseY = equationInverse.xToY(12);
    System.out.println("For the inverse linear equation, and given an x value of 12,");
    System.out.println("Y value: " + inverseY);

    }
    /**
    * Method main
    *
    *
    * @param args
    *
    */
    public static void main(String[] args) {
    //Call methods

    slopePoint();
    pointPoint();



    }
    }


    Please let me know if I need to fix any comments as well :). You're help is VERY much appreciated!
     
    Black Fire, Nov 4, 2009 IP
  2. Black Fire

    Black Fire Well-Known Member

    Messages:
    804
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    130
    #2
    Note: I think this is how to do it, but I'm not sure, and it hasn't worked for me:
    public LinearEquation composition(LinearEquation inner)
    {
    LinearEquation a,b;
    a.composition(b);
    }

    What I want to know is how to fix this with my program.
     
    Black Fire, Nov 4, 2009 IP