Need some general and specific advice.
So a few weeks ago I found out about the Stanford CS106a classes that you could find on youtube, and since then I have been occasionally working on Karel programs when I get the chance. For the past few days though Ive been working on programming actual java. I've done a few things so far, mainly just math related code to get me used to programming Console Programs. For the past 2 days when I've gotten the chance I've been working on just simply writing the quadratic formula, which took roughly 3-4 hours to write, and another hour and a half to debug mainly one bug that caused the math to not function properly.
So last night I got everything fixed, and this morning I thought before I celebrate I had better make it so that the program repeats so I don't have to close it and run it every time I run a problem. So, like I did in Karel programs, I used the standard "private void repeat() {" (without quotation marks). Which ended up getting some errors and causing some other errors. So after about an hour and a half now messing with the code, googling, and trying to figure out what the error messages mean, I havent gotten to far. At the moment I have "private @ repeat() {" (once again without quotes) and the only error message given is "Syntax error, insert 'enum Identifier' to complete enumHeader". All im aiming to do is create a method to call on if the user presses "y" and wants to run the program again. Heres the code:
Code :
/* This program should allow the user to input variables, and solve the quadratic formula.
*
* Code that causes program to repeat may not be debugged fully.
* Currently working on code to make the program repeat itself.
* Could look neater in the future.
*/
import acm.program.ConsoleProgram;
public class quadraticFormula extends ConsoleProgram {
public void run() {
private @ repeat() {
println("Enter values a, b, and c to compute Quadratic Formula");
int a = readInt("Enter a: "); // Getting some input for the formula //
int b = readInt("Enter b: ");
int c = readInt("Enter c: ");
int discriminant = ((b*b) - (4*a*c)); { // Solves the inside of the radical without finding the square root
if (discriminant < 0) { // This checks for an imaginary number //
println("This is an imaginary number.");
}
else { // This executes the rest of the equation if it is not imaginary //
println("The discriminant (result inside radical) is " + (Math.sqrt(discriminant))); // Prints & squares the discriminate //
int dividend = 2*a; // Simply multiplies the 2 on the bottom of the equation times a //
double posativeEquation = ((-b) + Math.sqrt(discriminant))/dividend; // These do the basic math to finish up the problem
double negativeEquation = ((-b) - Math.sqrt(discriminant))/dividend;
println("Result of addittion equation is " + posativeEquation + ".");
println("Result of subtraction equation is " + negativeEquation + ".");
println("----Would you like to compute another formula?");
int r = readInt("Enter y or n"); // Everything below this will cause the program to run again if y is read
int y = repeat;
int n = ABORT;
if (r = y); {
repeat();
}
if (r = n); {
ABORT();
}
}
}
}
}
}
I kinda realize this maybe kindav painful to look at, which is another question I had. My karel programs looked extremely neat, but these end up looking pretty rough. Could I get some advice about how to make this program look a little neater?
Like I was saying before Im really looking for something that I could just call on in "y" that would cause the program to restart.
I know this maybe pretty bad, but for my first real program Im pretty proud of it! Thx in advance for any advice you can give that I could use to improve!
Re: Need some general and specific advice.
I've never seen a method inside a method used inside of Java..
To repeat until a condition isn't satisfied, use a while loop or a do while loop. See Java while loops for more information on using while loops in Java (If you know C or C++, they operating in pretty much the same way and have the same syntax structure).
Code wise, it could use some formatting. If you have the Eclipse IDE, it has an auto-format option that can format your code following parameters you define, or you can use the default ones. It can correctly tab statements/brackets and such, put spaces in the correct places, add missing import statements, and make other changes format-wise to your program. There is an option that will allow Eclipse to arrange the order of code sections inside your program, but I would recommend that you keep it off because it can have unwanted consequences for program flow and do this kind of formatting by hand.
Re: Need some general and specific advice.
Quote:
Originally Posted by
helloworld922
I've never seen a method inside a method used inside of Java..
To repeat until a condition isn't satisfied, use a while loop or a do while loop. See
Java while loops for more information on using while loops in Java (If you know C or C++, they operating in pretty much the same way and have the same syntax structure).
Code wise, it could use some formatting. If you have the Eclipse IDE, it has an auto-format option that can format your code following parameters you define, or you can use the default ones. It can correctly tab statements/brackets and such, put spaces in the correct places, add missing import statements, and make other changes format-wise to your program. There is an option that will allow Eclipse to arrange the order of code sections inside your program, but I would recommend that you keep it off because it can have unwanted consequences for program flow and do this kind of formatting by hand.
A "while (true) { " worked perfectly! And ill look into the formatting when I can get a chance.