hypesystem

GA.2

Sep 8th, 2011
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. /******************************
  2.  * @file GA2Math.java
  3.  * Answers for GA.2
  4.  * @author hypesystem
  5.  */
  6.  
  7. package ga2math;
  8.  
  9. /**
  10.  * This is a class to handle math. Specifically math from GA.2.
  11.  * Basically it uses the main class to run the GA2Math class to run the Quadratic class.
  12.  * This is to keep the Quadratic class as clean as possible, and make the application
  13.  * executable. True story.
  14.  * @author hypesystem
  15.  */
  16. public class GA2Math {
  17.    
  18.     private Quadratic doMath;
  19.     private Quadratic doMath2;
  20.     private Quadratic doMath3;
  21.  
  22.     /**
  23.      * Creates objects of the Quadratic-class and does stuff with them!
  24.      */
  25.     public GA2Math() {
  26.         doMath = new Quadratic(3,9,-30);
  27.         doMath.show();
  28.         System.out.println("x = 3; y = " + doMath.compute(3));
  29.         doMath.solve();
  30.         System.out.println("");
  31.         doMath2 = new Quadratic(-3,1,-1);
  32.         doMath2.show();
  33.         System.out.println("x = 3; y = " + doMath2.compute(3));
  34.         doMath2.solve();
  35.         System.out.println("");
  36.         doMath3 = new Quadratic(10,3,-5);
  37.         doMath3.show();
  38.         System.out.println("x = 4; y = " + doMath3.compute(4));
  39.         doMath3.solve();
  40.     }
  41.    
  42.     /**
  43.      * Instantiates the application.
  44.      * @param args the command line arguments
  45.      */
  46.     public static void main(String[] args) {
  47.         GA2Math doTheMath = new GA2Math();
  48.     }
  49. }
  50.  
  51. /******************************
  52.  * @file Quadratic.java
  53.  * Answers for GA.2
  54.  * @author hypesystem
  55.  */
  56.  
  57. package ga2math;
  58.  
  59. import java.text.*;
  60.  
  61. /**
  62.  * This class holds a quadratic that can be solved, shown and calculated for a
  63.  * specific value of x.
  64.  * @author hypesystem
  65.  */
  66. public class Quadratic {
  67.    
  68.     private double a;
  69.     private double b;
  70.     private double c;
  71.     private NumberFormat nf = NumberFormat.getInstance();
  72.    
  73.     /**
  74.      * Creates a Quadratic of the format ax^2+bx+c with the given values.
  75.      * @param a
  76.      * @param b
  77.      * @param c
  78.      */
  79.     public Quadratic(double a, double b, double c) {
  80.         this.a = a;
  81.         this.b = b;
  82.         this.c = c;
  83.         nf.setMaximumFractionDigits(2);
  84.     }
  85.    
  86.     /**
  87.      * Inputs the values of a, b and c into the format of a quadratic,
  88.      * checks whether a > 0 (if it is a quadratic, or a linear function,
  89.      * then prints.
  90.      */
  91.     public void show() {
  92.         String print = "";
  93.         if(a != 0) {
  94.             print = "Andengradspolynomie:\n";
  95.         }
  96.         else {
  97.             print = "Lineær funktion:\n";
  98.         }
  99.         print = print + nf.format(a) + "x^2";
  100.         if(b >= 0) {
  101.             print = print + "+";
  102.         }
  103.         print = print + nf.format(b) + "x";
  104.         if(c >= 0) {
  105.             print = print + "+";
  106.         }
  107.         print = print + nf.format(c);
  108.         System.out.println(print);
  109.     }
  110.    
  111.     /**
  112.      * Calculates quadratic with given value of x
  113.      * @param x
  114.      * @return formatted number with max. 2 decimals as String
  115.      */
  116.     public String compute(double x) {
  117.         return nf.format((a * x * x) + (b * x) + c);
  118.     }
  119.    
  120.     /**
  121.      * Solves the quadratic by finding all solutions, if any. If it is
  122.      * a linear function, it is solved as such.
  123.      * Note: solve, superSolve & robustSolve combined.
  124.      */
  125.     public void solve() {
  126.         String print = "";
  127.         if(a != 0) {
  128.             double d = (b * b) - (4 * a * c);
  129.             if(d < 0) {
  130.                 print = "Ingen løsninger.";
  131.             }
  132.             else if(d == 0) {
  133.                 double solve = (0 - b + Math.sqrt(d)) / (2 * a);
  134.                 print = "Én løsning: " + solve;
  135.             }
  136.             else if(d > 0) {
  137.                 double solve1 = (0 - b + Math.sqrt(d)) / (2 * a);
  138.                 double solve2 = (0 - b - Math.sqrt(d)) / (2 * a);
  139.                 print = "To løsninger: " + nf.format(solve1) + " og " + nf.format(solve2);
  140.             }
  141.         }
  142.         else if (b != 0) {
  143.             double solve = (0 - c / b);
  144.             print = "Løsning: " + nf.format(solve);
  145.         }
  146.         else {
  147.             if(c == 0) {
  148.                 print = "Løsning: alle x";
  149.             }
  150.             else {
  151.                 print = "Ingen løsninger";
  152.             }
  153.         }
  154.         System.out.println(print);
  155.     }
  156.    
  157. }
Advertisement
Add Comment
Please, Sign In to add comment