I am new to java, and to programming as a whole. trying to become a self taught programmer. i read books and solve exercise. the issue i face is lack of feedback. i feel it is a lonely journey. is there any place i can get feedback of codes that i write? as of now i appreciate any comments on this simple program i wrote to solve quadratic equations:
/*
* Quadratic.java
* ------------------
* calculates the solutions of a quadratic equation (a*x^2 + b*x + c).
* this program calculates only real solutions. constant a is assembly nonzero.
*/
// to use input and output tools
import java.util.Scanner;
public class Quadratic {
/**
* prompt user to enter a-b-c values, computes solutions, display result
*/
public static void main(String [] args) {
// create scanner object to read values from user
Scanner input = new Scanner(System.in);
// prompt user to enter quadratic equation constants values (a,b,c)
System.out.println("Enter coefficients for the quadratic equation:");
// enter constant 'a' value
System.out.println("a: ");
double a = input.nextDouble();
// enter constant 'b' value
System.out.println("b: ");
double b = input.nextDouble();
// enter constant 'c' value
System.out.println("c: ");
double c = input.nextDouble();
// calculate quadratic equation solution
quadraticEquationSolver(a,b,c);
}
/**
* calculates quadratic equation solutions and print it to screen
* this method calculates only real solutions, in cases where that is not
* the case, a message to that effect is displayed on screen.
*/
private void quadraticEquationSolver(double a, double b, double c) {
// calculate square delta. delta = b^2 - 4*a*c
double deltaSquare = Math.pow(b, 2) - (4 * a *c);
// check if deltaSquare is a negative or a non-negative
if(deltaSquare < 0) {
// the equation has no real solutions
System.out.println("The equation has no real solutions");
return;
}else {
// calculate delat = sqrt (squareDelta)
double delta = Math.sqrt(deltaSquare);
// calculate first solution
double x1 = (-b + delta) / (2 * a);
// calculate second solution
double x2 = (-b - delta) / (2 * a);
// display result, each solution on different line
System.out.println("The first solution is " + x1);
System.out.println("The second solution is " + x2);
// in case solutions are equal, point it out
if (x1 == x2) System.out.println("both solutions are equal");
}
}
}
- comments? is it too much? or just not the right way or words?
- return statement used in method quadraticEquationSolver, inside if statement then clause? is it ok? bad? any other alternative?
-
if else statement, it could have been a cascaded if else statement like:
if(deltaSquare < 0) { // the equation has no real solutions System.out.println("The equation has no real solutions"); return; }else if (deltaSquare == 0){ // solutions are equal double solution = (-b ) / (2 * a); // display solution System.out.println("The first solution = The second solution = " + solution); } else { // calculate delat = sqrt (squareDelta) double delta = Math.sqrt(deltaSquare); // calculate first solution double x1 = (-b + delta) / (2 * a); // calculate second solution double x2 = (-b - delta) / (2 * a); // display result, each solution on different line System.out.println("The first solution is " + x1); System.out.println("The second solution is " + x2); }
which approach is better, the detailed cascading if statement ? or the brief more general simple if else statement? , in terms of good software engineering.
- method return: here i faced two challenges. 1) in then clause of if else statement, method should not return any value. how to handle a situation where in its most cases the method should return a value but in one or two case, it does not return anything? 2) in other cases the solution involves two values which is not possible for a method to return two values. i got around this situation by declaring a non return method which just communicates the results to the user directly. is it too bad to do that? or is it common ?
lastly, any comments you have to improve? from any point of view
Aucun commentaire:
Enregistrer un commentaire