Hallo,
ich habe ein kleines Problem und zwar...
Ich habe zwei Klassen programmiert (einen Frame und eine "normale" Ring-Klasse), die eine erzeugt einen Frame (und hat eine Main-Methode), die andere erzeugt "Ringe". Ich habe bereits einen Konstruktor, Getter/Setter, eine toString-Methode und eine Methode die mir die Fläche eines Ringes ausrechnet, wenn ich den Innenradius vom Außenradius abziehe. In dem Frame habe ich vier Buttons, die jeweils eine Funktion erfüllen. Alle Buttons funktionieren bisher wie sie sollen.
Doch hier mein Problem: Ich bin dabei den 4. Button zu programmieren. Er soll herausfinden welcher Ring in einem Ring-Array die größte Fläche hat. Ich habe es versucht zu lösen, habe es aber nicht hinbekommen, dass die Methode das macht was ich will
. Meine Idee war es den Ring mit der größten Fläche an 1. Stelle des Arrays zu befördern mit Hilfe des Dreieckstausches. Nur bin ich mir nicht sicher ob die von mir gewählte Variable dafür richtig gewählt ist und ob die Methode an sich Sinn macht.
Ich wäre sehr froh, wenn mir jemand helfen könnte, Danke!
Meine Ring-Klasse:
Mein Ring-Frame:
ich habe ein kleines Problem und zwar...
Ich habe zwei Klassen programmiert (einen Frame und eine "normale" Ring-Klasse), die eine erzeugt einen Frame (und hat eine Main-Methode), die andere erzeugt "Ringe". Ich habe bereits einen Konstruktor, Getter/Setter, eine toString-Methode und eine Methode die mir die Fläche eines Ringes ausrechnet, wenn ich den Innenradius vom Außenradius abziehe. In dem Frame habe ich vier Buttons, die jeweils eine Funktion erfüllen. Alle Buttons funktionieren bisher wie sie sollen.
Doch hier mein Problem: Ich bin dabei den 4. Button zu programmieren. Er soll herausfinden welcher Ring in einem Ring-Array die größte Fläche hat. Ich habe es versucht zu lösen, habe es aber nicht hinbekommen, dass die Methode das macht was ich will
Ich wäre sehr froh, wenn mir jemand helfen könnte, Danke!
Meine Ring-Klasse:
Java:
public class Ring {
private int innerradius;
private int outerradius;
private double area;
//Konstruktor
public Ring(int innerradius, int outerradius) {
this.innerradius = innerradius;
this.outerradius = outerradius;
}
//Setter und Getter:
public int getInnerradius() {
return innerradius;
}
public void setInnerradius(int innerradius) {
this.innerradius = innerradius;
}
public int getOuterradius() {
return outerradius;
}
public void setOuterradius(int outerradius) {
this.outerradius = outerradius;
}
//Berechnung der Fläche des Ringes (Methode):
public double calcArea() {
double area = Math.PI * (this.outerradius * this.outerradius - this.innerradius * this.innerradius);
this.area = area;
return area;
}
//toString Methode:
@Override
public String toString() {
return "Ring: innerradius = " + innerradius + ", outerradius = " + outerradius + ", area = " + String.format("%.2f", area);
}
}
Java:
public class RingFrame extends JFrame {
private JPanel contentPane;
private JLabel l1;
private JLabel l2;
private JTextField t1;
private JTextField t2;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
Ring [] ringArray = new Ring [20];
int arrayCount = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RingFrame frame = new RingFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public RingFrame() {
setResizable(false);
setTitle("GLFS");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 250, 255);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
l1 = new JLabel("Zahl 1:");
l1.setBounds(10, 11, 46, 14);
contentPane.add(l1);
l2 = new JLabel("Zahl 2:");
l2.setBounds(150, 11, 46, 14);
contentPane.add(l2);
t1 = new JTextField();
t1.setBounds(10, 36, 86, 20);
contentPane.add(t1);
t1.setColumns(10);
t2 = new JTextField();
t2.setBounds(150, 36, 86, 20);
contentPane.add(t2);
t2.setColumns(10);
b1 = new JButton("Action 1");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Random random = new Random();
int zufallszahl = 0;
int maximum = 100;
int minimum = 1;
System.out.print("Zufallszahlen: ");
for (int i = 0; i < 10; i++) {
zufallszahl = random.nextInt(maximum + minimum);
System.out.print(zufallszahl + " ");
}
System.out.println();
System.out.print("Zufallszahl (-5): ");
while (zufallszahl > 5) {
System.out.print(zufallszahl + " ");
zufallszahl = zufallszahl - 5;
}
}
});
b1.setBackground(Color.LIGHT_GRAY);
b1.setBounds(10, 80, 226, 23);
contentPane.add(b1);
b2 = new JButton("Action 2");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int ir = Integer.parseInt(t1.getText());
int or = Integer.parseInt(t2.getText());
Ring r = new Ring(ir, or);
if (ir < or) {
System.out.print(r);
} else {
System.err.println("Bitte geben Sie die Zahlen erneut ein!");
}
}
});
b2.setBackground(Color.LIGHT_GRAY);
b2.setBounds(10, 114, 226, 23);
contentPane.add(b2);
b3 = new JButton("Action 3");
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int ir = Integer.parseInt(t1.getText());
int or = Integer.parseInt(t2.getText());
Ring r = new Ring(ir, or);
ringArray [arrayCount] = r;
arrayCount++;
System.out.println("\n---------------------------- Ringe ----------------------------");
for (int i = 0; i < arrayCount; i++) {
r.calcArea();
System.out.println(arrayCount + ". " + ringArray [i]);
}
}
});
b3.setBackground(Color.LIGHT_GRAY);
b3.setBounds(10, 148, 226, 23);
contentPane.add(b3);
b4 = new JButton("Action 4");
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Ring tauschVariable = new Ring(0, 0);
for (int i = 0; i < arrayCount; i++) {
if (ringArray [i].calcArea() < ringArray [i + 1].calcArea()) {
ringArray [i] = tauschVariable;
ringArray [i + 1] = ringArray [i];
tauschVariable = ringArray [i + 1];
}
}
System.out.println("Ring with max. area: " + ringArray [0]);
}
});
b4.setBackground(Color.LIGHT_GRAY);
b4.setBounds(10, 182, 226, 23);
contentPane.add(b4);
}
}