Einen schönen guten Tag euch allen.
Ich bin wirklich nicht gut im Programmieren und hoffe sehr auf eure Hilfe.
Habe versucht mit GUI statistische Funktionen, wie Mittelwert und Varianz auswerten. Nur die Varianz wird leider nicht berechnet.
Die Code ist ziemlich lang, die Methode von Varianz ist ganz unten. Ich weiß nicht wo ich mein Fehler habe.
Auf jede Antwort und eure Hilfe freue ich mich sehr.
Viele Grüße
Remigina
[/B]
Ich bin wirklich nicht gut im Programmieren und hoffe sehr auf eure Hilfe.
Habe versucht mit GUI statistische Funktionen, wie Mittelwert und Varianz auswerten. Nur die Varianz wird leider nicht berechnet.
Die Code ist ziemlich lang, die Methode von Varianz ist ganz unten. Ich weiß nicht wo ich mein Fehler habe.
Auf jede Antwort und eure Hilfe freue ich mich sehr.
Viele Grüße
Remigina
Java:
package Calculator;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import plotter.Graphic;
import plotter.LineStyle;
import plotter.Plotter;
import plotter.TextObject;
public class Versuch3 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 2915991130900996102L;
private JPanel contentPane;
private JTextField txt_Werte;
private JTextField txt_Ergebnisse;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Versuch3 frame = new Versuch3();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Versuch3() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnZufall = new JButton("Zufall");
btnZufall.addActionListener(new ActionListener() {
//private int i;
public void actionPerformed(ActionEvent arg0) {
int wert = 0;
boolean Zufall = true;
if (Zufall) {
txt_Werte.setText("");
Zufall = false;
}
int[] a = new int[3];
for (int i = 0; i < 3; i++) {
a[i] = wert;
wert = (int) (Math.random() * 100) + 1;
txt_Werte.setText(txt_Werte.getText() + " " + wert);
}
}
});
btnZufall.setBounds(0, 228, 89, 23);
contentPane.add(btnZufall);
txt_Werte = new JTextField();
txt_Werte.setBounds(135, 155, 259, 23);
contentPane.add(txt_Werte);
txt_Werte.setColumns(10);
JEditorPane editorPane = new JEditorPane();
editorPane.setBounds(125, 11, 269, 133);
contentPane.add(editorPane);
JButton btnmittelwert = new JButton("Mittelwert");
btnmittelwert.addActionListener(new ActionListener() {
//private int i;
public void actionPerformed(ActionEvent arg0) {
String[] txtWerte = txt_Werte.getText().trim().split(" ");
double[] werte = new double[txtWerte.length];
for (int i = 0; i < txtWerte.length; i++) {
System.out.println(txt_Werte.getText());
werte[i] = Double.parseDouble(txtWerte[i]);
}
double mittelwert = mittelwert(werte);
txt_Ergebnisse.setText("Mittelwert: " + mittelwert);
}
});
JButton btnvarianz = new JButton("Varianz");
btnvarianz.addActionListener(new ActionListener() {
//private int i;
public void actionPerformed(ActionEvent arg0) {
//System.out.println("Var");
String[] txtWerte = txt_Werte.getText().trim().split(" ");
double[] werte = new double[txtWerte.length];
//double mittelwert = mittelwert(werte);
for (int i = 0; i < txtWerte.length; i++) {
System.out.println(txt_Werte.getText());
werte[i] = Double.parseDouble(txtWerte[i]);
}
double var = 0.0;
for (int i = 1; i < werte.length; i++) {
//var += ((werte[i] - mittelwert) * (werte[i] - mittelwert))/werte.length;
System.out.print(txt_Werte.getText());
werte[i]=Double.parseDouble(txtWerte[i]);
}
double varianz = varianz(werte);
txt_Ergebnisse.setText("Varianz: " + var);
}
});
JButton btnstandardabweichung = new JButton("Standardabweichung");
btnvarianz.addActionListener(new ActionListener() {
//private int i;
public void actionPerformed(ActionEvent arg0) {
System.out.println("stw");
String[] txtWerte = txt_Werte.getText().trim().split(" ");
double[] werte = new double[3];
double mittelwert = mittelwert(werte);
for (int i = 0; i < txtWerte.length; i++) {
System.out.println(txt_Werte.getText());
werte[i] = Double.parseDouble(txtWerte[i]);
}
double stw = 0.0;
double var = 0.0;
for (int i = 1; i < werte.length; i++) {
var += (werte[i] - mittelwert) * (werte[i] - mittelwert);
}
stw = Math.sqrt(var);
txt_Ergebnisse.setText("Standardabweichung: " + stw);
}
});
JButton btnmax = new JButton("Maximum");
btnmax.addActionListener(new ActionListener() {
//private int i;
public void actionPerformed(ActionEvent arg0) {
String[] txtWerte = txt_Werte.getText().trim().split(" ");
double[] werte = new double[txtWerte.length];
for (int i = 0; i < txtWerte.length; i++) {
System.out.println(txt_Werte.getText());
werte[i] = Double.parseDouble(txtWerte[i]);
}
double max = werte[0];
for (int i = 1; i < werte.length; i++) {
if (werte[i] > max) {
max = werte[i];
}
}
txt_Ergebnisse.setText("Maximum: " + max);
}
});
JButton btnmin = new JButton("Minimum");
btnmin.addActionListener(new ActionListener() {
//private int i;
public void actionPerformed(ActionEvent arg0) {
String[] txtWerte = txt_Werte.getText().trim().split(" ");
double[] werte = new double[3];
for (int i = 0; i < txtWerte.length; i++) {
System.out.println(txt_Werte.getText());
werte[i] = Double.parseDouble(txtWerte[i]);
}
double min = werte[0];
for (int i = 1; i < werte.length; i++) {
if (werte[i] < min) {
min = werte[i];
}
}
txt_Ergebnisse.setText("Minimum: " + min);
}
});
JButton btnclear = new JButton("Clear");
btnclear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
txt_Ergebnisse.setText("");
}
});
btnmittelwert.setBounds(89, 228, 89, 23);
contentPane.add(btnmittelwert);
JButton btnNewButton_1 = new JButton("Median");
btnNewButton_1.setBounds(174, 228, 89, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("Varianz");
btnNewButton_2.setBounds(259, 228, 89, 23);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("Standardabweichung");
btnNewButton_3.setBounds(345, 228, 89, 23);
contentPane.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("Maximum");
btnNewButton_4.setBounds(430, 228, 95, 23);
contentPane.add(btnNewButton_4);
//JButton btnNewButton_6 = new JButton("Minimum");
btnmin.setBounds(515, 228, 89, 23);
contentPane.add(btnmin);
//JButton btnclear = new JButton("Clear");
btnclear.setBounds(89,250, 89, 23);
contentPane.add(btnclear);
txt_Ergebnisse = new JTextField();
txt_Ergebnisse.setBounds(135, 189, 128, 20);
contentPane.add(txt_Ergebnisse);
txt_Ergebnisse.setColumns(10);
}
static double mittelwert(double[] x) {
int i, n;
n = x.length;
double w = 0;
for (i = 0; i < n; i++)
w += x[i];
w /= n;
return w;
}// Ende mittelwert()
static double varianz(double[] x){
int i,n;
n=x.length;
double v=0;
double w = mittelwert(x);
for (i=0; i<n; i++)
v+=(x[i]-w)*(x[i]-w);
v/=n;
return v;
}
}
[B]
[/B]