• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

loop in quadratic example

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 + "!");

}
}
}
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
     
    The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic