1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

Java Methods

Discussion in 'BBS Hangout' started by Lil Pun, Apr 15, 2002.

  1. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    I do not understand Java methods at all. I was told that these were to breakdown code into simpler structures yet all it does for me is make thing that much more difficult. Does anybody know how to use Java methods properly, I looked on the website posted in earlier Java threads for help and found nothing on Java methods. I just wrote a Java program using methods and cannot get the thing to function correctly so if anybody knows any info on these things please drop any information that you have. If you would like to chat sometime that would be fine too, just name it.
     
  2. movement

    movement Member

    Joined:
    Aug 9, 2001
    Messages:
    212
    Likes Received:
    0
    I think your programming class is teaching you “proper” style of writing code. Ideally all code should be in a modular shape, so if you need to redo an action, you can just call the method with a line of code rather than re-writing the same code again. For example, you have the program that does the bi-weekly pay-checks, I’m sure the code is broken into different methods that gathers the person’s name, SSN, address, wage, hours, deductions, etc, so that for each employee you can call that method to produce the his/her check. As your library of methods grow, other programs can call that method as well. If my example of methods is inaccurate, please correct. I’m taking intermediate programming classes that focuses on the logic of the program, not the style or structure.
     
  3. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    Hey movement - or anybody else that would know. I had a chance about two weeks ago for some bonus points and didn't do them. I was supposed to write two programs. The instructions are below. Some of you might remember me asking for a Java class for quadratic equations about two weeks ago and that is because that is what one of the programs deals with. Here are the program details and by the way this is without methods. What would the code for these look like? I tried to figure them out but couldn't and are these supposed to be easy because they sure aren't for me. Thanks for any help.

    Bonus #1
    Write an application that will accept a series of product costs along with the quantity purchased. The program should then display a subtotal of the purchases, the amount of sales tax based on a 6.75% rate, and the total of the purchase in a single dialog message. You should direct the user how to indicate the end of input.
    For example, if the input consisted of:

    Cost of Product Quantity Purchased
    $4.56 2
    $5.98 4
    $0.95 10
    $9.99 5

    Your output would look something like:

    Your subtotal is $92.49
    Sales tax is $6.24
    Purchase total is $98.73


    Bonus #2
    Write an application for the algorithm developed for determining the actual root(s) of a Quadratic Equation using the Quadratic Formula. Your program should display the Quadratic Equation along with the answer(s) or message of no real answers in the output. There should be no assumptions made. If the equation specified is not a Quadratic Equation, your output message should so indicate.
     
  4. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    BTW: Does anybody know of a free Java compiler that I can download because I am sick of executing these programs through the MS-DOS Prompt Window. That would REALLY help out. Thanks.
     
  5. Dr of Dunk

    Dr of Dunk Clutch Crew

    Joined:
    Aug 27, 1999
    Messages:
    46,666
    Likes Received:
    33,710
    Go to Sun's site. They used to offer Forte for free. See if they still do.

    Go to Borland's site. I believe they have a version of JBuilder 6 that's free.
     
  6. Mango

    Mango Member

    Joined:
    Sep 23, 1999
    Messages:
    10,253
    Likes Received:
    5,709
    <A HREF="http://www.thefreecountry.com/developercity/javacompilers.shtml">Some links to what DoD mentioned</A>



    Mango
     
  7. movement

    movement Member

    Joined:
    Aug 9, 2001
    Messages:
    212
    Likes Received:
    0
    For problem 1, how is the user entering the information? Is the program directing him to enter the information? For example:

    Enter the amount of products:
    <input>
    Enter the price for product 1:
    <input>
    Enter the quantity for product 1:
    <input>

    Or is the user entering one long string, and the program dissects the string into the correct format?

    Enter the prices and quantities, then press return:
    $4.56 2 $5.98 4 $0.95 10 $9.99 5
     
  8. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    Yes movement the program is supposed to direct him to enter the information. If you have the code may I ask, what does the code look like? I would like to study it and see what mistakes I was making in the bonus projects. The quadratic equation seemed almost impossible to me.
     
  9. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    I am having trouble with a while structure in one of my programs.
    The while structure is:

    while ( choice >= 1 ) ( choice <= 5 )




    Which means "while greater than or equal to 1 but less than or equal to 5"

    I get this error message though:

    Menu.java48: illegal start of expression







    What am I doing wrong?
     
  10. Gutter Snipe

    Gutter Snipe Member

    Joined:
    Jul 30, 2001
    Messages:
    2,987
    Likes Received:
    65
    The most likely answer would be:

    while (( choice >= 1 ) && ( choice <= 5 ))

    That will work in most C-like languages (java, javascript, c, c++)

    The correct way to read it would be:

    while choice is greater or equal to 1 and less than or equal to 5
     
  11. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    OK that worked but why can't I get this program to execute:
    If you have a java compiler paste and save this and then try to execute it.



    // import Java packages
    import javax.swing.JOptionPane;

    public class Menu {

    // OpeningInfo Method
    public static void OpeningInfo ()
    {
    JOptionPane.showMessageDialog( null, "My name is Jackie Dotson.\nThis program takes two numbers and does the indicated operation.\nThis is program number 9." );
    }

    // NormalExit Method
    public static void NormalExit ()
    {
    JOptionPane.showMessageDialog( null, "This was a normal program exit, Thank you." );
    }

    // Menu Method
    public static String Menu ()
    {
    String input; // Input entered
    String menu; // Menu entered as String
    int choice; // Choice entered as integer

    menu = "Choose from one of the choices please:\n\n " + // Menu text
    "1. Add two numbers\n2. Subtract two numbers\n " + // Menu text
    "3. Multiply two numbers\n4. Divide two numbers\n " + // Menu text
    "5. Exit\n\n"; // Menu text

    input = JOptionPane.showInputDialog( menu ); // Prompt for number
    choice = Integer.parseInt( input );

    // While loop
    while (( choice >= 1 ) && ( choice <= 5 ))
    {
    JOptionPane.showMessageDialog( null, "Error, please select again." );

    menu = "Choose from one of the choices please:\n\n " + // Menu text
    "1. Add two numbers\n2. Subtract two numbers\n " + // Menu text
    "3. Multiply two numbers\n4. Divide two numbers\n " + // Menu text
    "5. Exit\n\n"; // Menu text

    input = JOptionPane.showInputDialog( menu ); // Prompt for number
    choice = Integer.parseInt( input );
    }
    return menu; // Return for menu
    }

    // Number Method
    public static double Number ( String prompt )
    {
    String input; // Input entered
    double number; // Number entered as double

    // Prompt for first number
    input = JOptionPane.showInputDialog( prompt );
    number = Double.parseDouble( input );

    return number;
    }
    }



    // Execution of program begins
    public static void main ( String args[] )
    {

    String input; // Input entered
    String output; // Output shown
    String menu; // Menu attained
    int choice; // Choice entered as integer
    double number1; // Number entered as double type
    double number2; // Number entered as double type
    double sum; // Sum type double
    double total; // Total type double
    double product; // Product type double
    double quotient; // Quotient type double

    OpeningInfo (); // Information

    menu = Menu(); // Initializes menu

    // If structures
    if ( choice == 1 )
    {
    sum = number1 + number2; // Sum calculated

    // Sum output
    JOptionPane.showMessageDialog( null, "The sum is " + sum, "Results",
    JOptionPane.PLAIN_MESSAGE );
    }

    if ( choice == 2 )
    {
    total = number1 - number2; // Total calculated

    // Total output
    JOptionPane.showMessageDialog( null, "The total is " + total, "Results",
    JOptionPane.PLAIN_MESSAGE );
    }

    if ( choice == 3 )
    {
    product = number1 * number2; // Product calculated

    // Product output
    JOptionPane.showMessageDialog( null, "The product is " + product, "Results",
    JOptionPane.PLAIN_MESSAGE );
    }

    if ( choice == 4 )
    {
    quotient = number1 / number2; // Quotient calculated

    // Quotient output
    JOptionPane.showMessageDialog( null, "The quotient is " + quotient, "Results",
    JOptionPane.PLAIN_MESSAGE );
    }

    if ( choice == 5 )
    {
    NormalExit (); // Normal exit indicated
    }



    number1 = Number( "Please enter your first number." ); // First number entered by user
    number2 = Number( "Please enter your second number." ); // Second number entered by user

    System.exit( 0 ); // End
    }
     
  12. Chuck04

    Chuck04 Member

    Joined:
    Feb 15, 1999
    Messages:
    815
    Likes Received:
    233
    I'm pretty sure that while loop should be: while ((choice < 1) || (choice > 5)), cause the way you have it it is saying if choice is between 1 and 5, stay in that while loop. While I'm pretty sure you want to exit the loop when the user inputs a value between 1 and 5. Is that the problem, that it gets stuck in a loop, or does it return an error message at all?

    Does that help?

    After you return from the menu() procedure, you assign the result to a variable called "menu". But when you are determining which choice the user entered, you are comparing it to a variable called "choice".
    Also, the numbers for calculation:
    number1 = Number( "Please enter your first number." ); // First number entered by user
    number2 = Number( "Please enter your second number." ); // Second number entered by user


    ARE NOT read in after the user selects the operation, in fact they aren't read in at all. Which would mean that the program is trying to perform an operation on variables that don't have values.
     
    #12 Chuck04, Apr 17, 2002
    Last edited: Apr 17, 2002
  13. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    Also, I have this assignment where you ask the user for a large number and then the number is output but spaced:

    If the user entered 1234567

    The output would be 1 2 3 4 5 6 7

    How would you space the numbers though?

    Here is the program to get the number but how do you make the number space out?


    import javax.swing.*;

    public class Number {

    public static void main (String args[] )
    {
    String input;
    long number;

    input = JOptionPane.showInputDialog( "Enter a number" );
    number = Long.parseLong( input );

    JOptionPane.showMessageDialog( null, "The number is " + number, "Result",
    JOptionPane.PLAIN_MESSAGE );

    System.exit( 0 );
    }
    }
     
  14. movement

    movement Member

    Joined:
    Aug 9, 2001
    Messages:
    212
    Likes Received:
    0
    http://www.calicotech.org/spaces.html

    Written in vbscript:
    for j=1 to len(userinput) step 1
    character=mid(userinput,j,1)
    newinput=newinput + character + " "
    next

    the 'mid' function is the same as java's charAt(). you do a loop till the length of the user's input, you pull out one character/number out at one time. then you add the character to another variable, also adding a space to the end.
    newinput=newinput, you're adding newinput to itself so you keep the previous value.

    about the problem, to calculate the cost of an item +tax, i'm a little swamped with school work right now, but i have a day off on friday, so i'll take a look at it then, of course, all this depends on my scale of utter laziness.
     
    #14 movement, Apr 18, 2002
    Last edited: Apr 18, 2002
  15. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    Hey movement, how do you fit the space coding into the coding that I wrote?

    What would the input and output look like for this program:

    Bonus #2
    Write an application for the algorithm developed for determining the actual root(s) of a Quadratic Equation using the Quadratic Formula. Your program should display the Quadratic Equation along with the answer(s) or message of no real answers in the output. There should be no assumptions made. If the equation specified is not a Quadratic Equation, your output message should so indicate.
     
  16. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    Does anybody know what the code for the spacing of the numbers looks like and how you incorporate it into the program which I have written?
     
  17. movement

    movement Member

    Joined:
    Aug 9, 2001
    Messages:
    212
    Likes Received:
    0
    Sorry for the late reply, but I just got through all of my finals on Monday.

    http://www.calicotech.org/prices/1.html

    There are 4 steps to this problem:
    1) ask the user how many items he has
    2) in a loop: tell the user to enter in the price/qty, then assign what s/he entered into arrays (all within the same loop)
    3) calculate the price
    4) display the output


    For the spaces in the number problem,
    1) get the user’s input
    2) put spaces in it, with the bit of code in one of the earlier post in this thread
    3) show the number with spaces

    hope this helps.
     

Share This Page