• Post Reply Bookmark Topic Watch Topic
  • New Topic

loop in quadratic example  RSS feed

 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everybody,
im writing a program to output 2 vales for X for a quadratic equation. im not the best with loops however and i wana put a loop in the code so that the menu will continue to reappear once it has calculated the previous values and until the user selects the end option. any ideas for code???
my code so far is as follows:

import java.io.*;

class QuadraticApp {


// ----------------FIELDS--------------------

// Create BufferedReader class instance

public static InputStreamReader input =
new InputStreamReader(System.in);

public static BufferedReader keyboardInput = new
BufferedReader(input);

// ---------------METHODS--------------------

/* Main Method*/

public static void main(String[] args) throws IOException {
int selector;

// Output menu
selector = outputMenu();
// Process selection
processSelector(selector);
}

/* Menu */

public static int outputMenu() throws IOException {
System.out.println(" USER MENU ");
System.out.println("======================");
System.out.println("1. Calculate Quadratic");
System.out.println("2. End ");
System.out.println("Please select option: ");
return(Integer.parseInt(keyboardInput.readLine()));
}
/* Process selector. If unrecognised selection output error and repeat. */

public static void processSelector(int selector) throws IOException {
switch (selector) {
case 1:
System.out.println ("Input a value (no less than -100 or greater than 100) for a, b & c:");

double a = Double.parseDouble(keyboardInput.readLine());
double b = Double.parseDouble(keyboardInput.readLine());
double c = Double.parseDouble(keyboardInput.readLine());

// Create Instance

QuadraticClass newQuadratic = new QuadraticClass(a, b, c);

// a
if (a > 100 || a < -100){
System.out.println("Cannot complete calculation, please enter values between -100 and 100");
}
else
if (b > 100 || b < -100){
System.out.println("Cannot complete calculation, please enter values between -100 and 100");
}
else
if (c > 100 || c < -100){
System.out.println("Cannot complete calculation, please enter values between -100 and 100");
}

else{

// Calculation
double trial = a + b;
if (trial == 0){
System.out.println("Cannot complete calculation, incorrect values entered");
}
}
// Discrimination
double disc = (Math.pow((double)b,2)-(4*a*c));
if (disc < 0){
System.out.println("Cannot complete calculation, values are incompatible");
}
else{
// Calculate Root1
newQuadratic.calcRoot1(a, b, c);
// Calculate Root2
newQuadratic.calcRoot2(a, b, c);
}

break;
case 2:
System.out.println (" ");
break;

default:
System.out.println("ERROR: Unrecognised menu selection " +
selector + "!");

}
}
}
 
Alex Wolf
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
this sounds like a trick question.... but what the heck... tell me why you wouldn't just put

I would recommend to use the the code tags to make your code look neat. otherwise its hard to read.
 
Stephen Bloch
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another suggestion. You're doing at least four unrelated jobs all in one method:
  • the loop that keeps asking whether to get another quadratic;
  • getting and validating the coefficients of a quadratic;
  • computing the solutions; and
  • printing the solutions.

  • These four jobs should all be in separate methods, so if at some time in the future you need to do one without the other three, you can just call that method rather than rewriting all this code. In particular, I assume that calcRoot1 and calcRoot2 print their results; as a result, if you were writing a program that actually needed to use the results for some other computation, you'd need to rewrite calcRoot1 and calcRoot2. A better approach is to have calcRoot1 and calcRoot2 return their answers, so the method that called them can decide for itself whether to print them to System.out, display them on a graphics window, write them to a file, or do some other computation with them. The restructured program might look something like

    I would move the job of checking whether the user chose a legal menu option to inside the outputMenu method. I would move all the stuff about prompting the user for coordinates, and checking that the coordinates are in the right range, into readAndValidate. Checking the discriminant moves into the boolean-valued method hasRealRoots.
    All the printing and formatting of values (which presumably is now being done inside calcRoot1 and calcRoot2) will move into printRoots; as an added benefit, printRoots can check whether its parameters are equal (or nearly so), and print out a different message like "double root at 3.72".
    [ December 09, 2004: Message edited by: Stephen Bloch ]
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!