Sebastian1314

LinearEquation v3 by SbStudios

Dec 6th, 2011
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Linear Equation Creator v3 (C)SbStudios, 2011.
  2.  
  3. //NOW WITH LINEAR (WEIRD) STANDARD FORM!
  4.  
  5. public class LinearEquation
  6. {
  7.     protected double slope, yInt, x1, x2, y1, y2;
  8.    
  9.     protected Boolean hasPoints = false, hasSlope = false,hasYInt = false;
  10.    
  11.     public LinearEquation(double start_slope, double start_yInt)
  12.     {
  13.         slope = start_slope;
  14.         yInt = start_yInt;
  15.        
  16.         hasYInt = true;
  17.        
  18.         hasSlope = true;
  19.     }
  20.    
  21.     public LinearEquation(double start_x1, double start_x2, double start_y1, double start_y2)
  22.     {
  23.         x1 = start_x1;
  24.         x2 = start_x2;
  25.        
  26.         y1 = start_y1;
  27.         y2 = start_y2;
  28.        
  29.         hasPoints = true;
  30.     }
  31.    
  32.     public void calcSlope()
  33.     {
  34.         if (hasPoints)
  35.         {
  36.             slope = ((y2-y1)/(x2-x1));
  37.             hasSlope = true;
  38.         }
  39.        
  40.         else { System.out.println("Points haven't been given, slope not calculated."); }
  41.     }
  42.    
  43.     public void calcYInt()
  44.     {
  45.         if (hasPoints&&hasSlope)
  46.         {
  47.             yInt = y1 - (slope*x1);
  48.             hasYInt = true;
  49.         }
  50.        
  51.         else { System.out.println("ERROR! This doesn't have: Points OR Slope"); }
  52.     }
  53.    
  54.     public String getEquation()
  55.     {
  56.         if (hasYInt && hasSlope)
  57.         {
  58.             String desc = ("y="+slope+"x+"+yInt);
  59.             return desc;
  60.         }
  61.        
  62.         else
  63.         {
  64.             String descError = ("ERROR! This doesn't have: y Intercept OR Slope");
  65.             return descError;
  66.         }
  67.     }
  68.    
  69.     public String getLOLequation()
  70.     {
  71.         if (hasYInt && hasSlope)
  72.         {
  73.             double negativeYInt = -1*yInt;
  74.            
  75.             String desc = (slope+"x"+"-y="+negativeYInt);
  76.             return desc;
  77.         }
  78.        
  79.         else
  80.         {
  81.             String descError = ("ERROR! This doesn't have: y Intercept OR Slope");
  82.             return descError;
  83.         }
  84.     }
  85.    
  86.     public double getSlope() {return slope;}
  87.     public double getYInt() {return yInt;}
  88.     public double getX1() {return x1;}
  89.     public double getX2() {return x2;}
  90.     public double getY1() {return y1;}
  91.     public double getY2() {return y2;}
  92. }
  93.  
  94. //Linear Equation Creator v3 - BASIC TESTER (C)SbStudios, 2011.
  95.  
  96. import java.util.Scanner;
  97.  
  98. public class MainClass
  99. {
  100.     public static void main (String args[])
  101.     {
  102.         Scanner scan = new Scanner(System.in);
  103.        
  104.         System.out.println("LinEq Calc v2.0. Do you have:\n"
  105.                 + "\n"
  106.                 + "1) The slope and y Intercept\n"
  107.                 + "\n"
  108.                 + "2) x1, y1, and x2, y2\n");
  109.        
  110.         String option = scan.next();
  111.        
  112.         if (option.equals("1"))
  113.         {
  114.             System.out.println("Slope = ?");
  115.  
  116.             String initSlope = scan.next();
  117.            
  118.             //~~~~~~~~~~~~~~~~~~~~~~
  119.            
  120.             System.out.println("Y Intercept = ?");
  121.  
  122.             String initYInt = scan.next();
  123.            
  124.             //~~~~~~~~~~~~~~~~~~~~~~
  125.            
  126.             double slope = Double.parseDouble(initSlope);
  127.            
  128.             double yInt = Double.parseDouble(initYInt);
  129.            
  130.             //~~~~~~~~~~~~~~~~~~~~~~
  131.            
  132.             LinearEquation myEQ = new LinearEquation(slope,yInt);
  133.            
  134.             //~~~~~~~~~~~~~~~~~~~~~~
  135.            
  136.             String desc = myEQ.getEquation();
  137.            
  138.             System.out.println("Polynomial Standard Form Equation: "+desc);
  139.            
  140.             String desc2 = myEQ.getLOLequation();
  141.            
  142.             System.out.println("Standard Form Equation: "+desc2);
  143.            
  144.             //~~~~~~~~~~~~~~~~~~~~~~
  145.            
  146.             System.exit(0);
  147.         }
  148.        
  149.         else if (option.equals("2"))
  150.         {
  151.             System.out.println("x1 = ?");
  152.  
  153.             String initx1 = scan.next();
  154.            
  155.             //~~~~~~~~~~~~~~~~~~~~~~
  156.            
  157.             System.out.println("y1 = ?");
  158.  
  159.             String initY1 = scan.next();
  160.            
  161.             //~~~~~~~~~~~~~~~~~~~~~~
  162.            
  163.             System.out.println("x2 = ?");
  164.  
  165.             String initx2 = scan.next();
  166.            
  167.             //~~~~~~~~~~~~~~~~~~~~~~
  168.            
  169.             System.out.println("y2 = ?");
  170.  
  171.             String initY2 = scan.next();
  172.            
  173.             //~~~~~~~~~~~~~~~~~~~~~~
  174.  
  175.            
  176.             double x1 = Double.parseDouble(initx1);
  177.             double Y1 = Double.parseDouble(initY1);
  178.            
  179.             double x2 = Double.parseDouble(initx2);
  180.             double Y2 = Double.parseDouble(initY2);
  181.            
  182.             //~~~~~~~~~~~~~~~~~~~~~~
  183.            
  184.             LinearEquation myEQ = new LinearEquation(x1,x2,Y1,Y2);
  185.            
  186.             myEQ.calcSlope();
  187.             myEQ.calcYInt();
  188.            
  189.             //~~~~~~~~~~~~~~~~~~~~~~
  190.            
  191.             String desc = myEQ.getEquation();
  192.            
  193.             System.out.println("Polynomial Standard Form Equation: "+desc);
  194.            
  195.             String desc2 = myEQ.getLOLequation();
  196.            
  197.             System.out.println("Standard Form Equation: "+desc2);
  198.            
  199.             //~~~~~~~~~~~~~~~~~~~~~~
  200.            
  201.             System.exit(0);
  202.         }
  203.        
  204.         else
  205.         {
  206.             System.out.println("Please input a valid number (1 or 2). Quitting...");
  207.             System.exit(0);
  208.         }
  209.     }
  210. }
  211.  
Advertisement
Add Comment
Please, Sign In to add comment