Results 1 to 7 of 7
  1. #1
    Manikyr is offline Member
    Join Date
    Jan 2009
    Posts
    17
    Rep Power
    0

    Default I donīt understand

    I have this assignment where they wonīt me to write a grafic java program whick counts the average from some unknown values. The serie with integer ends with the cancel-dialog.

    I testet with my code and it works, but it doesnt work for this assignment.
    How can I change my code so it have the same level as programmation B?

    Java Code:
    import javax.swing.*;
    public class NewEmpty5 {
       public static void main(String[] arg){
           String s;
           s = JOptionPane.showInputDialog("First value?");
           int x = Integer.parseInt(s);
           s = JOptionPane.showInputDialog("Second value?");
           int y = Integer.parseInt(s);
           int average = averagevalue(x,y);
           JOptionPane.showMessageDialog(null, "Averagevalue: " + average);
        System.exit(0);
        }
    
        public static int averagevalue (int a, int b) {
            return  (a+b)/2;
        }
     }
    Last edited by Manikyr; 02-22-2009 at 08:35 PM.

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Whats the problem? Your program works? Great. So what are you doing here?
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    Manikyr is offline Member
    Join Date
    Jan 2009
    Posts
    17
    Rep Power
    0

    Default

    As I wrote It doesnt work for this assignment.
    "write a grafic java program whick counts the average from some unknown values. The serie with integer ends with the cancel-dialog. "

    I donīt know how to change my program so it works for advanced Java..

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    hhhmmm... it would appear that MK12 was not in a good mood when he answered...
    As I see it there are two requirements:
    • Change your program so that it can accept multiple (unknown amount) of values (it currently only accepts two values).
    • Display the result (average) in a graphical manner.

    I can help you with the first one.
    You will have to add a loop to your program and will have to change a little your average method.
    Some pseudo code:
    • initialize value and counter
    • start loop (while loop would be good here)
    • ask the user for the value
    • increment counter
    • add new value to previous value
    • end loop
    • call average method with total value and counter as parameters
    • Display resulting average in a graphical way.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Manikyr is offline Member
    Join Date
    Jan 2009
    Posts
    17
    Rep Power
    0

    Default

    Thank you for your help! Now you just have to explain that in swedish.. = (
    I dont want to demand anything, but can you give my some hints about the codes? I can look them up by myself. Normally I dont have any issues reading in english, but somehow the information about the codes have a big differences between english and swedish.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Thank you for your help! Now you just have to explain that in swedish.. = (
    Sorry, I don't speak Swedish and even if I did, I woudn't, because this is an English spoken forum... and besides, your level of English is good enough to understand my level of code explanation.

    Back to your code... Since your code now has to accept multiple values (not just two), you need a loop. The loop that I suggest is the "while" loop.
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    I know you're not going to read the above link, but I put there for other people on the forum.
    The "while loop" in your case would be something like:
    Java Code:
    while (!(s=="cancel")) {
         get user input (only once)
         increment the counter variable
         add s to the previous s value
    }
    Get that code going, and we'll continue to work on the rest.
    Some hints...
    Java Code:
    import javax.swing.*;
    public class NewEmpty5 {
       public static void main(String[] arg){
           String s;
           [B][COLOR="blue"]//while would start here[/COLOR][/B]
           s = JOptionPane.showInputDialog("First value?");
           int x = Integer.parseInt(s);
           s = JOptionPane.showInputDialog("Second value?");
           int y = Integer.parseInt(s);
           [B][COLOR="Blue"]//loop would end here[/COLOR][/B]
           int average = averagevalue(x,y);
           JOptionPane.showMessageDialog(null, "Averagevalue: " + average);
        System.exit(0);
        }
    
        public static int averagevalue (int a, int b) {
            return  (a+b)/2;
        }
     }
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    Manikyr is offline Member
    Join Date
    Jan 2009
    Posts
    17
    Rep Power
    0

    Default Quick! 30 minutes left

    Now it looks like this, and I still miss the piece where you write more values.
    Something is wrong.. I canīt find it!

    import javax.swing.*;
    public class NewEmpty1 {
    // Den nya metoden averagev
    public static double averagev(double[] a) {
    double sum = 0;
    for (double x : a)
    sum = sum + x;
    return sum/a.length;
    }
    public static void main(String[] arg) {
    while (true) {
    String s = JOptionPane.showInputDialog("Write value ");
    if (s == null)
    break;
    int n = Integer.parseInt(s);
    double[] tal = new double[n];
    for (int i=0; i<n; i++) {
    value[i] = Double.parseDouble(s);
    }
    // Anropa den nya metoden
    JOptionPane.showMessageDialog (null, "Medelvärde av talen(Averagevalue): " + averagev(value));
    }
    System.exit(0);

    }
    }

Similar Threads

  1. help me need to understand queries
    By hossainsadd in forum JDBC
    Replies: 1
    Last Post: 05-26-2008, 01:02 AM
  2. [SOLVED] Please help me understand...
    By Master Zero in forum Java Applets
    Replies: 6
    Last Post: 05-05-2008, 08:41 PM
  3. Errors I don't understand
    By MattyB in forum New To Java
    Replies: 4
    Last Post: 04-02-2008, 12:55 AM
  4. New: Want to understand Drawing...
    By diRisig in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 09:13 AM
  5. i don understand this error
    By Deon in forum New To Java
    Replies: 4
    Last Post: 01-12-2008, 11:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •