Browse Source

Upgranding: sumCalc Formel (from to)

remotes/origin/develop
fdai7460 2 years ago
parent
commit
61060a5be6
  1. 47
      src/main/java/src/TasksFrame.java
  2. 11
      src/test/java/src/TasksFrameTest.java

47
src/main/java/src/TasksFrame.java

@ -66,7 +66,7 @@ public class TasksFrame extends JFrame {
setupDiff(); setupDiff();
setupSumCalc(0, 10);
setupSumFromTo();
setup5ModList(1, 20); setup5ModList(1, 20);
@ -207,22 +207,51 @@ public class TasksFrame extends JFrame {
return a-b; return a-b;
} }
private void setupSumCalc(int from, int to) {
public void setupSumFromTo(){
JPanel jPanel = setupPanel("Sum from to"); JPanel jPanel = setupPanel("Sum from to");
JLabel jLabel = new JLabel();
jLabel.setVisible(true);
jLabel.setBounds(30, 30, 50, 20);
JTextField a = new JTextField();
a.setBounds(10, 50, 30, 20);
a.setVisible(true);
JTextField b = new JTextField();
b.setBounds(50, 50, 30, 20);
b.setVisible(true);
JTextField result = new JTextField();
result.setBounds(100, 50, 40, 20);
result.setVisible(true);
JButton sumFromTo = new JButton("calculate");
sumFromTo.setBounds(10,90,100,30);
sumFromTo.setVisible(true);
sumFromTo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String texta = a.getText();
String textb = b.getText();
Integer val_a = Integer.valueOf(texta);
Integer val_b = Integer.valueOf(textb);
Integer res = sumCalc(val_a, val_b);
result.setText(String.valueOf(res));
}
});
jPanel.add(a);
jPanel.add(b);
jPanel.add(result);
jPanel.add(sumFromTo);
}
public int sumCalc(int from, int to) {
int sum = 0; int sum = 0;
for (int i = from; i < to; i++) { for (int i = from; i < to; i++) {
sum += i; sum += i;
} }
jLabel.setText(String.valueOf(sum));
jPanel.add(jLabel);
return sum+to;
} }

11
src/test/java/src/TasksFrameTest.java

@ -35,6 +35,17 @@ class TasksFrameTest {
assertEquals(-10,sum3); assertEquals(-10,sum3);
} }
@Test
void setupSumFromTo(){
TasksFrame frame = new TasksFrame();
int p1= frame.sumCalc(1,5);
assertEquals(15,p1);
int p2= frame.sumCalc(1,10);
assertEquals(55,p2);
int p3= frame.sumCalc(0,2);
assertEquals(3,p3);
}
} }

Loading…
Cancel
Save