This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2017/9/25

duplicate class

divinity divinity

2017/9/25

#
can some tell me where is the duplicate class in this program .and how to correct it this program is telling me that there are duplicate classes and I am not seeing any
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package calculator;
 
import javax.swing.*;
import java.awt.event.*;
 
public class Calculator extends JFrame implements ActionListener{
 
    JTextField CoA, CoB, CoC;
 
     
     JLabel Root1, Root2;
     JLabel CoAx, CoBx, CoCx, Root1x, Root2x;
     JButton calculateB, exitB;
     CalButton cbHandler;
     ExitButton exitButton;
 
    public Calculator() {
 
        setTitle("Quadratic Calculator");     
 
         
        CoAx = new JLabel("a", SwingConstants.CENTER);
        CoBx = new JLabel("b", SwingConstants.CENTER);
        CoCx = new JLabel("c", SwingConstants.CENTER);
        Root1x = new JLabel("Root1", SwingConstants.CENTER);
        Root2x = new JLabel("Root2", SwingConstants.CENTER);
 
         
        CoA = new JTextField(10);
        CoB = new JTextField(10);
        CoC = new JTextField(10);
        Root1 = new JLabel("", SwingConstants.RIGHT);
        Root2 = new JLabel("", SwingConstants.RIGHT);
 
        
        calculateB = new JButton("Calculate");
        cbHandler = new CalculateButtonHandler();
        calculateB.addActionListener(cbHandler);
 
        //Create Exit Button
        exitB = new JButton("Exit");
        ebHandler = new ExitButton();
        exitB.addActionListener(ebHandler);
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(6, 2));
 
         
        pane.add(CoAx);
        pane.add(CoA);
        pane.add(CoBx);
        pane.add(CoB);
        pane.add(CoCx);
        pane.add(CoC);
        pane.add(Root1x);
        pane.add(Root1);
        pane.add(Root2x);
        pane.add(Root2);
        pane.add(calculateB);
        pane.add(exitB);
 
        setSize(400, 300);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
 
    }
 
    private class CalButton implements ActionListener
    {
 
        @Override
        public void actionPerformed(ActionEvent e) {
 
            double a = Double.parseDouble(CoA.getText());
            double b = Double.parseDouble(CoB.getText());
            double c = Double.parseDouble(CoC.getText());
 
            double det = (4 * a * c);
            b = b * b;
 
            double R1 = Math.sqrt(b + det);
            double R2 = Math.sqrt(b - det);
 
            Root1.setText("" + R1);
            Root2.setText("" + R2);
 
        }
 
    }
    private class ExitsButton implements ActionListener{
 
    @Override
    public void actionPerformed(ActionEvent e) {
 
        System.exit(0);
 
    }
  }
     public static void main ( String[] args)
 
    {
 
        Calculator cal = new Calculator();
 
    }
 
}
 
 
 
 
    
danpost danpost

2017/9/26

#
Since this class is self-contained, try removing the first line and see if you still get the error. If you do not get it anymore, try renaming the classes above one at a time after putting the first line back in (the 'package' line).
divinity divinity

2017/9/26

#
hi danpost which first line you are talking about
divinity divinity

2017/9/26

#
hi danpost I did some modification, like removing the package calculator,(dont know if that is the correct thing to do) but it help in some way but in another way it created more errors. can u explain to me how to get it right, how to fix it. here is the new codes, at the side of it, I will explain where the errors is. plz any help would be appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import javax.swing.*;
import java.awt.event.*;
 
public class Calculator extends JFrame implements ActionListener{
 
    JTextField CoA, CoB, CoC;
 
     
     JLabel Root1, Root2;
     JLabel CoAx, CoBx, CoCx, Root1x, Root2x;
     JButton calculateB, exitB;
     CalButton cbHandler;
     JButton ExitButton ;
 
    public Calculator() {
 
        setTitle("Quadratic Calculator");     
 
         
        CoAx = new JLabel("a", SwingConstants.CENTER);
        CoBx = new JLabel("b", SwingConstants.CENTER);
        CoCx = new JLabel("c", SwingConstants.CENTER);
        Root1x = new JLabel("Root1", SwingConstants.CENTER);
        Root2x = new JLabel("Root2", SwingConstants.CENTER);
 
         
        CoA = new JTextField(10);
        CoB = new JTextField(10);
        CoC = new JTextField(10);
        Root1 = new JLabel("", SwingConstants.RIGHT);
        Root2 = new JLabel("", SwingConstants.RIGHT);
 
        
        calculateB = new JButton("Calculate");
        cbHandler = new CalculateButtonHandler();(the error here is saying to create a class)
        calculateB.addActionListener(cbHandler);
 
        //Create Exit Button
        exitB = new JButton("Exit");
        ebHnandler = new ExitButton();(here it telling me to create a class in package)
        exitB.addActionListener(ebHandler);(telling to create a local variable
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(6, 2));
 
         
        pane.add(CoAx);
        pane.add(CoA);
        pane.add(CoBx);
        pane.add(CoB);
        pane.add(CoCx);
        pane.add(CoC);
        pane.add(Root1x);
        pane.add(Root1);
        pane.add(Root2x);
        pane.add(Root2);
        pane.add(calculateB);
        pane.add(exitB);
 
        setSize(400, 300);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
 
    }
 
    @Override
    public void actionPerformed(ActionEvent ae) {
             
            double a = Double.parseDouble(CoA.getText());
            double b = Double.parseDouble(CoB.getText());
            double c = Double.parseDouble(CoC.getText());
 
            double det = (4 * a * c);
            b = b * b;
 
            double R1 = Math.sqrt(b + det);
            double R2 = Math.sqrt(b - det);
 
            Root1.setText("" + R1);
            Root2.setText("" + R2);
    }
 
    private class CalButton implements ActionListener
    {
 
        @Override
        public void actionPerformed(ActionEvent ae) {
                 
        }
 
         
 
    }
    private class ExitsButton implements ActionListener{
 
    @Override
    public void actionPerformed(ActionEvent e) {
 
        System.exit(0);
 
    }
  }
     public static void main ( String[] args)
 
    {
 
        Calculator cal = new Calculator();
 
    }
 
}
 
 
 
 
    
danpost danpost

2017/9/26

#
Okay. The thing is that you are no longer getting duplicate class errors. I would think that you then already have a Calculator class within the package. Try changing the name of the class above (lines 4, 15 and 106 will need 'Calculator' changed to something else) -- and put line 1 (the 'package' line) back in.
divinity divinity

2017/9/26

#
hey danpost I kinda open up another application but I change the package from calculator to simpleCalculator, so the error has gone from the public class calculator extends JFrame implements ActionListener which I would presume is good but the problem now, is that I am now getting other errors. I am only just going to copy and paste the codes where the errors are not the whole codes. so here goes nothing. any help will be appreciated. do I have to create a class to the ones that says create a class. here is the part of the codes that have the errors. at the side of it, I will explain what is the error says. what it is telling me what to do
1
2
3
4
5
6
7
8
9
cbHandler = new CalculateButtonHandler();- the error here is saying: create a class calculatebuttonhandler in simplecalculator
 
ebHnandler = new ExitButton(); - this error here is telling me to create a class in package simple calculator
        exitB.addActionListener(ebHandler);-this error here is saying, to cast ebhandler to actionlistener
 
 public static  void main ( String[] args){
this is the main, the error here is: illegal static declaration in inner class calculator2: Calculator
        
    }
danpost danpost

2017/9/26

#
It may be that this set of code is not placed properly within the class. The main method cannot be in a class defined within a class. The ExitButton class may be mismatched as far as spelling (you have ExitsButton above -- with an 's'). When an error message tells you to create a class, what it really means is that it cannot find the class. Make sure a class does exist with that exact name and also make sure that it is accessible to this class.
divinity divinity

2017/9/27

#
what does this error mean and how do i get rid of it: Illegal static declaration in inner class Calculator2.Calculator.ExitsButton
1
2
3
public static   void main ( String[] args){
 
  }
danpost danpost

2017/9/27

#
It means that your 'main' method is embedded inside a class within a class. It needs to be in the root of the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// legal
public class Calculator2  ...
[
    public static void main(String[] args)
    {
        ...
    {
     
    public class CalculatorExitsButton  ...
    {
        ...
 
// illegal
public class Calculator2 ...
{
    ...
 
    public class CalculatorExitsButton  ...
    {
        public static void main(String[] args)
        {
            ...
You need to login to post a reply.