Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Linear Equation Creator v3 (C)SbStudios, 2011.
- //NOW WITH LINEAR (WEIRD) STANDARD FORM!
- public class LinearEquation
- {
- protected double slope, yInt, x1, x2, y1, y2;
- protected Boolean hasPoints = false, hasSlope = false,hasYInt = false;
- public LinearEquation(double start_slope, double start_yInt)
- {
- slope = start_slope;
- yInt = start_yInt;
- hasYInt = true;
- hasSlope = true;
- }
- public LinearEquation(double start_x1, double start_x2, double start_y1, double start_y2)
- {
- x1 = start_x1;
- x2 = start_x2;
- y1 = start_y1;
- y2 = start_y2;
- hasPoints = true;
- }
- public void calcSlope()
- {
- if (hasPoints)
- {
- slope = ((y2-y1)/(x2-x1));
- hasSlope = true;
- }
- else { System.out.println("Points haven't been given, slope not calculated."); }
- }
- public void calcYInt()
- {
- if (hasPoints&&hasSlope)
- {
- yInt = y1 - (slope*x1);
- hasYInt = true;
- }
- else { System.out.println("ERROR! This doesn't have: Points OR Slope"); }
- }
- public String getEquation()
- {
- if (hasYInt && hasSlope)
- {
- String desc = ("y="+slope+"x+"+yInt);
- return desc;
- }
- else
- {
- String descError = ("ERROR! This doesn't have: y Intercept OR Slope");
- return descError;
- }
- }
- public String getLOLequation()
- {
- if (hasYInt && hasSlope)
- {
- double negativeYInt = -1*yInt;
- String desc = (slope+"x"+"-y="+negativeYInt);
- return desc;
- }
- else
- {
- String descError = ("ERROR! This doesn't have: y Intercept OR Slope");
- return descError;
- }
- }
- public double getSlope() {return slope;}
- public double getYInt() {return yInt;}
- public double getX1() {return x1;}
- public double getX2() {return x2;}
- public double getY1() {return y1;}
- public double getY2() {return y2;}
- }
- //Linear Equation Creator v3 - BASIC TESTER (C)SbStudios, 2011.
- import java.util.Scanner;
- public class MainClass
- {
- public static void main (String args[])
- {
- Scanner scan = new Scanner(System.in);
- System.out.println("LinEq Calc v2.0. Do you have:\n"
- + "\n"
- + "1) The slope and y Intercept\n"
- + "\n"
- + "2) x1, y1, and x2, y2\n");
- String option = scan.next();
- if (option.equals("1"))
- {
- System.out.println("Slope = ?");
- String initSlope = scan.next();
- //~~~~~~~~~~~~~~~~~~~~~~
- System.out.println("Y Intercept = ?");
- String initYInt = scan.next();
- //~~~~~~~~~~~~~~~~~~~~~~
- double slope = Double.parseDouble(initSlope);
- double yInt = Double.parseDouble(initYInt);
- //~~~~~~~~~~~~~~~~~~~~~~
- LinearEquation myEQ = new LinearEquation(slope,yInt);
- //~~~~~~~~~~~~~~~~~~~~~~
- String desc = myEQ.getEquation();
- System.out.println("Polynomial Standard Form Equation: "+desc);
- String desc2 = myEQ.getLOLequation();
- System.out.println("Standard Form Equation: "+desc2);
- //~~~~~~~~~~~~~~~~~~~~~~
- System.exit(0);
- }
- else if (option.equals("2"))
- {
- System.out.println("x1 = ?");
- String initx1 = scan.next();
- //~~~~~~~~~~~~~~~~~~~~~~
- System.out.println("y1 = ?");
- String initY1 = scan.next();
- //~~~~~~~~~~~~~~~~~~~~~~
- System.out.println("x2 = ?");
- String initx2 = scan.next();
- //~~~~~~~~~~~~~~~~~~~~~~
- System.out.println("y2 = ?");
- String initY2 = scan.next();
- //~~~~~~~~~~~~~~~~~~~~~~
- double x1 = Double.parseDouble(initx1);
- double Y1 = Double.parseDouble(initY1);
- double x2 = Double.parseDouble(initx2);
- double Y2 = Double.parseDouble(initY2);
- //~~~~~~~~~~~~~~~~~~~~~~
- LinearEquation myEQ = new LinearEquation(x1,x2,Y1,Y2);
- myEQ.calcSlope();
- myEQ.calcYInt();
- //~~~~~~~~~~~~~~~~~~~~~~
- String desc = myEQ.getEquation();
- System.out.println("Polynomial Standard Form Equation: "+desc);
- String desc2 = myEQ.getLOLequation();
- System.out.println("Standard Form Equation: "+desc2);
- //~~~~~~~~~~~~~~~~~~~~~~
- System.exit(0);
- }
- else
- {
- System.out.println("Please input a valid number (1 or 2). Quitting...");
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment