/************************************************************************\
NUMBERS LIST
This program creates an AWT GUI form,
inputs numbers, stores them in a list,
and then adds up all the numbers.
\************************************************************************/
import java.awt.*;
import javax.swing.*;
TextField numBox = new TextField(" ");
Button b1 = new Button("Add to list");
java.awt.List numList = new java.awt.List(10);
Button b2 = new Button("Total");
Font big = new Font("Arial",0,36);
void setup()
{
size(300,500); // change size of standard window
b1.setFont(big); // change font on the Button b1
b2.setFont(big); // change font on Button b2
numBox.setFont(big);
numList.setFont(new Font("Arial",0,24));
numBox.setText("");
add(numBox);
add(b1); // display Button b1
add(numList);
add(b2); // display Button b2
}
boolean action(java.awt.Event evt, Object src)
{
Object source = evt.target; // this is the Button that was clicked
if(source==b1)
{
String value = numBox.getText(); // copy text from TextField
numList.add(value); // copy number into numList
numBox.setText(""); // clear the TextField
numBox.requestFocus(); // move cursor to TextField
}
if(source==b2) // [Total] Button
{
output("Total = " + total());
if( numList.getItemCount() != 0)
{ output("Average = " + (total() / numList.getItemCount())); }
}
return true;
}
double total()
{
double sum = 0;
int count = numList.getItemCount(); // count items in numList
for(int c = 0; c < count; c = c+1) // loop through all items
{
String value = numList.getItem(c); // get item as String
double num = Double.parseDouble(value); // convert String to double
sum = sum + num; // add to total
}
return sum;
}
//==== IBIO standard Input / Output functions ====//
public void output(String message)
{ javax.swing.JOptionPane.showMessageDialog(this,message); }
public void outputString(String message)
{ output(message); }
public void output(char info)
{ output(info + ""); }
public void output(byte info)
{ output(info + ""); }
public void output(int info)
{ output(info + ""); }
public void output(long info)
{ output(info + ""); }
public void output(double info)
{ output(info + ""); }
public void output(boolean info)
{ output(info + ""); }
static String input(String prompt)
{ return javax.swing.JOptionPane.showInputDialog(null,prompt); }
static String inputString(String prompt)
{ return input(prompt); }
static String input()
{ return input(""); }
static int inputInt()
{ return inputInt(""); }
static double inputDouble()
{ return inputDouble(""); }
static char inputChar(String prompt)
{ char result=(char)0;
try{result=input(prompt).charAt(0);}
catch (Exception e){result = (char)0;}
return result;
}
static byte inputByte(String prompt)
{ byte result=0;
try{result=Byte.valueOf(input(prompt).trim()).byteValue();}
catch (Exception e){result = 0;}
return result;
}
static int inputInt(String prompt)
{ int result=0;
try{result=Integer.valueOf(
input(prompt).trim()).intValue();}
catch (Exception e){result = 0;}
return result;
}
static long inputLong(String prompt)
{ long result=0;
try{result=Long.valueOf(input(prompt).trim()).longValue();}
catch (Exception e){result = 0;}
return result;
}
static double inputDouble(String prompt)
{ double result=0;
try{result=Double.valueOf(
input(prompt).trim()).doubleValue();}
catch (Exception e){result = 0;}
return result;
}
static boolean inputBoolean(String prompt)
{ boolean result=false;
try{result=Boolean.valueOf(
input(prompt).trim()).booleanValue();}
catch (Exception e){result = false;}
return result;
}