Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need some general and specific advice.

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:


    /* 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!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Morevan (January 3rd, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some general and specific advice.

    Quote Originally Posted by helloworld922 View Post
    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.

Similar Threads

  1. General CS concepts
    By helloworld922 in forum Java Programming Tutorials
    Replies: 14
    Last Post: April 21st, 2011, 10:35 AM
  2. Programming advice
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 6th, 2009, 04:19 PM
  3. i need some advice ....
    By mdstrauss in forum Java Theory & Questions
    Replies: 8
    Last Post: July 24th, 2009, 02:29 PM