how to apply a formula in the JOptionPane?

Question

Create a program to determine the average of sales for petrol pump station. Your input should ask the user to enter (number of station) and (sales for each station).

My question?

I have already create the program and I need some help for applying the formula (average formula) [Addition all the sales and divide the number of station]

import javax.swing.*;

publicclass oilstation

{

publicstaticvoid main (String[]args)

{

String input, input2;

String input4="";

int input1, input3;

input=JOptionPane.showInputDialog("Enter number of station");

input1=Integer.parseInt(input);

for (int i=1; i<=input1; i++)

{

input2=JOptionPane.showInputDialog("Pump Station "+i+" sales RM: ")+"\n";

input4+=(input2);

}

JOptionPane.showMessageDialog(null,"The average of sales: "+"\n"+input4);

[1487 byte] By [neosinclaira] at [2007-10-3 4:21:44]
# 1
What do you think this> input4+=(input2);is doing?If you want to add numbers, you need to have numbers.
CeciNEstPasUnProgrammeura at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 2

What did your compiler say about your code? btw, you should either go

totally 'mathematical' and name your variables n, s, t for the number of

stations, sales and total sales or give them descriptive names such as

nofStations, sales, totalSales.

kind regards,

Jos

JosAHa at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 3

[IMG]http://img244.imageshack.us/img244/4328/1ev7.gif[/IMG]

the output will be like this...

what i am trying to do is to count the average of the pump station sales..

how do I put the formula if theinput2=JOptionPane.showInputDialog("Pump Station "+i+" sales RM: ")+"\n";

data is changing each time...how do I put some formula into it?

neosinclaira at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 4
> the output will be like this...I know what the output looks like. The question is simply: "do you know what you are doing?" :) You're not adding numbers, you're simply concatenating strings.
CeciNEstPasUnProgrammeura at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 5

The JOptionPane simply displays a nice dialog and allows for a user

to enter a String value. It doesn't bother whether or not that String might

make up a number, integral or not. It simply doesn't care. It's your

responsibility to do with that String what you want/need to do with it.

Another issue is 'separation of concerns'. Just as the JOptionPane doesn't

care about matters, your 'AverageCalculator' shouldn't care about where

those numbers came from. All it should do is sum up all the numbers

given to it, one by one, and produce an average value when it is asked

for it. A separate little class that does just this would be fine here.

kind regards,

Jos

JosAHa at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 6
im not really understand what you mean by adding a numbers...can you explain breifly?isn`t the user need to type the numbers?
neosinclaira at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 7

> im not really understand what you mean by adding a

> numbers...can you explain breifly?

>

> isn`t the user need to type the numbers?

Yep, but the AverageCalculator doesn't care where those numbers come

from, all it does is add up all those individual numbers and keeps track

of how many of those numbers were fed to it. When it is asked to give

the average number it returns that sum divided by the number of numbers

fed to it; just like you and I would do it.

kind regards,

Jos

JosAHa at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 8
i get what you mean...but im still confusing with the loops..can you show example for it?
neosinclaira at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 9
> i get what you mean...but im still confusing with the> loops..can you show example for it?How about you telling us what you want to do inside the loop?
CeciNEstPasUnProgrammeura at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 10

input=JOptionPane.showInputDialog("Enter number of station");

input1=Integer.parseInt(input);

for (int i=1; i<=input1; i++)

{

input2=JOptionPane.showInputDialog("Pump Station "+i+" sales RM: ")+"\n";

as you can see here we put the number of station...then it will ask for the pump station sales and the value will store in input2.(am i rite)?

so..my question is how do i addition the input2 if its looping...it would be helpfull if you can provide me some code

neosinclaira at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 11

> as you can see here we put the number of

> station...then it will ask for the pump station sales

> and the value will store in input2.(am i rite)?

u r crrct. bt dt dsnt mk ds knd f wrtg lss anoyng. Plz dnt do dt. :)

> so..my question is how do i addition the input2 if

> its looping...

Hint 1: total = total + new amount.

Hint 2: you can't add Strings.

> it would be helpfull if you can provide

> me some code

No, it won't.

CeciNEstPasUnProgrammeura at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 12

im still beginners to this...please dont laugh at me : '(

int input1, input3;

numStation=JOptionPane.showInputDialog("Enter number of station");

input1=Integer.parseInt(numStation);

for (int i=1; i<=input1; i++)

{

input3=Integer.parseInt(JOptionPane.showInputDialog("Pump Station "+i+" sales RM: "));

input3=input3+input3/input1;

am i correct?

Message was edited by:

neosinclair

neosinclaira at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 13

> input3=input3+input3/input1;

Nope, that's not correct. Read what I wrote in my first reply and rename

your variables to 'nofStations', 'sales', 'totalSales' or similar and then

see what you're trying to do here. We want to see a loop like this:for (int i= 1; i <= nofStations; i++) {

sales= <some mumbo jumbo here>;

totalSales= totalSales+sales;

}

// we don't need fractions?

int averageSales= totalSales/nofStations;

kind regards,

Jos

JosAHa at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 14

> im still beginners to this...please dont laugh at me

> : '(

Nobody's laughing at you. But trust me if I say that giving you code won't help you. The primary skill you need to obtain is to break up a solution into an algorithm, and to write that algorithm in code. Providing you an example will not help you with that. I want you to get the idea by yourself.

When I asked about what you want to do, I didn't mean code. I just wanted to know the steps you want to repeatedly do.

the code looks better now, but there are still some issues:

- follow Jos' advise about renaming variables. Really, really do. It'll help you getting the formula correct. Have one "total", have one "numberOfStations", have one "input", have one "average".

CeciNEstPasUnProgrammeura at 2007-7-14 22:23:53 > top of Java-index,Java Essentials,Java Programming...
# 15

Ok...take a look for this..

import javax.swing.*;

public class oilstation

{

public static void main (String[]args)

{

String numStation;

int station, sales, totalSales;

numStation=JOptionPane.showInputDialog("Enter number of station");

station=Integer.parseInt(numStation);

for (int i=1; i<=station; i++){

sales=Integer.parseInt(JOptionPane.showInputDialog("Pump Station "+i+" sales RM: "));

totalSales=totalSales+sales;

}

int averageSales= totalSales/station;

JOptionPane.showMessageDialog(null,"The average of sales: "+"\n"+averageSales);

}

}

when i try to compile it says variable totalSales might not have been initialized...anthing that i miss?

Message was edited by:

neosinclair

neosinclaira at 2007-7-21 10:29:10 > top of Java-index,Java Essentials,Java Programming...
# 16

> when i try to compile it says variable totalSales

> might not have been initialized...anthing that i

> miss?

Yes: you didn't initialize the variables.

> totalSales=totalSales+sales;

So, given the very first run: which amount will totalSales have in the beginning? You basicaly say

> new value = undefined + 1000;

You need to set totalSales to 0 before using it.

CeciNEstPasUnProgrammeura at 2007-7-21 10:29:10 > top of Java-index,Java Essentials,Java Programming...
# 17

> when i try to compile it says variable totalSales might not have been

> initialized...anthing that i miss?

That compiler diagnostic message is crystal clear: that variable was not

initialized anywhere in your code. Before you start the loop you should

initialize that variable:totalSales= 0;

Do the terms "loop (in)variant, begin and end condition" sound familar?

If so, the loop invariant condition should be that "totalSales" should

contain the total sales given i numbers of sales. If i == 0, totalSales

should be equal to zero.

kind regards,

Jos

JosAHa at 2007-7-21 10:29:10 > top of Java-index,Java Essentials,Java Programming...
# 18
thanks guys...you all are awesome!...i will work harder for it more!
neosinclaira at 2007-7-21 10:29:10 > top of Java-index,Java Essentials,Java Programming...
# 19

> thanks guys...you all are awesome!...i will work

> harder for it more!

Good, that's the spirit. As far as I can tell you almost got it all working.

If so, try to enter the text "foo" in one of those dialog boxes or "0" anywhere

you like and see what it takes to modify your program to make it an

industrial strength program. No need to get frustrated though ;-)

kind regards,

Jos

JosAHa at 2007-7-21 10:29:10 > top of Java-index,Java Essentials,Java Programming...
# 20

> Good, that's the spirit. As far as I can tell you

> almost got it all working.

> If so, try to enter the text "foo" in one of those

> dialog boxes or "0" anywhere

> you like and see what it takes to modify your program

> to make it an

> industrial strength program. No need to get

> frustrated though ;-)

Nothing simplifies a program more than the lack of error handling.

CeciNEstPasUnProgrammeura at 2007-7-21 10:29:10 > top of Java-index,Java Essentials,Java Programming...
# 21

> > an industrial strength program. No need to get

> > frustrated though ;-)

>

> Nothing simplifies a program more than the lack of

> error handling.

A specialization of that remark is: "anyone can write a compiler for

perfect users". I don't remember who wrote that anymore" ...

kind regards,

Jos

JosAHa at 2007-7-21 10:29:10 > top of Java-index,Java Essentials,Java Programming...