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

Discussion in 'Programming' started by Orion, Nov 5, 2009.

Remove these ads by signing in
Remove these ads by signing in
Thread Status:
Not open for further replies.
  1. Orion New Member

    Member Since:
    Sep 29, 2009
    Message Count:
    969
    Likes Received:
    25
    Location:
    http://admin-spot.com
    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
  2. proweb New Member

    Member Since:
    Jul 22, 2009
    Message Count:
    1,308
    Likes Received:
    44
    Occupation:
    Joomla Designer
    Location:
    Joomla Expert
    I'm not familiar with javascript, maybe you can get more help on a dedicated javascript forum.
  3. Orion New Member

    Member Since:
    Sep 29, 2009
    Message Count:
    969
    Likes Received:
    25
    Location:
    http://admin-spot.com
    ...it's normal java lol (read the title). And I've figured it out and my program is working beautifully! :D
Thread Status:
Not open for further replies.

Share This Page