Sebastian1314

LinearEquation by SbStudios

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