Link to the Java code and JUnit Test: https://drive.google.com/open?id=0BwuDVy83
ID: 3585841 • Letter: L
Question
Link to the Java code and JUnit Test:
https://drive.google.com/open?id=0BwuDVy83jWPuYk8wc1lxOWpQcjQ
Problem b (PA3b.java) You are to first implement a class named LinearEquation for a 2x2 set of linear equations: ax + by- e cx+dy = f ed-bf ad - bc x= af - ec ad-bc y= The class has two constructors to provide the equation parameters (a-f), six getter methods for these parameters, a method to determine if the linear system is solvable (i.e. whether the denominator of the x/y equations equals o), and methods to get x/y (or null, if the system isn't solvable) You must also write a program that takes the parameters (a-f) via command-line arguments, validates the input, and outputs either an error (due to invalid inputs or a non-solvable system) or the solution (with three decimal places of precision). For example, consider the following runs... $ java edu.wit.cs.comp1050.PA3b Please supply 6 numbers (a-f). $ java edu . wit . cs.complese . PA3b 1.0 2.0 2.0 4.0 4.0 5.0 The equation has no solution. $ java edu.wit.cs.complese . PA3b 9. 4.0 3.0-5.0-6.0-21 , Solution: x--2.e0e, y-3.00eExplanation / Answer
Answer :
LinearEquation.java
public class LinearEquation {
double a,b,c,d,e,f;
public LinearEquation(double a, double b, double c, double d, double e, double f) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
}
/**
* Convenience constructor to initialize
* the linear equation via array
*
* THIS CONSTRUCTOR CALLS THE CONSTRUCTOR
* ABOVE USING THE ARRAY CONTENTS
*
* @param p parameter array, assumed to be length 6 (a-f, in order)
*/
public LinearEquation(double[] p) {
// MUST call the above constructor
// with the contents of p
this(p[0],p[1],p[2],p[3],p[4],p[5]);
}
public double getA() {
return a;
}
public double getB() {
return b;
}
public double getC() {
return c;
}
public double getD() {
return d;
}
public double getE() {
return e;
}
public double getF() {
return f;
}
/**
* Returns true if the parameterized
* equation is solvable (i.e. denominator
* ad-bc is not 0)
*
* @return true if solvable, false otherwise
*/
public boolean isSolvable() {
if((a*d-b*c) != 0)
return true;
else
return false;
}
/**
* Returns solution for x if solvable,
* null otherwise
*
* @return x if solvable, null otherwise
*/
public Double getX() {
double x;
if(isSolvable())
{
x = ((e*d)-(b*f))/((a*d)-(b*c));
return x;
}
else
return null;
}
/**
* Returns solution for y if solvable,
* null otherwise
*
* @return y if solvable, null otherwise
*/
public Double getY() {
double y;
if(isSolvable())
{
y = ((a*f)-(e*c))/((a*d)-(b*c));
return y;
}
else
return null;
}
}
PA3b.java
//package edu.wit.cs.comp1050;
//TODO: document this class
import java.util.Scanner;
import java.util.Arrays;
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) {
try
{
double[] array = Arrays.asList(args).stream().mapToDouble(Double::parseDouble).toArray();
return array;
}
catch(NumberFormatException ex)
{
}
return null;
//Hint: Try to catch the NumberFormatException exception here.
//Catch: You don't have execute anything in the catch.
//Try: 1) check if correct number of params is passed
// 2) if so: convert each value from String to Double
// and store in an array
}
/**
* 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)throws Exception {
String[] num = new String[6];
double x,y;
//LinearEquation lineq = new LinearEquation();
Scanner s = new Scanner(System.in);
System.out.println(ERR_USAGE);
for(int i=0;i<6;i++)
{
num[i] = s.nextLine();
}
double[] n = validateArgs(num);
LinearEquation lineq = new LinearEquation(n);
if(lineq.isSolvable())
{
x = lineq.getX();
y = lineq.getY();
System.out.println("x = "+x+", y = "+y);
}
else
System.out.println(ERR_NOSLTN);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.