Dati 4 reali, è possibile calcolare minimo, massimo, totale e
media
Codice Html
<applet code="MMTM.class" width="480" height="100">...</applet>
Codice Java
/* M M T M
* Minimo, Massimo, Totale, Media
*/
import java.awt.*;
import java.awt.color.*;
import java.awt.event.*;
import javax.swing.*:
public class MMTM extends JApplet
implements ActionListener
{
private JTextField txt1, txt2, txt3, txt4;
private JLabel lOut;
public void init()
{
JPanel pNORTH=new JPanel();
txt1=new JTextField("-20", 10); pNORTH.add(txt1);
txt2=new JTextField("-1" , 10); pNORTH.add(txt2);
txt3=new JTextField("+1" , 10); pNORTH.add(txt3);
txt4=new JTextField("+20", 10); pNORTH.add(txt4);
this.add(pNORTH, BorderLayout.NORTH);
lOut=new JLabel("???", JLabel.CENTER);
this.add(lOut, BorderLayout.CENTER);
JPanel pSOUTH=new JPanel();
JButton bMIN=new JButton("Minimo"); bMIN.addActionListener(this); pSOUTH.add(bMIN);
JButton bMAX=new JButton("Massimo"); bMAX.addActionListener(this); pSOUTH.add(bMAX);
JButton bTOT=new JButton("Totale"); bTOT.addActionListener(this); pSOUTH.add(bTOT);
JButton bMED=new JButton("Media"); bMED.addActionListener(this); pSOUTH.add(bMED);
this.add(pSOUTH, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent evento)
{
String nome=evento.getActionCommand();
String msg =new String();
try
{
double in1=Double.parseDouble(txt1.getText());
double in2=Double.parseDouble(txt2.getText());
double in3=Double.parseDouble(txt3.getText());
double in4=Double.parseDouble(txt4.getText());
double risultato=0;
if(nome.equals("Minimo"))
{
risultato=Math.min(Math.min(in1, in2),
Math.min(in3, in4));
msg="Minimo = " + risultato;
}
else if(nome.equals("Massimo"))
{
risultato=Math.max(Math.max(in1, in2),
Math.max(in3, in4));
msg="Massimo = " + risultato;
}
else if(nome.equals("Totale"))
{
risultato=in1+in2+in3+in4;
msg="Totale = "+risultato;
}
else if(nome.equals("Media"))
{
risultato=(in1+in2+in3+in4)/4.0;
msg="Media = "+risultato;
}
}
catch(Exception eccezione)
{
msg="???";
}
lOut.setText(msg);
}
}
Osserva
-
Double.parseDouble()
try
{
...
}
catch(...)
{
...
}
|