Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. package edu.wit.cs.comp1050;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  *  PA3b linear equation solver.
  7.  * @author caushie
  8.  *  02/15/2019
  9.  */
  10. public class PA3b {
  11.  
  12.     /**
  13.      * Error to display if the command-line arguments are invalid
  14.      */
  15.     public static final String ERR_USAGE = "Please supply 6 numbers (a-f).";
  16.  
  17.     /**
  18.      * Error to display if the equation has no solution
  19.      */
  20.     public static final String ERR_NOSLTN = "The equation has no solution.";
  21.  
  22.     /**
  23.      * Number of required parameters (a-f)
  24.      */
  25.     public static final int NUM_PARAMS = 6;
  26.  
  27.     /**
  28.      * Validates command-line arguments and returns parameters if valid
  29.      *
  30.      * @param args command-line arguments
  31.      * @return if valid an array of parameters, else null
  32.      */
  33.     public static double[] validateArgs(String[] args) {
  34.         if (args.length < 6)
  35.             return null;
  36.  
  37.         double[] myArr = new double[args.length];
  38.  
  39.         boolean isValid = false;
  40.  
  41.         for (int i = 0; i < args.length; ++i) {
  42.             try {
  43.                 myArr[i] = Double.parseDouble(args[i]);
  44.             } catch (NumberFormatException nfe) {
  45.                 return null;
  46.             }
  47.  
  48.         }
  49.         return myArr;
  50.     }
  51.  
  52.     /**
  53.      * Uses command-line arguments to create an instance of the linear equation, and
  54.      * reports the outcome
  55.      *
  56.      * @param args command-line arguments, interpreted as equation parameters
  57.      */
  58.     public static void main(String[] args) {
  59.        
  60.    
  61. //We call validate args to validate our  input and then do the according operations with the linear equation provided.
  62.         if (validateArgs(args) != null) {
  63.             LinearEquation equation = new LinearEquation(validateArgs(args));
  64.            
  65.             if (equation.isSolvable()) {
  66.                 System.out.printf("Solution: x=%.3f, y=%.3f%n", equation.getX(),equation.getY());
  67.             }
  68.             else {
  69.                 System.out.println(ERR_NOSLTN);
  70.             }
  71.         }else {
  72.             System.out.println(ERR_USAGE);
  73.             System.exit(0);
  74.         }
  75.    
  76.  
  77.  
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement