|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

01-22-2008, 08:38 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 9
|
|
|
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
|
|

01-22-2008, 11:55 PM
|
 |
Senior Member
|
|
Join Date: Nov 2007
Location: New York
Posts: 143
|
|
|
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.
__________________
//Haha javac, can't see me now, can ya?
|
|

01-23-2008, 01:49 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 9
|
|
|
that is the problem I have no clue how to get started!
|
|

01-23-2008, 01:51 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 9
|
|
|
import javax.swing.JOptionPane;
public class SumProductAverage {
public static void main(String [] args) {
|
|

01-23-2008, 01:59 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 9
|
|
|
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
|
|

01-23-2008, 01:59 AM
|
 |
Senior Member
|
|
Join Date: Nov 2007
Location: New York
Posts: 143
|
|
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.
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.
__________________
//Haha javac, can't see me now, can ya?
|
|

01-24-2008, 06:10 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
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:
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. 
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

01-24-2008, 06:18 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
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.
...
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!
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
Last edited by CaptainMorgan : 01-24-2008 at 06:21 AM.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|