Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************
- * @file GA2Math.java
- * Answers for GA.2
- * @author hypesystem
- */
- package ga2math;
- /**
- * This is a class to handle math. Specifically math from GA.2.
- * Basically it uses the main class to run the GA2Math class to run the Quadratic class.
- * This is to keep the Quadratic class as clean as possible, and make the application
- * executable. True story.
- * @author hypesystem
- */
- public class GA2Math {
- private Quadratic doMath;
- private Quadratic doMath2;
- private Quadratic doMath3;
- /**
- * Creates objects of the Quadratic-class and does stuff with them!
- */
- public GA2Math() {
- doMath = new Quadratic(3,9,-30);
- doMath.show();
- System.out.println("x = 3; y = " + doMath.compute(3));
- doMath.solve();
- System.out.println("");
- doMath2 = new Quadratic(-3,1,-1);
- doMath2.show();
- System.out.println("x = 3; y = " + doMath2.compute(3));
- doMath2.solve();
- System.out.println("");
- doMath3 = new Quadratic(10,3,-5);
- doMath3.show();
- System.out.println("x = 4; y = " + doMath3.compute(4));
- doMath3.solve();
- }
- /**
- * Instantiates the application.
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- GA2Math doTheMath = new GA2Math();
- }
- }
- /******************************
- * @file Quadratic.java
- * Answers for GA.2
- * @author hypesystem
- */
- package ga2math;
- import java.text.*;
- /**
- * This class holds a quadratic that can be solved, shown and calculated for a
- * specific value of x.
- * @author hypesystem
- */
- public class Quadratic {
- private double a;
- private double b;
- private double c;
- private NumberFormat nf = NumberFormat.getInstance();
- /**
- * Creates a Quadratic of the format ax^2+bx+c with the given values.
- * @param a
- * @param b
- * @param c
- */
- public Quadratic(double a, double b, double c) {
- this.a = a;
- this.b = b;
- this.c = c;
- nf.setMaximumFractionDigits(2);
- }
- /**
- * Inputs the values of a, b and c into the format of a quadratic,
- * checks whether a > 0 (if it is a quadratic, or a linear function,
- * then prints.
- */
- public void show() {
- String print = "";
- if(a != 0) {
- print = "Andengradspolynomie:\n";
- }
- else {
- print = "Lineær funktion:\n";
- }
- print = print + nf.format(a) + "x^2";
- if(b >= 0) {
- print = print + "+";
- }
- print = print + nf.format(b) + "x";
- if(c >= 0) {
- print = print + "+";
- }
- print = print + nf.format(c);
- System.out.println(print);
- }
- /**
- * Calculates quadratic with given value of x
- * @param x
- * @return formatted number with max. 2 decimals as String
- */
- public String compute(double x) {
- return nf.format((a * x * x) + (b * x) + c);
- }
- /**
- * Solves the quadratic by finding all solutions, if any. If it is
- * a linear function, it is solved as such.
- * Note: solve, superSolve & robustSolve combined.
- */
- public void solve() {
- String print = "";
- if(a != 0) {
- double d = (b * b) - (4 * a * c);
- if(d < 0) {
- print = "Ingen løsninger.";
- }
- else if(d == 0) {
- double solve = (0 - b + Math.sqrt(d)) / (2 * a);
- print = "Én løsning: " + solve;
- }
- else if(d > 0) {
- double solve1 = (0 - b + Math.sqrt(d)) / (2 * a);
- double solve2 = (0 - b - Math.sqrt(d)) / (2 * a);
- print = "To løsninger: " + nf.format(solve1) + " og " + nf.format(solve2);
- }
- }
- else if (b != 0) {
- double solve = (0 - c / b);
- print = "Løsning: " + nf.format(solve);
- }
- else {
- if(c == 0) {
- print = "Løsning: alle x";
- }
- else {
- print = "Ingen løsninger";
- }
- }
- System.out.println(print);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment