Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.wit.cs.comp1050;
- import java.util.Scanner;
- /**
- * PA3b linear equation solver.
- * @author caushie
- * 02/15/2019
- */
- public class PA3b {
- /**
- * Error to display if the command-line arguments are invalid
- */
- public static final String ERR_USAGE = "Please supply 6 numbers (a-f).";
- /**
- * Error to display if the equation has no solution
- */
- public static final String ERR_NOSLTN = "The equation has no solution.";
- /**
- * Number of required parameters (a-f)
- */
- public static final int NUM_PARAMS = 6;
- /**
- * Validates command-line arguments and returns parameters if valid
- *
- * @param args command-line arguments
- * @return if valid an array of parameters, else null
- */
- public static double[] validateArgs(String[] args) {
- if (args.length < 6)
- return null;
- double[] myArr = new double[args.length];
- boolean isValid = false;
- for (int i = 0; i < args.length; ++i) {
- try {
- myArr[i] = Double.parseDouble(args[i]);
- } catch (NumberFormatException nfe) {
- return null;
- }
- }
- return myArr;
- }
- /**
- * Uses command-line arguments to create an instance of the linear equation, and
- * reports the outcome
- *
- * @param args command-line arguments, interpreted as equation parameters
- */
- public static void main(String[] args) {
- //We call validate args to validate our input and then do the according operations with the linear equation provided.
- if (validateArgs(args) != null) {
- LinearEquation equation = new LinearEquation(validateArgs(args));
- if (equation.isSolvable()) {
- System.out.printf("Solution: x=%.3f, y=%.3f%n", equation.getX(),equation.getY());
- }
- else {
- System.out.println(ERR_NOSLTN);
- }
- }else {
- System.out.println(ERR_USAGE);
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement