Results 1 to 8 of 8
  1. #1
    chitwood is offline Member
    Join Date
    Jan 2008
    Posts
    23
    Rep Power
    0

    Default sum,product,average program

    How do i write a java program that will ask fdor 5 numbers to be inputted from an input dialog box and display the sum = ?,the product =, and the average = altogether in the same program using those 5 numbers?????

    I need to get this written and submitted before 12:000am on the 23,2008 of jan. I am in an introductory programming class in college and I have never written any programs except for the ones in the book, that actually work with my help.



    I would appreciate any assistance with this matter



    Thanks,


    Michael Chitwood

  2. #2
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    What have you got so far? In the future, please don't post an assignment asking people to do it. Give us the code you've come up with so far, and ask any questions. You didn't really specify what you are having trouble with.

  3. #3
    chitwood is offline Member
    Join Date
    Jan 2008
    Posts
    23
    Rep Power
    0

    Default

    that is the problem I have no clue how to get started!

  4. #4
    chitwood is offline Member
    Join Date
    Jan 2008
    Posts
    23
    Rep Power
    0

    Default

    import javax.swing.JOptionPane;

    public class SumProductAverage {
    public static void main(String [] args) {

  5. #5
    chitwood is offline Member
    Join Date
    Jan 2008
    Posts
    23
    Rep Power
    0

    Default

    Thanks for the advise, but I know nothing about programming in Java or any other language for that matter. You can say that I am green. I have just started this class and this is the first programming language that I have been introduced to. We are using the Book Fundamentals First: Introduction to Java Programming by Y. Daniel Liang. What I have posted is all that I can think of on how to get started. I dont understand if what I have is right, or even if it is roght where to go to from here. The assigment is for us to create an input dialog box with 5 different numbers and the end product has to tell the sum, product, and average.

    Thanks

    Chitwood

  6. #6
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Ok well, you could do it this way, but i prefer a more object-oriented approach. Basically, what i mean is that i would prefer to make a class in which i can construct a new object that calls for the 5 numbers, and there are methods in this class for doing each operation. If you want to get more sophisticated and practical, you can have it call for any amount of numbers (through use of arrays), but don't worry about that yet. So Ok, i'll set up the structure for you and see if you can figure out the method definitions.

    Java Code:
    public class MathData
    {
         private int a;
         private int b;
         private int c;
         private int d;
         private int e;
         public MathData(int a1, int b1, int c1, int d1, int e1)
         {
               a = a1;
               b = b1;
               c = c1;
               d = d1;
               e = e1;
          }
    
          public int findSum()
          {
               //code goes here
          }
          public int findProduct()
          {
               //code goes here
          }
          public int findAverage()
          {
               //code goes here
          }
    
    }
    Ok so i've set up the class for you. See if you can write the code for the methods.

  7. #7
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Everything you need for your program can be found at the Sun tutorials. After that, you just need a little mathematical logic.

    Btw, please do not PM staff with code/logic issues. Other members are not off limits, so long as they approve of your PMs. But often, staff are busy manning the forum helms - besides you can get a lot of feedback from the forums vs. PMs.

    Provided the code you PMed:
    Java Code:
      import javax.swing.JOptionPane;
    
        public class MathData1 {
           public static void main(String[] args) {
           
           
             JOptionPane.showInputDialog(null,
                "Enter Integer 1",
                "Input Dialog",
                JOptionPane.QUESTION_MESSAGE);
                
          
             JOptionPane.showInputDialog(null,
                "Enter Integer 2",
                "Input Dialog",
                JOptionPane.QUESTION_MESSAGE);
          
             JOptionPane.showInputDialog(null,
                "Enter Integer 3",
                "Input Dialog",
                JOptionPane.QUESTION_MESSAGE);
                
             JOptionPane.showInputDialog(null,
                "Enter Integer 4",
                "Input Dialog",
                JOptionPane.QUESTION_MESSAGE);
                
             JOptionPane.showInputDialog(null,
                "Enter Integer 5",
                "Input Dialog",
                JOptionPane.QUESTION_MESSAGE);
               
          	
          	// Convert string to int
             int intValue = Integer.parseInt(intString);
             
          	//Obtain Sum
             sum = "int 1" + "int 2" + "int 3" + "int 4" + "int 5";
          	
             JOptionPane.showMessageDialog(null,
             "Sum"
          	JOptionPane.INFORMATION_MESSAGE);
          		
          
          }
          
       }
    one way to do what you need is declare some variables for your different ints. Also, sum is not declared in your program and yet you try and set it, you can't set what doesn't exist! When computing your sum, do not compute Strings - this is math, remember? compute integers. :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  8. #8
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Btw, you can also incorporate gibson's class into the code you PMed me. That is, if you know how to instantiate objects, given the correct constructor and make calls to the class's methods. His code can make the concept of what you're trying to accomplish clearer via its OO characteristics.

    I'll get you started on one integer.
    Java Code:
        ...
        String sOne = JOptionPane.showInputDialog(null,
            "Enter Integer 1",
            "Input Dialog",
            JOptionPane.QUESTION_MESSAGE);
    
        ...
        MathData md = new MathData(sOne, ......);
        JOptionPane.showMessage(null, md.findSum());
        ...
    You've got less than an hour... get crackin!
    Last edited by CaptainMorgan; 01-24-2008 at 06:21 AM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 12:09 PM
  2. Adding Plugin Dependency for Feature-based Product
    By abiieez in forum SWT / JFace
    Replies: 2
    Last Post: 05-27-2008, 09:02 PM
  3. Replies: 0
    Last Post: 10-29-2007, 06:34 AM
  4. get the average and maximum score
    By Eric in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 05:15 AM
  5. Calculate average age for women and men?
    By Legoland in forum New To Java
    Replies: 3
    Last Post: 04-18-2007, 11:38 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
  •